国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊(cè) 使用無(wú)限生命期Session的方法(轉(zhuǎn))

使用無(wú)限生命期Session的方法(轉(zhuǎn))

Jun 21, 2016 am 09:12 AM
cookie php session

session

使用無(wú)限生命期Session的方法

本文不敢說(shuō)非常好,但是筆者相信大多數(shù)PHPer都曾經(jīng)想過(guò)這些問(wèn)題。

使用無(wú)限生命期Session的方法
在PHP4.0中加入了對(duì)Session的支持,方便了我們很多程序,比如購(gòu)物車(chē)等等!
在很多論壇中,Session也用于處理用戶的登陸,記錄下用戶名和密碼,使得用戶不必每次都輸入自己的用戶名和密碼!但是一般的Session的生命期有限,如果用戶關(guān)閉了瀏覽器,就不能保存Session的變量了!那么怎么樣可以實(shí)現(xiàn)Session的永久生命期呢?
大家知道,Session儲(chǔ)存在服務(wù)器端,根據(jù)客戶端提供的SessionID來(lái)得到這個(gè)用戶的文件,然后讀取文件,取得變量的值,SessionID可以使用客戶端的Cookie或者Http1.1協(xié)議的Query_String(就是訪問(wèn)的URL的“?”后面的部分)來(lái)傳送給服務(wù)器,然后服務(wù)器讀取Session的目錄……

要實(shí)現(xiàn)Session的永久生命期,首先需要了解一下php.ini關(guān)于Session的相關(guān)設(shè)置(打開(kāi)php.ini文件,在“[Session]”部分):
1、session.use_cookies:默認(rèn)的值是“1”,代表SessionID使用Cookie來(lái)傳遞,反之就是使用Query_String來(lái)傳遞;
2、session.name:這個(gè)就是SessionID儲(chǔ)存的變量名稱,可能是Cookie,也可能是Query_String來(lái)傳遞,默認(rèn)值是“PHPSESSID”;
3、session.cookie_lifetime:這個(gè)代表SessionID在客戶端Cookie儲(chǔ)存的時(shí)間,默認(rèn)是0,代表瀏覽器一關(guān)閉SessionID就作廢……就是因?yàn)檫@個(gè)所以Session不能永久使用!
4、session.gc_maxlifetime:這個(gè)是Session數(shù)據(jù)在服務(wù)器端儲(chǔ)存的時(shí)間,如果超過(guò)這個(gè)時(shí)間,那么Session數(shù)據(jù)就自動(dòng)刪除!
還有很多的設(shè)置,不過(guò)和本文相關(guān)的就是這些了,下面開(kāi)始講使用永久Session的原理和步驟。

前面說(shuō)過(guò),服務(wù)器通過(guò)SessionID來(lái)讀取Session的數(shù)據(jù),但是一般瀏覽器傳送的SessionID在瀏覽器關(guān)閉后就沒(méi)有了,那么我們只需要人為的設(shè)置SessionID并且保存下來(lái),不就可以……
如果你擁有服務(wù)器的操作權(quán)限,那么設(shè)置這個(gè)非常非常的簡(jiǎn)單,只是需要進(jìn)行如下的步驟:
1、把“session.use_cookies”設(shè)置為1,打開(kāi)Cookie儲(chǔ)存SessionID,不過(guò)默認(rèn)就是1,一般不用修改;
2、把“session.cookie_lifetime”改為正無(wú)窮(當(dāng)然沒(méi)有正無(wú)窮的參數(shù),不過(guò)999999999和正無(wú)窮也沒(méi)有什么區(qū)別);
3、把“session.gc_maxlifetime”設(shè)置為和“session.cookie_lifetime”一樣的時(shí)間;
設(shè)置完畢后,打開(kāi)編輯器,輸入如下的代碼:
------------------------------------------------------------------------------------

session_start();
session_register('count');
$count++;
echo $count;
?>
------------------------------------------------------------------------------------
然后保存為“session_check.php”,用瀏覽器打開(kāi)“session_check.php”,看看顯示的是不是“1”,再關(guān)閉瀏覽器,然后再打開(kāi)瀏覽器訪問(wèn)“session_check.php”,如果顯示“2”,那么恭喜了,你已經(jīng)成功;如果失敗的話,請(qǐng)檢查你前面的設(shè)置。

但是如果你沒(méi)有服務(wù)器的操作權(quán)限,那就比較麻煩了,你需要通過(guò)PHP程序改寫(xiě)SessionID來(lái)實(shí)現(xiàn)永久的Session數(shù)據(jù)保存。查查php.net的函數(shù)手冊(cè),可以見(jiàn)到有“session_id”這個(gè)函數(shù):如果沒(méi)有設(shè)置參數(shù),那么將返回當(dāng)前的SessionID,如果設(shè)置了參數(shù),就會(huì)將當(dāng)前的SessionID設(shè)置為給出的值……
只要利用永久性的Cookie加上“session_id”函數(shù),就可以實(shí)現(xiàn)永久Session數(shù)據(jù)保存了!
但是為了方便,我們需要知道服務(wù)器設(shè)置的“session.name”,但是一般用戶都沒(méi)有權(quán)限查看服務(wù)器的php.ini設(shè)置,不過(guò)PHP提供了一個(gè)非常好的函數(shù)“phpinfo”,利用這個(gè)可以查看幾乎所有的PHP信息!
------------------------------------------------------------------------------------

PHP相關(guān)信息顯示

------------------------------------------------------------------------------------
打開(kāi)編輯器,輸入上面的代碼,然后在瀏覽器中運(yùn)行這個(gè)程序,會(huì)見(jiàn)到PHP的相關(guān)信息(如圖1所示)。其中有一項(xiàng)“session.name”的參數(shù)(圖中已經(jīng)標(biāo)出),這個(gè)就是我們需要的服務(wù)器“session.name”,一般是“PHPSESSID”。
記下了SessionID的名稱后,我們就可以實(shí)現(xiàn)永久的Session數(shù)據(jù)儲(chǔ)存了!
打開(kāi)編輯器,輸入下面的代碼:
------------------------------------------------------------------------------------

session_start(); // 啟動(dòng)Session
session_register('count'); // 注冊(cè)Session變量Count
if(isset($PHPSESSID)) {
session_id($PHPSESSID);
} // 如果設(shè)置了$PHPSESSID,就將SessionID賦值為$PHPSESSID,否則生成SessionID
$PHPSESSID = session_id(); // 取得當(dāng)前的SessionID
$count++; // 變量count加1
setcookie('PHPSESSID', $PHPSESSID, time()+3156000); // 儲(chǔ)存SessionID到Cookie中
echo $count; // 顯示Session變量count的值
?>
------------------------------------------------------------------------------------

保存之后,利用和剛才擁有服務(wù)器權(quán)限時(shí)候的檢測(cè)一樣的方法,檢測(cè)是否成功的保存了SessionID。


后記:
其實(shí)真正的永久儲(chǔ)存是不可能的,因?yàn)镃ookie的保存時(shí)間有限,而服務(wù)器的空間也有限……但是對(duì)于一些需要保存時(shí)間比較長(zhǎng)的站點(diǎn),以上方法就已經(jīng)足夠了!關(guān)于Session的其他應(yīng)用,可以參見(jiàn)zphp.com的文章。
最后,筆者的調(diào)試環(huán)境:Windows98DigExt(SE)+Apache+PHP 4.04。?



Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Why We Comment: A PHP Guide Why We Comment: A PHP Guide Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

How to Install PHP on Windows How to Install PHP on Windows Jul 15, 2025 am 02:46 AM

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

PHP Syntax: The Basics PHP Syntax: The Basics Jul 15, 2025 am 02:46 AM

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

PHP 8 Installation Guide PHP 8 Installation Guide Jul 16, 2025 am 03:41 AM

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

python if else example python if else example Jul 15, 2025 am 02:55 AM

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

Your First PHP Script: A Practical Introduction Your First PHP Script: A Practical Introduction Jul 16, 2025 am 03:42 AM

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

What is PHP and What is it Used For? What is PHP and What is it Used For? Jul 16, 2025 am 03:45 AM

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

how to handle undefined index in PHP how to handle undefined index in PHP Jul 15, 2025 am 02:08 AM

The "undefinedindex" error occurs because a key that does not exist in the array is accessed. Solutions include: 1. Use isset() to check whether the key exists, which is suitable for processing user input; 2. Use array_key_exists() to determine whether the key is set, and it can be recognized even if the value is null; 3. Use the empty merge operator?? to set the default value to avoid directly accessing undefined keys; in addition, you need to pay attention to common problems such as the spelling of form field names, the database result is empty, the array unpacking is not verified, the child keys are not checked in foreach, and the session_start() is not called.

See all articles