会话控制-cookie-session
Cookie简介
Cookie是存储在用户计算机上的小文件,它可以用于存储一些会话数据和用户信息,以便在用户下次访问时能够识别和记住用户信息。
<?php header('Content-type:text/html;charset=utf-8');
$cookiename = 'names';
$value = 'css';
$time = time() + 3600;
setcookie($cookiename, $value, $time); echo "<br>"; var_dump(setcookie($cookiename, $value, $time));
if (isset($_COOKIE)) { echo "<br>"; echo 'cookie存在!'; } echo "<br>"; var_dump($_COOKIE); echo "<br>"; var_dump($_COOKIE['names']);
|
页面的输出结果如下:
unqid
var_dump(uniqid(rand(100, 90000)));
|
Session
<?php
session_start();
$_SESSION['name'] = 'SYW_SEC'; $_SESSION['email'] = '12345678@qq.com'; $_SESSION['url'] = 'linxiao-wangchuang.github.io';
<?php
session_start();
session_unset();
session_destroy();
setcookie('names', '', time() - 2600);
setcookie('names', '', time() - 2600, '/');
|
Session数据会存入到对应的tmp目录当中(默认的)。可以在php.ini里面修改位置,找到session.save_path即可。

小项目:登录-cookie
各个页面:
login的php文件
<!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>登录页面</title> <style> * { margin: 0; padding: 0; }
form { width: 300px; height: 200px; margin: 100px auto; }
width: 100px; height: 30px; margin: 10px 80px; }
p { width: 100%; height: 2em; text-align: center; } </style> </head>
<body style="background-color: orange"> <?php if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') { exit("您已经登录了,请不要重复登录!"); } ?> <form action="login.php" method="post"> <p>姓名:<input type="text" name="username" value=""></p> <p>密码:<input type="password" name="password" value=""></p> <p><input type="submit" name="submit" value="登录" id="submit"></p> </form> <?php header('Content-type:text/html;charset=utf-8'); if (isset($_POST['submit'])) { if (isset($_POST['username']) && isset($_POST['password']) && $_POST['username'] != "" && $_POST['password'] != "") { if ($_POST['username'] === 'admin' && $_POST['password'] === 'password') { $boolname = setcookie('username', $_POST['username'], time() + 3600); setcookie('password', $_POST['password'], time() + 3600); if ($boolname == true) { header('Location:index.php'); } else { header('Location:skip.php?url=index.php&info=cookie设置失败!'); } } else { header('Location:skip.php?url=login.php&info=用户名或密码输入错误!'); } } else { echo "<script>alert('用户名或者密码未输入!')</script>"; } } ?> </body>
</html>
|

index的php文件
<!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>欢迎</title> <style> body { background-color: orange; }
h1 { width: 500px; font-size: 50px; font-weight: 700; margin: 200px auto; } </style> </head>
<body> <?php header('Content-type:text/html;charset=utf-8'); if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') { echo "亲爱的{$_COOKIE['username']}您好!"; echo '<a href="logout.php">注销登录状态</a>'; } else { echo '您的登录状态丢失!点击<a href="login.php">此处进行登录</a>'; } ?> <h1>欢迎来到骇客论坛!</h1>
</body>
</html>
|

skip中转站
<?php if (!isset($_GET['url']) || !isset($_GET['url'])) { exit(); } ?> <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="refresh" content="3;URL=<?php echo $_GET['url'] ?>" /> <title>正在跳转……</title> <style> body { background-color: red; color: aliceblue; font-size: 35px; font-weight: 800; } </style>
</head>
<body> <div> <?php echo $_GET['info']; echo '3s后完成跳转……'; ?> </div> </body>
</html>
|
销毁
<?php header('Content-type:text/html;charset=utf-8'); if (isset($_COOKIE['username']) && $_COOKIE['username'] === 'admin') { if (setcookie('username', $_POST['username'], time() - 3600)) { header("Location:skip.php?url=index.php&info=注销成功!正在跳转到主页面!"); } else { header("Location:skip.php?url=index.php&info=注销失败!没有这个用户!正在跳转到主页面!"); } } else { echo '您的登录状态丢失!点击<a href="login.php">此处进行登录</a>'; }
|
上面的例子可以更改为Session的,具体暂不阐述,看下面代码吧。
小项目-登录-session
login的php文件
<!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>登录页面</title> <style> * { margin: 0; padding: 0; }
form { width: 300px; height: 200px; margin: 100px auto; }
width: 100px; height: 30px; margin: 10px 80px; }
p { width: 100%; height: 2em; text-align: center; } </style> </head>
<body style="background-color: orange"> <?php session_start();
if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin') { exit("您已经登录了,请不要重复登录!"); } ?> <form action="login.php" method="post"> <p>姓名:<input type="text" name="username" value=""></p> <p>密码:<input type="password" name="password" value=""></p> <p><input type="submit" name="submit" value="登录" id="submit"></p> </form> <?php header('Content-type:text/html;charset=utf-8'); if (isset($_POST['submit'])) { if (isset($_POST['username']) && isset($_POST['password']) && $_POST['username'] != "" && $_POST['password'] != "") { if ($_POST['username'] === 'admin' && $_POST['password'] === 'password') { $_SESSION['username'] = $_POST['username']; $_SESSION['password'] = $_POST['password']; header('Location:index.php'); } else { header('Location:skip.php?url=login.php&info=用户名或密码输入错误!'); } } else { echo "<script>alert('用户名或者密码未输入!')</script>"; } } ?> </body>
</html>
|
index的php文件
<!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>欢迎</title> <style> body { background-color: orange; }
h1 { width: 500px; font-size: 50px; font-weight: 700; margin: 200px auto; } </style> </head>
<body> <?php header('Content-type:text/html;charset=utf-8'); session_start(); if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin') { echo "亲爱的{$_SESSION['username']}您好!"; echo '<a href="logout.php">注销登录状态</a>'; } else { echo '您的登录状态丢失!点击<a href="login.php">此处进行登录</a>'; } ?> <h1>欢迎来到骇客论坛!</h1>
</body>
</html>
|
logout.php
<?php session_start(); header('Content-type:text/html;charset=utf-8'); if (isset($_SESSION['username']) && $_SESSION['username'] === 'admin') { session_unset(); session_destroy(); setcookie(session_name(), '', time() - 3600, '/'); header("Location:skip.php?url=index.php&info=注销成功!正在跳转到主页面!"); } else { header("Location:skip.php?url=index.php&info=注销失败!请稍后重试!"); }
|
skip的直接抄上面的skip.php即可。当然,既然时项目,那么所有的php都会放在同一个文件夹下。