0

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
  • more detail please .. user logs in via page oNE and then is taken to a page where you want to limit the number of times (including refresh) that they can view it ? Commented Oct 1, 2009 at 3:36
  • Yeah, user logs in, and gets sent to a survey, which comes in 2 pages, page 1 adds 1 to the access count. That's where refresh would run the code again. Commented Oct 1, 2009 at 3:39
  • I assume you only run your log-in code once, why not just run the AccessCount code right after the log-in code i.e., when a use logs in? Or are you using HTTP authentication? Commented Oct 1, 2009 at 3:40

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Sessions are definitely how I would handle this.
1

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.

1 Comment

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!
0

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.

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.