woodplease Posted September 20, 2010 Share Posted September 20, 2010 i've got a piece of code, which acts as a page view counter by updating a field in a table everytime the page is loaded. Also, a session is created containing the current number of page views. This works except for the fact that everytime the page is refreshed, the counter is updated. To solve this i added" if (!isset ", to see if the session exists. if it does, it does not run the code, but if it doesnt exist, then it does. However, when i added the "if (!isset"... , the code stops working, and nothing gets updated if (!isset($_SESSION['pageviews'])){ $count = mysql_query("SELECT * FROM topic WHERE topic_id =" .$id) or die("Select Error3 :" . mysql_error()); $current = mysql_fetch_assoc($count); $view = $current['views']; $new = $view + 1; $update = mysql_query("UPDATE topic SET views =" .$new. " WHERE topic_id =" .$id) or die("Select Error3 :" . mysql_error()); $_SESSION['pageviews'] = $new; } Any ideas on whats wrong will be great. Link to comment https://forums.phpfreaks.com/topic/213950-if-not-set/ Share on other sites More sharing options...
vungee Posted September 20, 2010 Share Posted September 20, 2010 Do you have the following code at the beginning of your page? session_start(); This is require to use sessions... just a thought... Link to comment https://forums.phpfreaks.com/topic/213950-if-not-set/#findComment-1113477 Share on other sites More sharing options...
woodplease Posted September 20, 2010 Author Share Posted September 20, 2010 yes, it is at the beginning of the page Link to comment https://forums.phpfreaks.com/topic/213950-if-not-set/#findComment-1113478 Share on other sites More sharing options...
Pikachu2000 Posted September 20, 2010 Share Posted September 20, 2010 Then it sounds like it's working. How are you testing to make sure that $_SESSION['pageviews'] is unset when you test the script? Add an else to the conditional to check it. if( !isset($_SESSION['pageviews']) ) { //do all the stuff you have up there . . . } else { echo '$_SESSION['[pageviews'] is already set, so not updating counter'; } Link to comment https://forums.phpfreaks.com/topic/213950-if-not-set/#findComment-1113484 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.