woodplease Posted January 27, 2011 Share Posted January 27, 2011 i'm creating a page counter which updates a value in a database each time the page is loaded. I'm trying to make it so that it checks to see if a session has been set, if not, it updates the database, and then sets the session. This way it wont update every time someone refreshes the page. $id=$_GET['id']; if(!isset($_SESSION[$id])){ $_SESSION[$id]= $id; $views = $row['views'] + 1; $update_views=mysql_query("UPDATE topic SET views='".$views."' WHERE topic_id='".$id."'") i want to set the session variable as that of the page id ($id) The problem is that it keeps updating the database everytime the page is reloaded. I'm not sure if i'm setting the session variable correctly. Any ideas would be great Thanks Link to comment https://forums.phpfreaks.com/topic/225868-page-counter-setting-a-session-variable/ Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 do you have session_start() somewhere above that code? Link to comment https://forums.phpfreaks.com/topic/225868-page-counter-setting-a-session-variable/#findComment-1166070 Share on other sites More sharing options...
woodplease Posted January 27, 2011 Author Share Posted January 27, 2011 yes, right at the top, first line Link to comment https://forums.phpfreaks.com/topic/225868-page-counter-setting-a-session-variable/#findComment-1166081 Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 can we see all relevant code? Link to comment https://forums.phpfreaks.com/topic/225868-page-counter-setting-a-session-variable/#findComment-1166084 Share on other sites More sharing options...
woodplease Posted January 27, 2011 Author Share Posted January 27, 2011 its ok, i've fixed it now, turns out i needed to add some quotes to the variable if(!isset($_SESSION["'$id'"])){ $_SESSION["'$id'"]= $id; Thanks anyway Link to comment https://forums.phpfreaks.com/topic/225868-page-counter-setting-a-session-variable/#findComment-1166087 Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 that doesn't make sense, but whatever works. Link to comment https://forums.phpfreaks.com/topic/225868-page-counter-setting-a-session-variable/#findComment-1166089 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.