Learning notes: describes how to use PHPSession. PHP is worth learning a lot. here we mainly introduce PHPSession usage. Compared with cookies in PHP Development, session is stored on the server side, which is relatively secure and is not worth learning like PHP. here we mainly introduce PHP Session usage. Compared with cookies in PHP Development, session is stored on the server side, which is relatively secure and does not have storage length restrictions like cookies. next we will briefly introduce PHP Session usage.
The Session is stored on the server as a text file, so the client is not afraid to modify the Session content. In fact, in the Session file on the server side, PHP automatically modifies the session file permissions, only retaining the system read and write permissions, and cannot be modified through ftp, which is much safer. For the open-source community portal in PHPChina, if we want to verify whether a user logs in, we must save the user name and password (probably the md5 encrypted string) in the Cookie ), verify each request page. If the user name and password are stored in the database, a database query is executed every time, causing extra burden on the database.
Because we cannot perform only one verification. Why? Because the information in the client Cookie may be modified. If you store the $ admin variable to indicate whether the user has logged on, $ admin indicates logging on when it is set to true, and false indicates not logging on, after the first verification is passed, $ admin equals true is stored in the Cookie, so no verification is required next time. is this correct? Wrong. if someone spoofs a $ admin variable with the value true, isn't the administrator privilege immediately obtained? Very insecure.
The Session is different. The Session is stored on the server, and remote users cannot modify the content of the session file. Therefore, we can simply store a $ admin variable to determine whether to log on, after the first verification is passed, set $ admin to true, and then judge whether the value is true. if not, transfer it to the login interface, which can reduce a lot of database operations. In addition, it can reduce the security of passing passwords to verify cookies every time (session verification only needs to be passed once, if you do not use the SSL security protocol ). Even if the password is encrypted with md5, it is easily intercepted.
Of course, session has many advantages, such as easy control and user-defined storage (stored in the database ). I will not talk about it here. Does PHP Session need to be set in php. ini? Generally, this is not required because not everyone has modified PHP. ini permission. The Default session storage path is the temporary system folder of the server. we can customize it to be stored in our own folder. I will introduce it later.
This topic describes how to create a session. Very simple, really. Start the session and create a $ admin variable:
- // Start the session
- Session_start ();
- // Declare a variable named admin and assign a null value.
- $ _ Session ["admin"] = null;
- ?>
If you use Seesion or the PHP file needs to call the Session variable, you must start the Session before calling it and use the session_start () function. PHP automatically creates the session file. After executing this program, we can find the session file in the temporary folder of the system. the file name is generally sess_4c83638b3b0dbf65583181c2f89168ec, followed by a 32-bit encoded random string. Open it in the editor and check its content:
Generally, the content is structured as follows:
- // After the form is submitted...
- $Posts=$ _ POST;
- // Clear some blank symbols
- Foreach ($ posts as $Key=>$ Value)
- {
- $ Posts [$ key] = trim ($ value );
- }
- $Password=Md5($ Posts ["password"]);
- $Username= $ Posts ["username"];
- $Query="SELECT 'Username' FROM 'user' WHERE 'password' = '$ password '";
- // Obtain the query result
- $UserInfo= $ DB->GetRow ($ query );
- If (! Empty ($ userInfo ))
- {
- If ($ userInfo ["username"] = $ username)
- {
- // After the verification is passed, start the session
- Session_start ();
- // Register the logon admin variable and assign the value true.
- $ _ Session ["admin"] = true;
- }
- Else
- {
- Die ("incorrect user name and password ");
- }
- }
- Else
- {
- Die ("incorrect user name and password ");
- }
- We start the session on the page that requires user verification to determine whether to log on:
- // Prevent security risks caused by global variables
- $Admin=False;
- // Start the session. This step is required.
- Session_start ();
- // Determine whether to log on
- If (isset ($ _ SESSION ["admin"]) & $ _ session ["admin"] = true)
- {
- Echo "you have successfully logged on ";
- }
- Else
- {
- // Verification failed. set $ _ session ["admin"] to false
- $ _ Session ["admin"] = false;
- Die ("You are not authorized to access ");
- }
- ?>
Is it easy? Consider $ _ session as an array stored on the server. every variable we register is an array key, which is no different from the array used.
Used by the reset Session. Comparing cookies in PHP Development, session is stored on the server side, which is relatively secure and not like...