用php人工使网页过期
用PHP人工使網(wǎng)頁過期???? detrox [翻譯]?
關(guān)鍵字?? 網(wǎng)頁過期,注冊網(wǎng)頁編寫
出處?? http://www.phpbuilder.net/columns/clark20030702.php3
?
?Manually Expiring Web Pages
人工使網(wǎng)頁過期
作者: Joe Clark
翻譯: 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?
在填寫完成某注冊過程中的一系列網(wǎng)頁后,你不想用戶能夠在最終提交后回到以前的頁面。你應(yīng)該怎樣做才能人工地使這些網(wǎng)頁過期,并且如果有可能則給出一條已定制好的消息呢。
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.
在這一關(guān)里,我不想讓我的session過期,因?yàn)槲倚枰軌蚶^續(xù)運(yùn)轉(zhuǎn)。我使用一個(gè)額外的session變量來跟蹤我的session是否為活動(dòng)的。有三個(gè)主要的組件: (1) 入口腳本 (2) 緩存控制指示符 (3)條件檢測 和 (4) 人工使一部分session過期。
THE 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.
我使用一個(gè)入口腳本來開始我的session. 它用來完成兩件事: (1) 銷毀任何已經(jīng)存在于過程中的session,和(2) 開始一個(gè)新的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.
在上面的腳本中,我們開始session, 用session_unset()清除一切已經(jīng)注冊的session變量,并且用session_destory()來銷毀先前的session。然后,我們開始一個(gè)新的session并且注冊一個(gè)session變量。這個(gè)特定的變量將跟蹤表示這部分session是否為活動(dòng)的。我們將為變量設(shè)置一些值,之后重定向到我們的一系列注冊網(wǎng)頁的第一頁。
CACHE-CONTROL AND CONDITIONAL CHECK
緩存控制和條件檢測
In the following code snippet, we will auto-detect if the session is still in use.
在下面這段簡短的代碼中,我們將自動(dòng)檢測session是否仍然正在使用。
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?? //用戶試圖在session被銷毀前返回?? 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.
上面的緩存控制指示符號(hào)非常重要。使用"must-revalidate"告訴瀏覽器應(yīng)該用從服務(wù)器端讀取網(wǎng)頁而不是使用從瀏覽器的緩存中讀出。因?yàn)閺姆?wù)器端重新讀出的網(wǎng)頁將重新檢查$_SESSION["alive"]變量看看他的值是否為1。如果是的則網(wǎng)頁會(huì)被正常讀取,如果不是那么我們將把用戶重定向到一個(gè)定制了錯(cuò)誤消息的網(wǎng)頁。將這段腳本放到注冊系列頁的每一頁的開始,就可以捕獲每一次用戶對"Back"按鈕的點(diǎn)擊。僅把這段腳本放在一系列注冊網(wǎng)頁的最后一頁是不夠的,因?yàn)橛脩艨赡懿恢挂淮蔚攸c(diǎn)擊"Back"按鈕。我把這段內(nèi)容寫入了createaccount.php, createaccount1.php, createaccount2.php and createaccount3.php.
MANUALLY EXPIRE THE SESSION
人工地使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:
最后一件要做的事就是人工地使session過期,或者至少使一部分過期。在這個(gè)情況下,我想要session保持活動(dòng),因此我不能使用session_unset() 或者 session_destroy().但無論如何,我不想讓用戶回到前一頁去改變什么。記得$_SESSION["alive"]變量嗎?我們要做的就是在最后一次提交后擺脫它。有兩個(gè)方法可以達(dá)到目的
createaccount4.php (the page after the final submit):
?<?php??? session_start();??? $_SESSION["alive"] = "0"; ?>??? or <?php?? session_start();?? session_unregister('alive'); ?>
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.
任一方法都可以完成相同的事。現(xiàn)在,當(dāng)"Back"按鈕被按下,用戶將不能回到前一頁去做數(shù)據(jù)更改和重復(fù)提交。取而代之的是用戶將被重定向到error100.php(或者任何你選用的頁面)并且得到一個(gè)已定制的錯(cuò)誤信息。
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".
因此,下次當(dāng)你想阻止用戶返回前一頁改變以前輸入的數(shù)據(jù)時(shí),如果你想人工的控制它,就是用這個(gè)方法吧。記住使用入口腳本設(shè)置session變量到"alive"狀態(tài),使用出口腳本(在處理過程中最終提交動(dòng)作的后面)設(shè)置session變量到"not alive"狀態(tài)。"Cache-control: must-revalidate" 強(qiáng)迫瀏覽器從服務(wù)器端重新讀取網(wǎng)頁,并且實(shí)施"alive"檢測。當(dāng)session變量不再為"alive"時(shí),重定向到一個(gè)定制頁。
轉(zhuǎn)載于:https://www.cnblogs.com/8user/archive/2008/08/24/1275285.html
總結(jié)
以上是生活随笔為你收集整理的用php人工使网页过期的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 春招平均薪酬最高的职业TOP10
- 下一篇: 这4种分析方法,大牛产品经理都在用