PDA

View Full Version : VERY Frustraiting SESSION variable problem.


lpeek
06-03-2008, 11:54 AM
I have a system where a user logs in, and the number of times they have logged in is stored in a database..

the code stored this var in a session variable:


$_SESSION['username'] = $userinfo['username'];
$_SESSION['user_id'] = $userinfo['user_id'];
$_SESSION['login_num'] = $userinfo['login_num'];


and then uses this code to display a message if its their first time:


if ($_SESSION['login_num']<1)
{
if ($_SESSION['user_login_num']==0)
{
// WELCOME TEXT

$login_num = $_SESSION['user_login_num']+1;
$_SESSION['user_login_num'] = $login_num;
$user_id = $_SESSION['user_id'];
$sql = mysql_query("UPDATE users SET login_num='".$login_num."' WHERE user_id='".$user_id."'") or die(mysql_error());
}
}


The thing is... no matter what value the login_num has in the database, its always set back to 0 when a user logs in, so it displays the welcome text on every login...

Its pretty confusing as I have 2 different login areas and the other works fine.

Any help greatly appreciated.

Cheers

Piba
06-03-2008, 12:36 PM
try to close the session after write the value of login_num:


$_SESSION['username'] = $userinfo['username'];
$_SESSION['user_id'] = $userinfo['user_id'];
$_SESSION['login_num'] = $userinfo['login_num'];
session_write_close();


This may solve the problem

lpeek
06-03-2008, 12:48 PM
try to close the session after write the value of login_num:


$_SESSION['username'] = $userinfo['username'];
$_SESSION['user_id'] = $userinfo['user_id'];
$_SESSION['login_num'] = $userinfo['login_num'];
session_write_close();


This may solve the problem

Cheers, but no different :(

Fumigator
06-03-2008, 03:25 PM
While your code has a lot of redundancy in it, it does look like it should work. Though, what's the point of using a $_SESSION['login_num'] variable and a $_SESSION['user_login_num'] variable? And where is $_SESSION['user_login_num'] assigned a value initially? Also, can you show us the code where $userinfo['login_num'] is assigned its value?

I'm assuming your UPDATE query has no errors... can we see the value of your $_SESSION array just before you go into the "if" statement? Use print_r($_SESSION).

JamieThompson
06-03-2008, 07:48 PM
I think I have spotted your error:
You call if ($_SESSION['user_login_num']==0)
Firstly you have not stated the value of user_login_num I assume that you mean $_SESSION['login_num'] Changing this will fix your problem!
The reason that your code is working is that the value of the if statement is false or equal to 0.
However you could use === (identical to)
if ($_SESSION['user_login_num']===0)
If your code was correct in the first place.

Hope that helps!
Thanks if it does!

lpeek
06-10-2008, 10:05 AM
No that didnt work :(

it's really bugging me, havent been able to figure it out for 2 weeks now.

It seems to be not storing the tables field in the session variable, yet the code stores the rest of the data correctly...

Never mind... i'll have to just scrap it :(

lpeek
06-10-2008, 10:10 AM
While your code has a lot of redundancy in it, it does look like it should work. Though, what's the point of using a $_SESSION['login_num'] variable and a $_SESSION['user_login_num'] variable? And where is $_SESSION['user_login_num'] assigned a value initially? Also, can you show us the code where $userinfo['login_num'] is assigned its value?

I'm assuming your UPDATE query has no errors... can we see the value of your $_SESSION array just before you go into the "if" statement? Use print_r($_SESSION).

Done that... the result is very bizzare...

it shows that the variable hasnt actually been set, yet the 3 other session variables have.... now im even more confused

heres the code that SHOULD set the session vars:


$_SESSION['username'] = $userinfo['username'];
$_SESSION['user_id'] = $userinfo['user_id'];
$_SESSION['firstname'] = $userinfo['firstname'];
$_SESSION['user_login_num'] = $userinfo['login_num'];


and the missing variable earlier was just my mistake >.<
there is no $_SESSION['login_num'] just $_SESSION['user_login_num']

Fumigator
06-10-2008, 03:42 PM
Also, can you show us the code where $userinfo['login_num'] is assigned its value?

This would be helpful....