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. Quote Link to comment 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... Quote Link to comment 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 Quote Link to comment 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'; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.