Session variables are not working properly. Doesn't hold the values 'sometimes'

session variables are acting weird. I try to login the first time, it doens' let me, I try it second time, I can get in most of the time. But, seems like the session variables are not retaining the values properly.
Here' what I have...
page 1 ----------------------------------------------
        <form action="auth.php" method="post">
        <legend>Please Log In</legend>
        <label for="user_name">Username: </label>
        <input type="text" name="user_name" id="user_name" value="<?php echo $user_name; ?>"/>
        <label for="user_password">Password:</label>
        <input type="password" name="user_password" id="user_password" value="<?php echo $user_password; ?>" />
        <br class="clear" />
        <input type="submit" name="submit" id="submit" value="submit"  />

page 2 ---------------------------------------------------
<?php session_start();

$input_username = $_POST['user_name'];
$input_password = $_POST['user_password'];


$cn = mysql_connect($host, $userName, $pass);

mysql_select_db('dev_graphics', $cn);
$sql = "select * from admin where adm_username = '$input_username' and adm_password = '$input_password' ";
$rs = mysql_query($sql, $cn);
$num_rows = mysql_num_rows($rs);

if($num_rows == 1){
$_SESSION['authenticate'] = $input_username;

page 3 --------------------------------------
<? session_start();
if (!$_SESSION['authenticate']) header("Location:index.php");
echo $_SESSION['authenticate'];

The ODD thing is the first time I try to login (using correct user name/password, it stays on index.php page which means it finds the record but session variable is blank, the first time, second time it works sometimes. sometimes it doesn't
any idea?
Who is Participating?
syedasimmeesaqConnect With a Mentor Commented:
Also make sure that your php.ini has this value corrected

session.save_path = "C:\php\temp"

it shows the correct path to temp directory in your PHP folder

make sure you have read and write permission on your temp directory for internet guest account and other accounts that would use it sometime Network services if you are on server 2003

are you using IIS or apache? I had this trouble with IIS and not Apache. Let me know
awarraicAuthor Commented:
I am using IIS.
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

ok, I suspected so. This is what you need to do. make sure your temp directory within PHP folder has read and write permissions. Let me know .
awarraicAuthor Commented:
I 'll try that on Monday, 'll have to talk to the network guy. it's weekend now so 'll keep you updated after the weekend. thanks much.
Best regards and have a great weekend.
awarraicAuthor Commented:
I talked to the Network guy and it seems like something else is going on with session variables. Rest of all web sites are working fine. Still having the same problem, first time I log in, session variables are blank, second time it works fine. Any other suggesions?
Its the exact same problem I had. What version of PHP are you using. Because there is a problem with older version of PHP and IIS.
awarraicAuthor Commented:
Sorry, have no control over network, also, all rest of the sites are working fine so not sure why this one site will act ODD but thanks much for your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.