Channel6 Posted September 28, 2011 Share Posted September 28, 2011 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 https://forums.phpfreaks.com/topic/248024-_session-doesnt-seem-to-work/ Share on other sites More sharing options...
AyKay47 Posted September 28, 2011 Share Posted September 28, 2011 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 https://forums.phpfreaks.com/topic/248024-_session-doesnt-seem-to-work/#findComment-1273526 Share on other sites More sharing options...
Buddski Posted September 28, 2011 Share Posted September 28, 2011 !mysql_num_rows is not valid.. It mightn't be perfect but it is still valid code. $var = 0; if (!$var) { echo 'Works'; } Link to comment https://forums.phpfreaks.com/topic/248024-_session-doesnt-seem-to-work/#findComment-1273536 Share on other sites More sharing options...
Buddski Posted September 28, 2011 Share Posted September 28, 2011 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 Link to comment https://forums.phpfreaks.com/topic/248024-_session-doesnt-seem-to-work/#findComment-1273540 Share on other sites More sharing options...
Channel6 Posted September 28, 2011 Author Share Posted September 28, 2011 Buddski, That's what I originally did. But it turned out that somehow some visitors had more than 1 IP address, so it counted double... even up to 4 times as many views than it should have. Any other suggestions? Link to comment https://forums.phpfreaks.com/topic/248024-_session-doesnt-seem-to-work/#findComment-1273583 Share on other sites More sharing options...
AyKay47 Posted September 28, 2011 Share Posted September 28, 2011 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 https://forums.phpfreaks.com/topic/248024-_session-doesnt-seem-to-work/#findComment-1273586 Share on other sites More sharing options...
Zane Posted September 28, 2011 Share Posted September 28, 2011 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 https://forums.phpfreaks.com/topic/248024-_session-doesnt-seem-to-work/#findComment-1273588 Share on other sites More sharing options...
AyKay47 Posted September 28, 2011 Share Posted September 28, 2011 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 https://forums.phpfreaks.com/topic/248024-_session-doesnt-seem-to-work/#findComment-1273590 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.