Jump to content

Recommended Posts


Hi all,

Wonder if anyone could help with the following problem i'm having.

Ok I have a script and am trying to modify the bit that updates the database
with the number of times the page has been viewed.

In the script at present every time the page is reloaded then the count increments
by one. Obviously that brings up the problem of the page just getting reloaded to
increase the number of hits so I want it to check the session so that it would update
only once even if the same person refreshed the page several times.

At the top of the script it calls in a function to start a session as this is required for
other aspects of the site then towards the end it has the following that updates
the database:

$UP = "UPDATE $ads_tbl SET sitehits = sitehits + 1 where siteid = '{$_GET['sid']}'";
mysql_query ($UP) or die(mysql_error());
} else {
    echo "<td>$error</td></tr></table></td></tr></table>";
}


How would i change the above to prevent reloads adding to the count??


Any help appreciated  ???

Cheers

Steve
Link to comment
https://forums.phpfreaks.com/topic/28445-web-counter-question/
Share on other sites

you could do something like this, it involves arrays as well.

[code=php:0]
session_start();
if(!isset($_SESSION['page'])){ $_SESSION['page'][] = $_SERVER['REQUEST_URI']; }

if(!in_array($_SERVER['REQUEST_URI'], $_SESSION['page'])){
//UPDATE THE HITS
$_SESSION['page'][] = $_SERVER['REQUEST_URI'];
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130372
Share on other sites

If you wanted to recount the same user after x minutes you could do something like the following:
(at the top of the page)
[code=php:0]
$minvar = 5; //how many minutes you wish to elapse before a user is recounted.
$UP = "UPDATE $ads_tbl SET sitehits = sitehits + 1 where siteid = '{$_GET['sid']}'"; // your update query
if(!$_SESSION['lutime']) {  //sets the var if its not set.
$_SESSION['lutime'] = time();
mysql_query ($UP) or die(mysql_error()); // if the variable isnt set that means the user is new, so update
}
elseif(($_SESSION['lutime'] + ($minvar*60)) < time()) { //if the last updated time + seconds * minvar > the current time, update
mysql_query ($UP) or die(mysql_error()); // its been x minutes since the user was counter, so count them
$_SESSION['lutime'] = time();
}
[/code]

Hmm that should work (if you wish to use it).  It might not work though (writtin in the little quick reply box at 1 AM)...

(edited to reflect the following post.)
Link to comment
https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130391
Share on other sites

correct me if i'm wrong but shouldn't this:
[code=php:0]($_SESSION['lutime'] + ($minvar*60)) > time()[/code]
be this:
[code=php:0]($_SESSION['lutime'] + ($minvar*60)) < time()[/code]

because its always going to be greater then time() if you add it. so when time() catches up to it and becomes greater then it we can run the code and update it again.
Link to comment
https://forums.phpfreaks.com/topic/28445-web-counter-question/#findComment-130398
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.