Jump to content

if not set


woodplease

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.