Analyze the use of session to prevent page refreshing

Source: Internet
Author: User
A short piece of code prevents page refreshing, which may not be of much purpose. it is just a reference. Principle

A short piece of code prevents page refreshing, which may not be of much purpose. it is just a reference.
Principle: when a page is accessed, check whether a session exists. If no session exists, create a session and set it to the current time. The program runs down normally. if a session exists, determine the time difference between the session time and the current time. if the interval is less than the specified time, for example, 5 minutes, the program is interrupted and an error message is prompted, if the time in the session is greater than the current time, the time in the session is refreshed and the page is executed normally.
The code is as follows:

Session_start (); // start the session
$ TimeOutLimit = "300"; // you can specify a time interval of five minutes.
If (isset ($ _ SESSION ["timeout"]) {// determines whether a session exists.
If (time ()-$ _ SESSION ["timeout"] <$ timeOutLimit) {// if a session exists and the session interval is less than 5 minutes, die () exit and output a prompt.
Die ("Please do not refresh the current page multiple times ");
} Else {// if no timeout occurs, reset the session time to the current time.
$ _ SESSION ["timeout"] = time ();
}
} Else {
$ _ SESSION ["timeout"] = time (); // if no timeout session exists, the session is created as the current time, which is easy to judge when you access the page next time.
}
// Other page code .....
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.