Hello,
I've been building a client log-in for my site and I seem to have everything working except that I need to log-in twice to make the authentication go through properly. I've got my PHP code from my page that does the log-in posted below, but I will explain the whole process first and what I think is going wrong. I hope someone can tell me where I've messed up.
My client log-in is pretty standard, but I wanted to take a shot at building it myself so I can learn from the experience and so it does everything I need it to do. The process starts with a simple HTML page containing a form that POSTs a username and password to a page called login.php.
The login.php page receives the variables, queries the database, and retrieves some info about a user if the username and password match up with the info in the database. I need to be able to pass one of these variables ("company") onto subsequent pages using PHP sessions.
The first time I start a browser and use my log-in, it doesn't work. I get kicked back to my log-in screen because the "protected" page has checked the "company" variable against whatever company I have specified on that page.
But, if I reenter the same exact information in my client log-in a second time, the whole thing works fine. For some reason, the second time the information is entered in, the info that was pulled from the database gets recorded into my $company variable. The first time around it is not stored in the variable.
Once I've logged in successfully, I can use my logout.php page and destroy my session. To re-authenticate myself, I thought I would have to reenter my information twice again on my client log-in, but for some reason, I only need to log-in once. I don't get kicked back again.
Here's my thought at what's wrong: I have to have another step between querying the database and trying to store the variable in a session variable. But I don't know how to do that now or if that's even the problem.
Here's my code. Any help would be appreciated. Thanks. - John
<?php require_once('myConnectInfo.php');
$username=$_POST["username"];
$password=md5($_POST["password"]);
mysql_select_db($database_myConnectInfo, $myConn);
$query_login = "SELECT username, password, gotoURL, company FROM mytable
WHERE username= '$username' and password= '$password'";
$login = mysql_query($query_login, $myConn) or die(mysql_error());
$row_login = mysql_fetch_assoc($login);
$totalRows_login = mysql_num_rows($login);
if($totalRows_login >=1) {
$path = $row_login["gotoURL"];
$company = $row_login["company"];
session_start();
session_register("company");
// user gets directed to the path from the database
header("Location: $path");
exit;
} else {
// if username and password didn't work, goes back to login
header("Location: index.php");
exit;
}
?>
-John 😕