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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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