Jump to content

$_SESSION Doesn't Seem To Work...


Channel6

Recommended Posts

I've written a script where it counts the page impressions on my website.

 

When a visitor lands on the page, it counts their impression and then sets a $_SESSION so that if they reload or return to the page it doesn't count it again. But the problem is it sometimes does.

 

Does this mean that the $_SESSION isn't being set probably? That's what it seems to be. And if so, what would be the reason for that?

 

Here's my basic code:

 

if(isset($_SESSION['userid'])){ 
     $userid = $_SESSION['userid']; 
} 
else{ 
     $userid = rand(1000, 10000000); 
     $_SESSION['userid'] = $userid; 
} 

$date = date('Y-m-d'); 

$counters = mysql_query("SELECT * FROM counters 
WHERE userid='$userid' 
AND impressions='1' 
AND date='$date'") 
or die(mysql_error()); 

if(!mysql_num_rows($counters)){ 
     mysql_query("INSERT INTO counters 
     (userid, impressions, date) 
     VALUES('$userid', '1', current_date)") 
     or die(mysql_error()); 
} 

Link to comment
Share on other sites

the return value for mysql_num_rows is an integer not a boolean value.. !mysql_num_rows is not valid..

 

if(mysql_num_rows($counters) == 0){ 
     mysql_query("INSERT INTO counters 
     (userid, impressions, date) 
     VALUES('$userid', '1', current_date)") 
     or die(mysql_error()); 
} 

Link to comment
Share on other sites

Instead of creating a random number for each person who visits your site, why not log the IP address? This will remove the need for a session.

Just a thought :)

can't rely on that method as a user can have multiple ip adresses..

 

OP is your script still not working with the change that i suggested?

 

Link to comment
Share on other sites

I don't see anywhere in your provided code where you are actually starting the session with session_start

 

On another note,

the return value for mysql_num_rows is an integer not a boolean value.. !mysql_num_rows() is not valid..

It is so valid.  What gave you the idea that it wasn't

The number of rows in a result set on success or FALSE on failure.

 

And if there are zero rows, well then 0 is the same a false.  If this were a mysql issue, I'd imagine the OP would have posted a mysql error.

Link to comment
Share on other sites

I don't see anywhere in your provided code where you are actually starting the session with session_start

 

On another note,

the return value for mysql_num_rows is an integer not a boolean value.. !mysql_num_rows() is not valid..

It is so valid.  What gave you the idea that it wasn't

The number of rows in a result set on success or FALSE on failure.

 

And if there are zero rows, well then 0 is the same a false.  If this were a mysql issue, I'd imagine the OP would have posted a mysql error.

i stand corrected.. i forgot that it returns an int on success and FALSE on failure... how stupid of me..my apologies..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.