When you run an app, you open it, make changes, and then close it. It's a lot like a session. The computer knows who you are. It knows when you start the application and when it terminates it. But on the Internet, there's a problem: the server doesn't know who you are and what you do, and that's because HTTP addresses don't maintain state.
PHP session solves this problem by storing user information on the server for subsequent use (such as user name, purchased items, etc.). However, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database.
Related topic recommendations: php session (including pictures, texts, videos, cases)
Copy the manual, then try each one and write it out for your own reference. Who told us to just learn it? Woolen cloth. Session has about 12 functions:
session_start: initial session.
session_destroy: End session.
session_unset: Release session memory.
session_name: access the current session name.
session_module_name: access the current session module.
session_save_path: access the current session path.
session_id: access the current session code.
session_register: Register new variables.
session_unregister: Delete registered variables.
session_is_registered: Check whether the variable is registered.
session_decode: Session data decoding.
session_encode: Session data encoding.
There is also a global variable: $_SESSION
Before you store user information in the PHP session, you must first start the session.
Note: session_start()
The function must be placed before the label:
<?php session_start(); ?> <html> <body> </body> </html>
Storage Session variables
<?php session_start(); // store session data $_SESSION['views']=1; ?> <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['views']; ?> </body> </html> [html] 终结 Session unset() 函数用于释放指定的 session 变量: [code] <?php unset($_SESSION['views']); ?>
You can also completely terminate the session through the session_destroy() function:
<?php session_destroy(); ?>
Example:
<?php session_start(); switch ( $_GET['action'] ){ case "loginif"; //登陆验证,假定session储存的秘密应该等于123才为正确 if ($_SESSION['pass']=="123"){echo "密码正确 您可以执行注销";}else{echo "密码错误,您可以重新登陆";} break; case "logout"; //注销登陆 session_unset(); session_destroy(); echo "注销成功!可以判断一下密码是否正确来看看是不是成功注销"; break; case "login"; //写入session以供验证, $pass="123";//密码 $_SESSION['pass']=$pass; echo "写入登陆密码了 去判断密码成功与否吧。"; break; } ?> <p>假定本页名为temp.php </p> <p><a href="temp.php?action=login">用户进行登陆post,程序处理写入session</a></p> <p><a href="temp.php?action=loginif">判断用户密码是否正确</a></p> <p><a href="temp.php?action=logout">登陆成功的用户注销登陆</a></p>
I summarized the session in php usage.
(1) Start session
Before each use of session, add this sentence: "session_start();". As the name suggests, the function of this function is to start using the session.
(2) Register session
First, you must create a global (note, it must be defined as global, otherwise it cannot be used on other pages) array, such as $login, where $login['name']="Victor", $login[ 'pwd']="111111", and then call the function "session_register(login);", the session is successfully registered.
(3) Using variables in the session
Similar to registering a session, you must first create a global array, and then it is the same as using a normal array.
(4) Determine whether the session is registered
It is very simple, just use "if (session_is_registered(login))" to judge.
(5) Uninstalling the session
is also very simple, just "session_unregister(login);".
Note: Be sure to do (1) before doing (2) (3) (4) (5).例 The following example is given:
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:html;toolbar:false"><html>
<head>
<title>测试</title>
</head>
<body>
<FORM METHOD=POST ACTION="login.php">
用户名:<INPUT TYPE="text" NAME="name"><br/>
密码:<INPUT TYPE="password" name="pwd"><br/>
<INPUT TYPE="submit" value="提交">
</FORM>
</body>
</html></pre><div class="contentsignin">Copy after login</div></div>e
rreeee
rreeeee
<?php
global $login;
if ($_POST['name']!="Victor" || $_POST['pwd']!="111111")
{
echo "登陆失败";
echo "请<a href=index.htm>返回</a>";
exit;
}
$login = array('name'=>$_POST['name'],
'pwd'=>$_POST['pwd']);
session_start();
session_register(login);
echo "<a href=info.php>查看信息</a><br/>";
echo "<a href=logout.php>退出登陆</a><br/>";
?>