Forum Moderators: coopster
I am am having a completely baffling intermittent problem with a login page that sets a session up.
It seems totally random whether the session is set or not. Sometimes it works and sometimes it doesn't.
On all subsequent pages I have included the 'session start();' command (am I missing something there? should there be something in the brackets?) via an included auth page that checks to see if a couple of the elements are set. If not it chucks you out to the login page. The same test is done by this same file on every page.
So maybe there is a problem with the login file itself:
<?php
session_start ();
include("../conn.inc.php");
if (isset ($_GET['do'])) {
$sql = "SELECT staff_username FROM staff
WHERE staff_username='$_POST[staff_username]'";
$result= mysql_query($sql)
or die("Could not execute query.");
$num = mysql_num_rows($result);
if ($num >= 1)
// login name was found
{
$sql="SELECT staff_id, staff_username, staff_adminlevel FROM staff
WHERE staff_username='$_POST[staff_username]'
AND staff_password='$_POST[staff_password]'";
$result2 = mysql_query($sql)
or die("Couldn’t execute query.");
$num2 = mysql_num_rows($result2);
$row = mysql_fetch_assoc($result2);
if ($num2 > 0) //password is correct
{
$logname=$_POST['staff_username'];
$password=$_POST['staff_password'];
$logid= $row['staff_id'];
$adminlevel= $row ['staff_adminlevel'];
$_SESSION['auth']="yes";
$_SESSION['staff_id']=$logid;
$_SESSION['logname']=$logname;
$_SESSION['password']=$password;
$_SESSION['adminlevel']=$adminlevel;
$datetime= date("Y-m-d h:i:s");
$sql = "INSERT INTO staff_logintime (staff_id, staff_logintime) VALUES ('$logid', '$datetime')";
$result= mysql_query($sql) or die ("cant add the date time stuff");
header("Location: admin_area.php");
}
else // password is not correct
{
unset($do);
$message="!The login name, '$_POST[staff_username]'
exists, but the password is incorrect!<br>";
include("admin_login_form.inc.php");
}
}
elseif ($num == 0) // login name not found
{
unset($do);
$message = "The login name you entered does not
exist! Please try again.<br>";
include("admin_login_form.inc.php");
}
}
else {
$_SESSION=array();
include("admin_login_form.inc.php");
}
?>
I am new to this so I am sure there are some screwups in there but why does it work some times <i>with the same pages</i> and coming from the same locations, and not at others!?
Is it a problem with my web host maybe? Do I need to make the pages go from one to another more slowly or something? do I need to do something to retrieve the session variables and turn them into page variables? I have tried everything I can think of for days and just not cracked it.
Hope someone can help
Dave
Anyway it seems like the white space could have been the problem. I have deleted it all (being an arty type for whom none of this comes n aturally in the slightest my pages were littered with white space to help me make sense of it all!) Thus far on brief testing it is looking promising.
If any of you know and have the time to explain I would be very interested why 'nothing' (i.e. white space) can cause a promlem in the computer world?
Anyway thanks very much for you time and help BUT I may be back!
My admin_auth.inc.php file is like this:
<?php
session_start ();
if ($_SESSION['auth'] ==''¦¦$_SESSION['password'] =='')
{
$redirect = $_SERVER['PHP_SELF'];
header ("Refresh: 2;URL=index.php?redirect=$redirect");
$notlogged= "!YOU ARE CURRENTLY NOT LOGGED IN!<br> we are redirecting you, hang on...<br>(if your browser doesnt redirect you in 10 seconds <a href=\'admin_login.php?redirect=$redirect\'>click here<a/>)";
}
else{
$logout="<a href='index.php'>log-out</a>";
}
?>
is there anything wrong with that? I even see that the details that this code looks for is in the session array after I get tracked back sometimes! (via a print_r use for testing). It is just totally baffling. I'll set someone up a login and you can see for yourselfs if it helps?