tags:

views:

125

answers:

4

So I have this page that can only be seen by a user 5 times. And I made this code that goes into the database and it adds 1 to the AccessCount field everytime the user logs in. What I want to do is that if the user refreshes his web browser, have the code that records the AccessCount NOT run.

How can I do this? Thanks!

+3  A: 

count the number of executions of the php script that generates the page you want to limit, instead of counting logins . you can use $_SESSION and session functions to maintain and retrieve the id of the user for whom the script is running

Scott Evernden
Sessions are definitely how I would handle this.
Christian
+1  A: 

if they counter is being incremented only when the user logs in, only increment it in the portion of the code that logs them in (sets cookies, session vars, etc), no need to worry about page reloads.

otherwise there is no way to detect a new 'visit' by a user vs. a page reload. you could set a time frame in which they could refresh the page and not have the counter incremented.

contagious
I used a variant of this. When the user logs in I send him to a page exclusively for the increment (and a few other settings), then that page sends him over to the survey. Thanks!
Carlo
A: 

Typically you would do the counting in another page and pass the user to the page that they are only supposed to see 5 times after you have incremented the count. It is similar to what you would do after submitting a form, submit the form to a page that enters the data to the database then pass the browser along to a new page telling them it is complete. This way reloading the page doesn't continue to update the database with either the form data or in your case the count data.

catfarm
A: 

You have to use sessions for the solution. The code below should work for your purposes.

session_start();

function incrementAccessCount() {
    if (empty($_SESSION['loggedin'])) {

        /* mysql code to increment access count */

        $_SESSION['loggedin'] = 1;
    }

}

incrementAccessCount();
Alec Smart