Hi there, I've run into a problem with sessions and mysql that I'm trying to use to authenticate users. I'm running XP with PHP 4.2.3, MySQL 3.23, and Apache 2.0.36. PHP code seems to run fine on my server, and I can store some cookies and sessions - but they don't always seem to work. My site authenticates a user by looking up the username and password in the database, and then sending them to their respective folders (like admin\member folders). That part works. With the incorrect login it gives the user failure.php, and with the correct one it sends the user to the correct folder. The problem is that all of the files in the admin folder have code to see if the person is authenticated. So after I successfully login, it sends me to the Admin folder, but then it rejects me and says I don't have authorization.
I've tried just making a simple .php file that is the re-direct after a correct username\password is entered, like this:
print "Your username is: ".$_SESSION["USERNAME"];
and it prints it out correctly, so that tells me that the session is being stored. Here is the code that is at the top of my files to see if the user is authorized:
<?php
// DB SETTINGS
$dbhost = "localhost"; // DB Host name
$dbusername = "root"; // DB User
$dbpass = "****"; // DB User password
$dbname = "test-auth"; // DB Name
$query = "SELECT * FROM authuser WHERE uname='$username' AND passwd='$password' AND status <> 'inactive'";
$connection = mysql_connect($this->HOST, $this->USERNAME, $this->PASSWORD);
$SelectedDB = mysql_select_db($this->DBNAME);
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
// CHECK IF THERE ARE RESULTS
// Logic: If the number of rows of the resulting recordset is 0, that means that no
// match was found. Meaning, wrong username-password combination.
if ($numrows == 0) {
print "NO";
}
elseif ($row["level"]==1) { // ADMIN LOGIN
print "WINNAR";
}
else {
print "ORDINARY MEMBER";
}
?>
I've simplified this a bit to see where the problem is, but it consistently returns "NO" when I login. Is that because it can't find it in the database, or is that because it doesn't like the username\password combination?
The session code to the initial login.php file is as follows:
<?php
session_start();
$_SESSION["USERNAME"] = $username;
$_SESSION["PASSWORD"] = $password;
I cannot tell where the problem is, but I would appreciate any help that could be offered. I've tried cookies vs. sessions, and different ways of connecting through mysql - but still nothing works. Thanks.
Jim