Use PHP to manually expire a page

Source: Internet
Author: User
Tags exit continue manual php and sessions

Manually expiring Web Pages

Manual page Expiration

Author: Joe Clark

Translation: Detrox

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

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

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

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

The ENTRY SCRIPT

Portal Script

I use a entry script to start the session. This is accomplishes two things: (1) destroys any sessions 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) destroying any session that already exists in the process, and (2) starting 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 sessions, get rid of any registered sessions 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 to the session are alive or not. We set the variable to some value and then we redirect to our I-page in the registration series.

In the script above, we start the session with Session_unset () to erase all 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. We will set some values for the variables and then redirect to the first page of our series of registered pages.

Cache-control and CONDITIONAL CHECK

Cache control and Conditional detection

In the following code snippet, we'll auto-detect if the session was still in.

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-after-the session was destroyed//users attempted to return before session was destroyed  Header ("location:/php/error100.php"); }?>

The "cache-control" directive above is very important. Using the "Must-revalidate" tells the browser, it has to fetch the page from the server again instead of if from I TS Cache. Because it reloads the page from the server, it would re-check the $_session["alive"] variable to the ' if its ' value is ' 1 '. If So, the page can load properly. If not, then we'll redirect the user to another page which 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's the last page in 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 is very important. Use "Must-revalidate" to tell the browser that the Web page should be read from the server side instead of using the browser's cache. Because pages that are reread from the server side will re-examine 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 page that has customized the error message. By placing this script at the beginning of each page of the registered series page, you can capture each user's click on the Back button. 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 the passage into createaccount.php, createaccount1.php, createaccount2.php and createaccount3.php.

Manually EXPIRE the session

To expire a session manually

The "expire" session, or at least a portion of it. In my case, I wanted the sessions 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, we are have to does get rid of it. There are two ways to doing this:

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

createaccount4.php (the page after final submit):

<?php session_start ();  $_session["Alive"] = "0";?> or <?php session_start (); Session_unregister (' alive ');?>

Either way would accomplish the same thing. Now while the "Back" button is pressed, the user won ' t return the previous page and being able to change data and RESUBMI T. Instead, they would be redirected to error100.php (or whatever page you choose) and'll 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 do data changes and repeat submissions. Instead, the user will be redirected to error100.php (or any page you choose) and get a customized error message.

So, the "next time" you are want to stop the user "going back to" change data previously entered, and if you want manual Rol over it. 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 of the page from the server, and the ' Alive ' check is Perfo Rmed. Redirection to a custom page occurs the session variable be not "alive".

So the next time you want to prevent the user from returning to the previous page to change the data entered before, if you want to control it manually, that's the way it is. Remember to use the portal script to set the session variable to the "Alive" state, and use the exit script (after the final commit action in the process) 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 side and implement "Alive" detection. Redirects to a custom page when the session variable is no longer "alive".

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.