Using PHP to manually expire the page _php tutorial

Source: Internet
Author: User
Use PHP to manually expire the page Detrox [translate]
Keyword page expires, registration page writing
SOURCE Http://www.phpbuilder.net/columns/clark20030702.php3

Manually expiring Web Pages
Manually expire pages

Author: Joe Clark
Translation: Detrox


After going through a series of pages during a registration process, you don't want the user to being able to go back after T He final submit. What can manually "expire" those pages, and perhaps display a custom message?

After completing a series of pages in the registration process, you do not want the user to be able to return to the previous page after the final submission. What you should do to make these pages expire manually, and if possible, give a custom message.

In this scenario, I didn ' t want my session to expire as I needed it to continue. Instead, I used an extra session variable to track whether my session is alive or not. There is three main components: (1) The entry script, (2) the Cache-control Directive, (3) The conditional check, and (4) Manually expiring a portion of the session.

In this sekiri, I don't want my session to expire, because I need it to keep working. I use an extra session variable to keep track of whether my session is active or not. There are three main components: (1) entry script (2) cache control indicator (3) condition detection and (4) manually expire part of the session.

The ENTRY SCRIPT
Portal Script
I Use the entry script to start my session. This accomplishes the things: (1) destroys any session already in progress, and (2) starts a new session.

I used an entry script to start my session. It is used to accomplish two things: (1) Destroy any session that already exists in the process, and (2) Start a new session.

entry.php:

PHP session_start (); Session_unset (); Session_destroy (); Session_Start (); Session_register (' Alive '); $_session["Alive"] = "1"; Header ("location:/php/createaccount.php");? >
In the above script, we start the session, get rid of any registered session variables with Session_unset (), and destroy T Hat session with Session_destroy (). Then, we start a new session and register a session variable. This particular variable would track whether this portion of the session was alive or not. We set the variable to some value and then we redirect to our first page in the registration series.

In the above script, we start the session, use Session_unset () to clear all the registered session variables, and use Session_destory () to destroy the previous session. Then, we start a new session and register a session variable. This particular variable will track whether this part of the session is active or not. We will set some values for the variable and then redirect to the first page of our list of registered pages.

Cache-control and CONDITIONAL CHECK
Cache control and condition detection
In the following code snippet, we'll auto-detect if the session is still on use.
In the short code below, we will automatically detect if the session is still in use.

createaccount.php:

PHP session_start (); Header ("Cache-control:must-revalidate"); if ($_session["Alive"]! = "1") {//user is attempting to go back after the session was destroyed//user attempted to return to Hea before session was destroyed Der ("location:/php/error100.php"); }?>
The "cache-control" directive above is very important. The Using "Must-revalidate" tells the browser that it had to fetch the page from the server again instead of loading if from I TS Cache. Because It reloads the page from the server, it'll re-check the $_session["alive"] variable to see if it's value is "1". If So, the page can load properly. If not, then we'll redirect the user to another page, contains a custom error message. Placing this script at the beginning of every page in the registration series would catch every "back" button press by the User. It's not enough-to-place it on the last page of the registration series as a user could press the "back" button more than One time. I have this snippet in createaccount.php, createaccount1.php, createaccount2.php and createaccount3.php.

The above cache control indicator symbol is very important. Use "Must-revalidate" to tell the browser to read the Web page from the server side instead of using the cache from the browser. Because the page reread from the server side will recheck the $_session["alive" variable to see if his value is 1. If yes, the page will be read normally, if not then we will redirect the user to a webpage that has customized the error message. By placing this script at the beginning of each page of the registered series page, you can capture the user's click on the Back button every time. It is not enough to put this script on the last page of a series of registered pages, because users may click the Back button more than once. I wrote this to createaccount.php, createaccount1.php, createaccount2.php and createaccount3.php.

Manually EXPIRE the SESSION
To manually expire the session
The last thing to does is manually "expire" the session, or at least a portion of it. In my case, I wanted the session to stay alive, so I could not use Session_unset () or Session_destroy (). However, I didn ' t want the user to go back to the previous pages and change things. Remember that $_session["alive"] variable? After the final submit, all we had to does is get rid of it. There is and ways to does this:

The last thing to do is to manually expire the session, or at least make a portion expire. In this case, I want to keep the session active, so I can't use Session_unset () or Session_destroy (). But anyway, I don't want to let users go back to the previous page to change anything. Remember $_session["Alive"] variables? All we have to do is get rid of it after the last commit. There are two ways to achieve this.

createaccount4.php (the page after the final submit):

or
Either accomplish the same thing. Now while the "Back" button is pressed, the user won ' t return the the previous page and being able to change data and RESUBMI T. Instead, they'll be redirected to error100.php (or whatever page you choose) and would get a custom error message.

Either method can do the same thing. Now, when the "Back" button is pressed, the user will not be able to return to the previous page to make data changes and repeat submissions. Instead, the user is redirected to error100.php (or any page you choose) and gets a customized error message.

So, the next time you want to stop the user from going back to change data previously entered, and if you want manual cont Rol over it, use the This method. Just Remember the entry script sets the session variable to the ' Alive ' state, and the ' Exit script ' (right after your Final submit during the process) sets the session variable to a ' not alive ' state. The "Cache-control:must-revalidate" forces the browser to reload the page from the server, and the "Alive" check is Perfo Rmed. Redirection to a custom page occurs when the session variable was not "alive".

Therefore, the next time you want to prevent users from returning to the previous page to change the previously entered data, use this method if you want to control it manually. Remember to use the portal script to set the session variable to the "Alive" state, using the exit script (after the final commit action during processing) to set the session variable to the "not Alive" state. "Cache-control:must-revalidate" forces the browser to re-read the Web page from the server and implement "Alive" detection. Redirects to a custom page when the session variable is no longer "alive".

http://www.bkjia.com/PHPjc/735099.html www.bkjia.com true http://www.bkjia.com/PHPjc/735099.html techarticle use PHP to manually expire the page Detrox [Translate] keyword page expires, the registration page to write the source http://www.phpbuilder.net/columns/clark20030702.php3 manually expiring Web Pages Manual ...

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