Home > Article > Backend Development > Study notes: Talk about how to use PHP Session_PHP Tutorial
There is a lot worth learning about PHP. Here we mainly introduce the use of PHP Session. Compared with Cookie in PHP development, session is a session stored on the server side, which is relatively safe and does not have storage length limit like Cookie. Below we will briefly introduce the use of PHP Session.
Because we cannot do verification only once. Why? Because the information in the client cookie may be modified. If you store the $admin variable to indicate whether the user is logged in, when $admin is true, it means logged in, and when it is false, it means not logged in. After passing the verification for the first time, store $admin equal to true in the cookie, and there will be no need to verify next time. Okay, is this right? Wrong. If someone forges a $admin variable with a value of true, doesn't that mean he or she will immediately gain administrative rights? It's very unsafe.
The Session is different. The Session is stored on the server side. Remote users cannot modify the contents of the session file. Therefore, we can simply store a $admin variable to determine whether to log in. Set $admin after the first verification is passed. If the value is true, then determine whether the value is true. If not, go to the login interface, which can reduce a lot of database operations. And it can reduce the insecurity of passing the password every time to verify the cookie (session verification only needs to be passed once, if you do not use the SSL security protocol). Even if the password is md5 encrypted, it can be easily intercepted.
Of course, there are many advantages to using session, such as easy control and user-defined storage (stored in the database). I won’t say much more here. Does PHP Session need to be set in php.ini? Generally not required, because not everyone has the permission to modify PHP.ini. The default storage path of session is the system temporary folder of the server. We can customize it to be stored in In your own folder, I will introduce this later.
Start introducing how to create a session. Very simple, really. Start the session and create a $admin variable:
<ol class="dp-xml"> <li class="alt"><span><span>// 启动 session </span></span></li> <li class=""><span>session_start(); </span></li> <li class="alt"><span>// 声明一个名为 admin 的变量,并赋空值。 </span></li> <li class=""><span>$_session["admin"] = null; </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
If you use Seesion, or the PHP file wants to call the Session variable, you must start it before calling the Session, use the session_start() function . You don’t need to set anything else, PHP automatically creates the session file. After executing this program, we can find the session file in the system temporary folder. Generally, the file name is in the form: sess_4c83638b3b0dbf65583181c2f89168ec, followed by a 32-bit encoded random string. Open it with an editor and take a look at its content:
Generally the content is structured like this:
<ol class="dp-xml"> <li class="alt"><span><span>// 表单提交后... </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">posts</font></span><span> = $_POST; </span> </li> <li class="alt"><span>// 清除一些空白符号 </span></li> <li class=""> <span>foreach ($posts as $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value) </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>$posts[$key] = trim($value); </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">password</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">md5</font></span><span>($posts["password"]); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">username</font></span><span> = $posts["username"]; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">query</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"SELECT `username` FROM `user` WHERE `password` = '$password'"</font></span><span>; </span> </li> <li class="alt"><span>// 取得查询结果 </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">userInfo</font></span><span> = $DB-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getRow($query); </span> </li> <li class="alt"><span>if (!empty($userInfo)) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>if ($userInfo["username"] == $username) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>// 当验证通过后,启动 session </span></li> <li class=""><span>session_start(); </span></li> <li class="alt"><span>// 注册登陆成功的 admin 变量,并赋值 true </span></li> <li class=""><span>$_session["admin"] = true; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>else </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>die("用户名密码错误"); </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>else </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>die("用户名密码错误"); </span></li> <li class=""><span>} </span></li> <li class="alt"><span>我们在需要用户验证的页面启动 session,判断是否登陆: </span></li> <li class=""><span>// 防止全局变量造成安全隐患 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">admin</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">false</font></span><span>; </span> </li> <li class=""><span>// 启动会话,这步必不可少 </span></li> <li class="alt"><span>session_start(); </span></li> <li class=""><span>// 判断是否登陆 </span></li> <li class="alt"><span>if (isset($_SESSION["admin"]) && $_session["admin"] === true) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>echo "您已经成功登陆"; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>else </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>// 验证失败,将 $_session["admin"] 置为 false </span></li> <li class=""><span>$_session["admin"] = false; </span></li> <li class="alt"><span>die("您无权访问"); </span></li> <li class=""><span>} </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
Is it very simple? Think of $_session as It can be stored in an array on the server side. Each variable we register is a key of the array, which is no different from using an array.