search
HomeBackend DevelopmentPHP TutorialUse PHP to manually expire web pages_PHP tutorial
Use PHP to manually expire web pages_PHP tutorialJul 13, 2016 am 10:38 AM
phpArtificialKeywordsregisterusewriteWeb pagetranslateExpired

Use PHP to manually expire web pages detrox [Translation]
Keywords web page expiration, registration web page writing
Source http://www.phpbuilder.net/columns/clark20030702.php3

Manually Expiring Web Pages
Manually Expiring Web Pages

Author: Joe Clark
Translation: detrox


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

After completing a series of pages in a registration process, you don’t want users to be able to return to previous pages after the final submission. What should you do to manually expire these pages and if possible give a customized 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 was 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 of the session.

In this level, I don’t want my session to expire because I need it to continue functioning. I use an additional session variable to track whether my session is active. There are three main components: (1) entry script (2) cache control indicator (3) condition detection and (4) manual expiration of part of the session.

THE ENTRY SCRIPT
Entry script
I use an entry script to start my session. This accomplishes two things: (1) destroys any session already in progress, and (2) starts a new session.

I use an entry script to start my session. It does two things: (1) destroy any sessions that already exist 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 that session with session_destroy(). Then, we start a new session and register a session variable. This particular variable will track whether this portion of the session is alive or not. We set the variable to some value, 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 registered session variables, and use session_destory() to destroy the previous session. Then, we start a new session and register a session variable. This specific variable will keep track of whether this part of the session is active. We'll set some values ​​for the variables and then redirect to the first page of our series of registration pages.

CACHE-CONTROL AND CONDITIONAL CHECK
Cache control and condition detection
In the following code snippet, we will auto-detect if the session is still in use.
In the following short code snippet, we will auto-detect if the session is still in use. , we will automatically detect whether 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 //The user tried to return before the session was destroyed Header("Location:/php/error100.php"); }?>
The "Cache-control" directive above is very important. Using "must-revalidate" tells the browser that it has to fetch the page from the server again instead of loading if from its cache. Because it reloads the page from the server, it will re-check the $_SESSION["alive"] variable to see if its value is "1". If so, the page can load properly. If not, then we'll redirect the user to another page that contains a custom error message. Placing this script at the beginning of every page in the registration series will catch every "Back" button press by the user. It's not enough to place it on 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 cache control indicator above is very important. Use "must-revalidate" to tell the browser that it should read the page from the server instead of reading it from the browser's cache. Because the web page re-read from the server will re-check the $_SESSION["alive"] variable to see if its value is 1. If yes then the web page will be read normally, if not then we will redirect the user to a web page with a customized error message. Place this script at the beginning of each page of the registration series page to capture every user click on the "Back" button. Simply placing this script on the last page of a series of registration pages is not enough because users may click the "Back" button more than once. I wrote this content into createaccount.php, createaccount1.php, createaccount2.php and createaccount3.php.

MANUALLY EXPIRE THE SESSION
Manually expire the SESSION
The last thing to do 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 have to do is get rid of it. There are two ways to do this:

The last thing to do is to manually expire the session, or at least partially expire it. 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 the user to go back to the previous page to change something. Remember the $_SESSION["alive"] variable? All we have to do is get rid of it after the last commit. There are two ways to achieve the goal

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

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

Either method can accomplish the same thing. Now, when the "Back" button is pressed, the user will not be able to go back to the previous page to make data changes and resubmit. 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 want to stop the user from going back to change data previously entered, and if you want manual control over it, use this method. Just remember that 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 performed. Redirection to a custom page occurs when the session variable is not "alive".

So, next time you want to prevent the user from returning to the previous page to change previously entered data, if you want to manually control it, use this method. Remember to use the entry 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 reread the web page from the server and implement "alive" detection. When the session variable is no longer "alive", redirect to a custom page.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735099.htmlTechArticleUsing PHP to manually expire web pages detrox [Translation] Keyword web page expiration, registration web page writing source http://www .phpbuilder.net/columns/clark20030702.php3 Manually Expiring Web Pages Manually...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools