PHP4中的SESSION管理
Jun 21, 2016 am 09:14 AMsession
PHP4的新特性中最引人注目就是對SESSION的管理功能.當別人訪問你的站點時,你可以把變量保存到SESSION對象中,從而實現(xiàn)許多新的功能.
什么是SESSION
如果你還不太清楚,先我來解釋一下什么是SESSION.一個SESSION在別人訪問你的網(wǎng)站是開始,在他離開網(wǎng)站是結束.當然你也可以在程序里終止它.基本上,COOKIE與瀏覽器聯(lián)系在一起的,一些服務器上的資源用來保存SESSION的變量.默認情況下,PHP4用文件來保存這些變量.當然,從理論上來講,也可以用數(shù)據(jù)庫或共享的內(nèi)存來保存.
所有使用PHP4的SESSION特性的網(wǎng)頁都必須調(diào)用session_start()函數(shù),讓PHP4引擎裝入與SESSION相關的信息 到內(nèi)存中.session_start函數(shù)會嘗試從COOKIE或當前HTTP的請求的參數(shù)中尋找SESSION的ID.如果沒找到,就會建立一個新的SESSION對象.
什么是SESSION變量
SESSION的變量其實就是一個普通的全局變量.當以SESSION變量的方式注冊(聲明)時,所有使用PHP4的SESSION功能的網(wǎng)頁都可以訪問它的值.要把一個變量注冊為SESSION變量,只要先給這個變量賦值,然后調(diào)用session_register("變量名").在后續(xù)的使用SESSION功能(通過session_start)的網(wǎng)頁中,"變量名"這個變量在無須重新注冊為SESSION變量,它就象一個普通的變量,而它的值就是前面賦給它的.在程序中改變這個變量的值,它會自動重新注冊和保存,且在它后續(xù)的網(wǎng)頁中可以正常使用.
那么,它能做什么呢?
問得好!有很多辦法可以使用SESSION管理和SESSION變量.這里,我舉一個例子來說明.假如你正在建立一個虛擬社區(qū)的站點,你可能要跟蹤當前經(jīng)過論證的用戶名以及他有多少新的消息等信息.為了降低數(shù)據(jù)庫的負載,你要用其他辦法來保存這些信息.有兩種辦法:
使用三個COOKIE
authenticatied_user - 當前經(jīng)過論證的用戶名
num_message - 他有多少新的消息
expire_time - COOKIE的過期時限
使用SESSION,注冊三個SESSION變量
第一個方法有很大的安全隱患,別人可以偽造COOKIE,然后用另一個帳號去訪問系統(tǒng).再說,所有的信息都通過HEADER()函數(shù)實現(xiàn),比較麻煩.另外,當用戶的瀏覽器拒絕接受其中的一個COOKIE是,整個數(shù)據(jù)就不完整了.
用SESSION,只要一個COOKIE,就能保存所有的信息.安全性也好一點.
不足之處
使用任何服務器端的腳本語言,SESSION都可以提供一個非常自由,靈活和強大的功能.但在PHP4中的SESSION有一些限制: -不能把對象(Objects)本身保存在SESSION里 -保存在SESSION里的數(shù)據(jù)的效率不是很高,因為PHP4用文件來保存SESSION的信息.

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)

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

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 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

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

Solutions for document.cookie not being obtained: 1. Browser privacy settings; 2. Same-origin policy; 3. HTTPOnly Cookie; 4. JavaScript code error; 5. Cookie does not exist or expires; 6. Cross-domain issues; 7. Viewer mode; 8. Server problems; 9. JavaScript execution timing; 10. Check console log, etc.

In our daily use of computers and the Internet, we are often exposed to cookies. A cookie is a small text file that saves records of our visits to the website, preferences and other information. This information may be used by the website to better serve us. But sometimes, we need to find cookie information to find the content we want. So how do we find cookies in the browser? First, we need to understand where the cookie exists. in browser

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.
