If you'd like to do it in a session variable, do this.
<?php
$host = "---";
$username = "---";
$password = "---";
$db = "---";
$tbl_name = "---";
// Connect to server and select database.
mysql_connect($host, $username, $password) or die("Unable to connect to database.");
mysql_select_db($db) or die("Unable to connect to database.");
// If no magic quotes, add slashes
if(!get_magic_quotes_gpc()) {
$myusername = addslashes($_POST['myusername']);
$mypassword = addslashes($_POST['mypassword']);
}
// Username and password sent from form.
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
// Mysql_num_row is counting table row
$count = mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1) {
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
session_register("mycompany");
// Assign the mycompany variable from user table
$_SESSION['mycompany'] = $row['company'];
// Update the last_login field of the MySQL database
$lastlogin = mysql_query("UPDATE $tbl_name SET last_login=now() WHERE username='$myusername'");
// Redirect good login attempt
header("location:login_success.php");
}
// Redirect bad login attempt
else {
header("location:login_retry.php");
}
?>
I'm not completely certain you need to register the username and password with the session, but I may be wrong. It's be a while since I wrote my own session handler.
Oh, and if magic_quotes_gpc is enabled, you should use stripslashes() on the submitted form data and visa versa.