Home > php教程 > PHP开发 > body text

Detailed explanation of Session usage in ThinkPHP

高洛峰
Release: 2016-12-22 10:25:49
Original
1491 people have browsed it

The examples in this article describe the usage of Session in ThinkPHP. Share it with everyone for your reference. The details are as follows:

The Session class is encapsulated in ThinkPHP, and users can use it directly. Commonly used methods are:

Session::set(name, value): Register session.

Session::is_set(name): Check whether the value of Session is set.

Session::get(name): Read session.

Session::clear(): Clear the Session.

Session::destroy(): Destroy session.

ThinkPHP opens the session by default, so there is no need to use the session_start() function to open the session before using the Session class.

Use the session instance

Register the session by form submission below, and register the session on the other two pages respectively. Use Session::get method to read the session value in template mode and operation.

Register session

User module sessionTest operation to register session Example:

class UserAction extends Action{  
public function session(){  
if(!emptyempty($_POST['username'])){  
Session::set('username',$_POST['username']);  
}  
$this->display();  
}  
}
Copy after login

sessionTest.html Template (fragment):

<p>  
<present name="_SESSION[&#39;username&#39;]">{$_SESSION[&#39;username&#39;]} 你好!  
<a href="__APP__/">首页</a>  
<a href="__URL__/user">本模块其他页面</a>  
<a href="__URL__/logout">注销</a><else />请输入您的用户名:  
</p>  
<form action="__SELF__" method="post">  
<p><input type="text" name="username" /></p>  
<p><input type="submit" value=" 提交 " /></p>  
</form>  
</present>
Copy after login

When filling in the user name ( Such as testuser), after clicking the submit button, submit it to the sessionTest method (i.e. the current page) to process and register the session value. Use the template tag present to control the output logic. It detects that the $_SESSION['username'] variable has been registered and outputs:


testuser Hello! Home page Log out

Otherwise the form will be output.

Detect whether the session is registered

Detect in the template

In the template, you can directly use tags such as present or notempty or even switch to determine whether the session variable is registered to determine the corresponding session value to be output (directly output the session as an output array unit) Variable value) or display other page elements. For details, please refer to the template tag part of this tutorial and the above example.

Detection in operation

In the operation, you can use the Session::is_set method to check whether the value of the Session is set, such as User The operation of module user is as follows:

public function user(){  
// 由于直接在操作里输出,为避免乱码  
header("Content-Type:text/html; charset=utf-8");  
if(Session::is_set(&#39;username&#39;)){  
echo Session::get(&#39;username&#39;).&#39; 你好&#39;;  
}else{  
echo &#39;session 未注册&#39;;  
}  
}
Copy after login

Other module pages

In other pages (such as Index/index), judge and read the code snippet of session:

<present name="_SESSION[&#39;username&#39;]">{$_SESSION[&#39;username&#39;]} 你好!<else />未登录</present>
Copy after login
session is invalid (cannot be transferred)

Invalid session (cannot be passed) may occur in ThinkPHP passed to other pages), the possible reasons are as follows:

The first letter of the Session class is not capitalized, such as: session::set.

The page has information output, such as the entry file has blank lines, etc.

On the server ( The session storage path (session.save_path) of Linux/Unix has incorrect permissions, resulting in the inability to store session information correctly.

Suggestions for scope issues:

ThinkPHP’s Session class is just a simple wrapper for the session, which can actually be used during operation Directly use PHP's native session function, which is also the official recommendation.

I hope this article will be helpful to everyone's ThinkPHP framework programming.

For more detailed explanations of Session usage in ThinkPHP, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template