使用PHPLIB進(jìn)行Session的管理和認(rèn)證(轉(zhuǎn)載)
Jun 21, 2016 am 09:13 AMsession
PHPLIB還可以做很多別的事情,例如數(shù)據(jù)庫類。本篇文章只是對PHPLIB的簡單介紹。有很多類和功能都沒有提到。你可以到http://phplib.netuse.de去獲取更多的幫助文檔
---------------------------------------------------------------------
測試環(huán)境:標(biāo)準(zhǔn)環(huán)境
首先要說明一個事實,用Web頁面設(shè)計需要保存客戶當(dāng)前狀態(tài)的程序時極為不便,例如在線Shopping,作為一名程序員,你必須時時面對在各個主頁之間傳遞的狀態(tài)參數(shù)??蛻舻纳矸菡J(rèn)證、他已做出的選擇、他當(dāng)前的狀態(tài)等等,Web主頁并不會替你保存這些狀態(tài)信息,你必須自己小心處理這些參數(shù),這給我們帶來了太多的不便,利用 http://url?var1=x1&var2=x2 來在主頁間傳送參數(shù)實在太危險,尤其是變量中包含用戶注冊信息時很容易被sniff,那么,我們?nèi)绾谓鉀Q這個問題呢?
PHPLIB解決了這個問題,它是在PHP3上的一個擴展,提供了很多類庫,使得程序員能很容易地建立一個交互式Web站點,PHPLIB最基本的功能包括用戶認(rèn)證,Session管理,權(quán)限及數(shù)據(jù)庫的抽象化。
安裝PHPLIB前你必須在你的服務(wù)器上先安裝好php3,PHPLIB可以運行在Cgi方式或apache附加模塊方式。PHP3的版本必須時在3.0.5之上,PHP3早期版本可以在編譯時使用參數(shù) --enable-foce-cgi-redirect來獲得支持,如果不這么做的話,會出現(xiàn)安全問題。PHP3的配置中 track_vars 需要設(shè)置為 enabled。同時需要一個數(shù)據(jù)庫,PHPLIB支持MySQL、Oracle、ODBC、PostgreSQL、Sybase。
第一步,PHPLIB的類庫需要根據(jù)系統(tǒng)進(jìn)行初始化,你可以修改local.inc文件,其中包含著一些基本參數(shù),你可以根據(jù)自己機器的情況來進(jìn)行修改。
我們說明一下PHPLIB的工作原理,每一個使用PHPLIB的頁面首先必須可以找到運行PHPLIB的必須類庫文件,我們可以在php3.ini中設(shè)置auto_prepend變量來支持,PHPLIB分發(fā)包中包含一個prepend.php3文件,將auto_prepend指定為prepend.php3后,各頁面就會自動包含PHPLIB類庫,我們還可以將PHPLIB類庫所在目錄加進(jìn)include變量中,以便可以找到這些文件,當(dāng)然,最苯的辦法就是指定絕對路徑,這可不是個好主意!
第二步,每一個使用PHPLIB的頁面中,你必須使用函數(shù)page_open進(jìn)行初始化。這會告訴PHPLIB,你現(xiàn)在或?qū)頃玫綘顟B(tài)保存。一個典型的page_open例子包含到了認(rèn)證、Session、權(quán)限:
page_open(array( "sess" => "Cms_Session", "auth" => "Cms_Auth", "perm" => "Cms_Perm"));
?>
數(shù)組變量(sess,auth,perm)用來初始化一些狀態(tài)保存對象,注意:必須使用PHPLIB內(nèi)置名(sess,auth,perm),這些內(nèi)置名是你在local.ini中所定義的,page_open函數(shù)必須在頁面內(nèi)容輸出到瀏覽器之前被調(diào)用。(如果你將來不會用到認(rèn)證的話,可以不初始化sess),php3腳本最后應(yīng)以page_close()結(jié)束,這將會將有關(guān)狀態(tài)數(shù)據(jù)寫回到數(shù)據(jù)庫中,如果你忘了的話,將會,哈哈哈。。。
因為PHPLIB使用了Cookies來保存狀態(tài)信息,所以page_open()函數(shù)必須在頁面內(nèi)容輸出到瀏覽器之前被調(diào)用, 這里的頁面內(nèi)容可以是任何HTML信息或者空行,如果你發(fā)現(xiàn)了錯誤"Oops - SetCookie called after header has been sent",這表明在page_open()之前向瀏覽器輸出了些什么,你要特別留意空行,因為非常難找到,典型的錯誤是在標(biāo)記之間輸出了空行,你應(yīng)檢查在local.inc和prepend.php3文件中是否包含了空行,這也是一個非常容易出錯的地方。
PHP使用了一種比基本認(rèn)證方法更為復(fù)雜的架構(gòu),這使得安全有了更好的保證。
舉例來說,對于你想要限制訪問的頁面,會首先使用page_open來調(diào)用"auth" => "auth_class" ,初始化認(rèn)證狀態(tài)對象后,狀態(tài)就會被保存起來,隨后當(dāng)客戶再訪問別的頁面的時候,認(rèn)證系統(tǒng)就會首先檢測用戶的身份是否已經(jīng)經(jīng)過認(rèn)證。
讓我們解釋一下,當(dāng)一個用戶第一次訪問頁面時,他的身份未經(jīng)過認(rèn)證,PHPLIB會調(diào)用一個注冊窗口(并非在WINDOWS中的彈出窗口),你可以自己設(shè)計注冊窗口的樣式,當(dāng)用戶輸入他的用戶名與口令,并按下提交鈕后,身份認(rèn)證工作就開始了,隨后的情況有些復(fù)雜,讓我們慢慢解釋……
這里分兩種情況,如果用戶的瀏覽器不能兼容JavaScript的話,認(rèn)證工作就象詢問嫌疑犯一樣,用戶名與口令被送往服務(wù)器,與存放在那里的數(shù)據(jù)進(jìn)行比較。如果用戶的瀏覽器與JavaScript兼容,這就麻煩一些了,PHPLIB首先會在客戶端的頁面中放入一個用來加密的種子字串,名叫“challenge”,當(dāng)用戶提交該頁面時,用戶的用戶名、口令和challenge字串會使用md5的加密方式進(jìn)行加密,生成一個加密字串,將該加密字串與用戶名提交給服務(wù)器。當(dāng)服務(wù)器收到用戶名和加密后的字串后,他根據(jù)數(shù)據(jù)庫中的用戶名與口令和得到的種子進(jìn)行md5運算,將生成的字串與用戶提交的字串進(jìn)行比較,如果符合的話,說明用戶身份是正確的,就允許用戶進(jìn)行隨后的訪問。這種方法的好處是:用戶不用提交密碼,這使得認(rèn)證比較安全。
Session 管理
其實Session的管理和身份認(rèn)證非常接近,當(dāng)一個用戶的身份認(rèn)證過了后,隨即用戶的session就開始了,如果用戶的瀏覽器支持cookie的話,將會建立一個session的id放入cookie,這個唯一的ID是由PHP3隨機生成,然后又用隨機種子
字串進(jìn)行md5加密過了的,這里的cookie應(yīng)該叫做session cookie,因為這個cookie是不會寫到用戶硬盤里去的,當(dāng)一個session進(jìn)行完的時候,該cookie也被完結(jié)了。如果用戶瀏覽器不支持cookie的話,那么 該session的id將會放入url鏈中,因為是加密過的,所以竊取了也沒用。session id存放著用戶的有關(guān)信息,如用戶已認(rèn)證、認(rèn)證到期時間、用戶權(quán)限,和其他一些你可能需要的信息,方便我們?nèi)∮谩?
Session其實就是用戶一次會話的過程。Session的管理并不是僅僅用來跟蹤用戶的注冊,實際上,它還可以脫離認(rèn)證來使用,你可以用它來存儲任何你想要存貯的信息,這些信息可以在用戶隨后訪問的頁面中派上用場,當(dāng)然前提是那些頁面要使用PHPLIB。方法很簡單,注冊一個變量后即可在隨后的頁面中使用它,直至session結(jié)束。方法:
register( "variable_name"); ?>
注意,這里的variable_name不是變量值,而是變量名,可以先指定變量名,隨后再賦值。你在某個頁面中可以改變變量的值,隨后的頁面訪問該變量時會得到改變后的值。變量的類型是多樣的,可以是一個字串,一個數(shù)字,一個數(shù)組,甚至一個對象。舉例來說明:
$sess->register( "first");
if (check($firstname)) {
$first = $firstname;
}
?>
注意:這里有一點很重要。你可以先注冊一個變量隨后再對它賦值,這樣非常有效,我們大可以在腳本的任何地方定義變量而不賦值,而在隨后的頁面中再賦值,這樣方便集中定義變量。大家可能注意到了,上面的例子中我們沒有簡單的對變量賦值,處于安全的考慮,你不應(yīng)該輕率地將表單數(shù)據(jù)放入變量。上例中,我們對變量進(jìn)行了檢查,然后才對變量賦值。這是一個很好的習(xí)慣。大家應(yīng)該注意。
注冊完一個變量,當(dāng)頁面最后調(diào)用page_close()函數(shù)后,各個session變量會被寫回到數(shù)據(jù)庫中,如果你忘記調(diào)用page_close()函數(shù)的話,變量就不會被寫回數(shù)據(jù)庫,將出現(xiàn)不可予知的后果。當(dāng)變量被使用完畢后,你不在需要用到了,可以調(diào)用以下函數(shù)將變量刪除:
$sess->unregister( "variable_name");
?>
PHPLIB 7.0中,使用了一種存儲結(jié)構(gòu),它允許你存儲session數(shù)據(jù)到數(shù)據(jù)庫中、共享內(nèi)存中或者LDAP中。PHPLIB使用了數(shù)據(jù)庫類,這使得你有了更多的選擇。
權(quán)限管理
權(quán)限是和認(rèn)證分不開的。當(dāng)一個用戶的身份被確認(rèn)以后,你可以接著來確定他的級別及權(quán)限。當(dāng)然,你必須先調(diào)用page_open來初始化"perm"對象。檢查用戶權(quán)限的命令如下:
$perm->check( "permission_level");
?>
這條命令會檢查用戶是否符合你指定的級別,指定的級別應(yīng)在local.inc文件中已經(jīng)定義好,你可以自己定義各種級別。如果用戶被檢查出不符合級別。則perm_invalid()函數(shù)自動被調(diào)用。你可以建立自己的perm_invalid函數(shù)。
以下是PHPLIB中檢查權(quán)限的另一種方法:
$perm->have_perm( "permission_level");
?>
have_perm與check函數(shù)不同,它只返回true或false,但并不退出腳本,這樣我們可以更好的控制程序流程。
if ($perm->have_perm( "guest"))
{ //do something; }
elseif ($perm->have_perm( "admin"))
{ //do something else; }
else { //yet something else; }
?>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The problem was found in the springboot project production session-out timeout. The problem is described below: In the test environment, the session-out was configured by changing the application.yaml. After setting different times to verify that the session-out configuration took effect, the expiration time was directly set to 8 hours for release. Arrived in production environment. However, I received feedback from customers at noon that the project expiration time was set to be short. If no operation is performed for half an hour, the session will expire and require repeated logins. Solve the problem of handling the development environment: the springboot project has built-in Tomcat, so the session-out configured in application.yaml in the project is effective. Production environment: Production environment release is

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to the problem that the php session disappears after refreshing: 1. Open the session through "session_start();"; 2. Write all public configurations in a php file; 3. The variable name cannot be the same as the array subscript; 4. In Just check the storage path of the session data in phpinfo and check whether the sessio in the file directory is saved successfully.

Problem: Today, we encountered a setting timeout problem in our project, and changes to SpringBoot2’s application.properties never took effect. Solution: The server.* properties are used to control the embedded container used by SpringBoot. SpringBoot will create an instance of the servlet container using one of the ServletWebServerFactory instances. These classes use server.* properties to configure the controlled servlet container (tomcat, jetty, etc.). When the application is deployed as a war file to a Tomcat instance, the server.* properties do not apply. They do not apply,

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

The default expiration time of session PHP is 1440 seconds, which is 24 minutes, which means that if the client does not refresh for more than 24 minutes, the current session will expire; if the user closes the browser, the session will end and the Session will no longer exist.

1. Implementing SMS login based on session 1.1 SMS login flow chart 1.2 Implementing sending SMS verification code Front-end request description: Description of request method POST request path /user/code request parameter phone (phone number) return value No back-end interface implementation: @Slf4j@ ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1. Verify mobile phone number if

JavaScriptCookies Using JavaScript cookies is the most effective way to remember and track preferences, purchases, commissions and other information. Information needed for a better visitor experience or website statistics. PHPCookieCookies are text files that are stored on client computers and retained for tracking purposes. PHP transparently supports HTTP cookies. How do JavaScript cookies work? Your server sends some data to your visitor's browser in the form of a cookie. Browsers can accept cookies. If present, it will be stored on the visitor's hard drive as a plain text record. Now, when a visitor reaches another page on the site
