Hi All,

Using php and MySQL, I completed a login session script and it works great. I am assigning the username and password to select users, and there will be no registration form.

My "users" table in MySQL has 5 columns as follows:

  • id
  • username
  • password
  • company
  • last_login

I need help with two things:

  1. How do I INSERT datetime to the MySQL db from the php login script?
  2. How do I echo the company name on every page of the website whether or not the page contains protected content?

The session_start() is on every page. Protected pages contain an authentication script after the session_start(). My authentication script does not verify the company name, but I want to display the company name upon session register for the duration of the session. Obviously, session_destroy() will terminate display of the company name.

Thanks for the help.

How do I INSERT datetime to the MySQL db from the php login script?

Assuming you have the code in place to set up the username and password vars from your form:

//connect to database
dbConnect();
//query the database
$query = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
//if there is a match proceed
if (mysql_num_rows($query) == 1) {

$row = mysql_fetch_array($query);
//set a session variable to company
$_SESSION['company'] = $row['company'];
}

//now would be a good time to update the database for this user
$lastLoginQuery = mysql_query("UPDATE users set last_login=NOW() WHERE id = $row[id]");

How do I echo the company name on every page of the website whether or not the page contains protected content?

On each page after the session_start(); function, try some code like this:

//check to see if the variable is set in the $_SESSION array
if (isset($_SESSION['company']) {
//echo it to the browser, or whatever you want to do with it
 echo "<h2>".$_SESSION['company']."</h2>";
}

Joe, thank you. That makes mucho sense and I'm def on the right track. I can't try it out til tomorrow, but I'll post back and let you know. ~gp

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.