会话控制的思想是指能够在网站中根据一个会话跟踪用户。
1 cookie
1.1 什么是cookie
You are logged in as " Member only content goes here You are not logged in. session_start() 初始化session,生命周期的开始。
:如果失效日期不设置,cookie将永远有效,如果不手动将其删除的话)。path和domain域合起来指定URL或与cookie相关的URL。secure关键字的意思是在普通的HTTP链接中不发送cookie。
1.2 通过PHP设置cookie
使用setcookie()函数在PHP中手动设置cookie。函数原型如下:
Set-Cookie: NAME = VALUE; [expires = DATE;] [path = PATH;] [domain = DOMAIN_NAME;] [secure]
:cookie标题头必须,否则就无效(这是cookie的限制,而不是PHP的限制)。
1.3 在会话中使用cookie
1.4 存储会话ID
2 实现简单的会话
2.1 开始一个会话
2.2 注册一个会话变量
2.3 使用会话变量
2.4 注销变量与销毁会话
ID。
3一个简单的会话(实例)
auto-main.php
bool setcookie(string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])
3.2 members_only.php
session_start();
if(isset ($_POST['userid' ]) && isset($_POST['password'])){
$userid = $_POST[ 'userid'];
$password = $_POST[ 'password'];
$db_conn = new mysqli('localhost' , 'root' , '' , 'test' );
if(mysqli_connect_error()){
echo "Connection to database failed:" . mysqli_connect_errno();
exit();
}
$query = "select * from authorized_users" . " where name = '$userid'" . "and password = ' $password'" ;
$result = $db_conn -> query($query);
if($result -> num_rows > 0){
$_SESSION[ 'valid_user'] = $userid;
}
$db_conn -> close();
}
?>
html>
body >
h1 >Home Page h1 >
if( isset($_SESSION[ 'valid_user'])){
echo 'You are logged in as: ' . $_SESSION['valid_user'] . '
' ;
echo 'Log out
';
} else {
if( isset($userid)){ //失败
echo 'Could not log you in.
';
} else {
echo 'You are not logged in.
';
}
}
echo '
' ;
?>
br />
a href ="members-only.php"> Members section a>
body >
html>
Userid:
Password:
3.3 logout.php
session_start();
echo "
Members only
";
if(isset ($_SESSION['valid_user' ])){
echo "
4 session
4.1 影响session数据的PHP函数或事件
session_start();
$old_user = $_SESSION['valid_user'];
unset($old_user);
session_destroy();
?>
html>
body >
h1 >Log out h1 >
if(! empty($old_user)){
echo 'Logged out.
';
} else {
echo 'You were not logged in, and so have not been logged out.
';
}
?>
a href ="auto-main.php"> Back to main page a>
body >
html>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
