本文已参与「新人创作礼」活动,一起开启掘金创作之路。
PHP会话管理和控制
php 会话控制 之 PHP中的Cookie
首先介绍一下php中设置cookie的方法。 php中提供了一个函数来让我们设置cookie,这个函数是:
bool setcookie (
string $名字
[, string $值]
[, int $过期时间 = 0]
[, string $路径]
[, string $域名]
[, bool $安全 = false]
[, bool $http只读 = false]
);
参数 描述
值 可选。规定 cookie 的值。 路径 可选。规定 cookie 的服务器路径。 安全 可选。规定是否通过安全的 HTTPS 连接来传输 cookie。 $http安读 可选。如果true,那么js就无法读取改cookie,增加安全性。
一般来说,我们其实用不到上面那么多参数,对于这个函数,我们一般这么用: setcookie(cookie名,cookie值,cookie有效期);
我们来建一个数据库login,其中有表user,有username和password这两个字段。
<?php
//第一次登陆的时候,通过用户输入的信息来确认用户
//null判断是不是非空的
if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) {
$userName = $_POST['username'];
$password = md5($_POST['password']);
//从db获取用户信息
//PS:数据库连接信息改成自己的 分别为主机 数据库用户名 密码
$conn = mysqli_connect('localhost','root','root');
mysqli_select_db($conn,'test');
$sql = "select * from user where `username` = '$userName' ";
$res = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($res);
if ($row['password'] == $password) {
//密码验证通过,设置cookies,把用户名和密码保存在客户端
setcookie('username',$userName,time()+60*60*24*30);//设置时效一个月,一个月后这个cookie失效
setcookie('password',$password,time()+60*60*24*30);
//最后跳转到登录后的欢迎页面
header('Location: welcome.php' . "?username=$userName");
}
}
//再次访问的时候通过cookie来识别用户
if ( ($_COOKIE['username'] != null) && ($_COOKIE['password'] != null) ) {
$userName = $_COOKIE['username'];
$password = $_COOKIE['password'];
//从db获取用户信息
//PS:数据库连接信息改成自己的 分别为主机 数据库用户名 密码
$conn = mysqli_connect('localhost','root','root','test');
$res = mysqli_query($conn,"select * from user where `username` = '$userName' ");
$row = mysqli_fetch_assoc($res);
if ($row['password'] == $password) {
//验证通过后跳转到登录后的欢迎页面
header('Location: welcome.php' . "?username=$userName");
}
}
?>
<html>
<head>
</head>
<body>
<form action="" method="POST">
<div>
用户名:<input type="text" name="username" />
密 码:<input type="text" name="password" />
<input type="submit" value="登录">
</div>
</form>
</body>
</html>
跳转到的welcome.php代码
<?php
$user = $_GET['username'];
?>
<html>
<head>
</head>
<body>
welcome,<?php echo $user;?>
</body>
</html>
这样,当我第一次访问cookie.php的时候,我需要输入用户名和密码,输入完毕后跳转到了welcome.php。然后我关闭浏览器,再次打开cookie.php,这次没有要求我输入用户信息,而是直接跳转到了welcome.php,因为之前我们存的cookie信息被浏览器自动发送到了服务端,服务端做完处理直接跳转到了welcome.php,服务器认识我们了!知道我是之前那个登陆过的用户,这样我们就通过cookie技术让无状态的HTTP协议保持了状态。 照着这个做一遍,我相信你会用cookie了。
我提供的源码
<!DOCTYPE<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
background: url('') center center fixed;
background-size: cover;
padding: 20px;
}
form{
width: 343px;
height: 500px;
margin: 0 auto;
padding: 30px;
border:1px solid rgba(0, 0, 0, 0.2);
border-radius: 5px;
background-color: rgba(0,0,0,0.5);
box-shadow: 0 0 13px 3px rgba(0,0,0,0.5);
overflow: hidden;
}
.border-radius{
width: 200px;
height: 200px;
border-radius: 200px;
background: url('./config/1 (22).jpg') no-repeat center center;
margin: 40px auto;
border:5px solid rgba(255,255,255,0.4)
}
input{
width:276px ;
height: 48px;
border:1px solid rgba(68, 67, 67, 0.4);
border-radius:4px;
display: block;
font-size: 18px;
color:rgb(59, 59, 59);
padding-left: 45px;
padding-right: 20px;
margin-bottom: 20px;
margin-left: 20px;
background: rgba(255,255,255,0.4) no-repeat 16px 16px;
}
input[type=submit]{
cursor: pointer;
}
::-webkit-input-placeholder{
color: white;
}
.btn{
width: 138px;
height: 44px;
border-radius: 4px;
padding:15px 20px;
border-radius: 6px;
color:whitesmoke;
margin-left: 60px;
}
.btn:hover{
border:1px solid black;
box-shadow: 0px 1px 0 black;
}
input:focus{
background-color: rgba(0, 0, 0, 0.2);
box-shadow: 0 0 5px 1px rgba(255,255,255,.0.5);
overflow: hidden;
}
.btn:active{
margin-top: 1px;
text-shadow: 0 -1px #333333;
background: #00B0DC;
color:#fff
}
</style>
</head>
<body>
<form action="" method="POST">
<div class="border-radius"></div>
<input type="text" name="username" placeholder="用户名">
<input type="password" name="password" placeholder="登录密码">
<input type="submit" name="submit" value="登录" class="btn">
</form>
</body>
</html>
<?php
//第一次登陆的时候,通过用户输入的信息来确认用户
//null判断是不是非空的
if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) {
$userName = $_POST['username'];
$password = md5($_POST['password']);
//从db获取用户信息
//PS:数据库连接信息改成自己的 分别为主机 数据库用户名 密码
$conn = mysqli_connect('localhost','cs_odwei_com','3GbX7biY8tBPDtwM');
//cs_odwei_com是我库里面的库
mysqli_select_db($conn,'cs_odwei_com');
//将输入的username和数据库的进行比对,在users表中
$sql = "select * from users where `username` = '$userName' ";
$res = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($res);
if ($row['password'] == $password) {
//密码验证通过,设置cookies,把用户名和密码保存在客户端
//一分钟60秒,一小时60分,一天24小时,一个月30天
setcookie('username',$userName,time()+60*60*24*30);//设置时效一个月,一个月后这个cookie失效
setcookie('password',$password,time()+60*60*24*30);
//最后跳转到登录后的欢迎页面
header('Location: 10.php' . "?username=$userName");
}
}
//再次访问的时候通过cookie来识别用户
if ( ($_COOKIE['username'] != null) && ($_COOKIE['password'] != null) ) {
$userName = $_COOKIE['username'];
$password = $_COOKIE['password'];
//从db获取用户信息
//PS:数据库连接信息改成自己的 分别为主机 数据库用户名 密码
$conn = mysqli_connect('localhost','cs_odwei_com','3GbX7biY8tBPDtwM','cs_odwei_com');
$res = mysqli_query($conn,"select * from users where `username` = '$userName' ");
$row = mysqli_fetch_assoc($res);
if ($row['password'] == $password) {
//验证通过后跳转到登录后的欢迎页面
header('Location: 10.php' . "?username=$userName");
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>star</title>
<script type="text/javascript">
window.onload = function () {
C = Math.cos; // cache Math objects
S = Math.sin;
U = 0;
w = window;
j = document;
d = j.getElementById("c");
c = d.getContext("2d");
W = d.width = w.innerWidth;
H = d.height = w.innerHeight;
c.fillRect(0, 0, W, H); // resize <canvas> and draw black rect (default)
c.globalCompositeOperation = "lighter"; // switch to additive color application
c.lineWidth = 0.2;
c.lineCap = "round";
var bool = 0,
t = 0; // theta
d.onmousemove = function (e) {
if(window.T) {
if(D==9) { D=Math.random()*15; f(1); }
clearTimeout(T);
}
X = e.pageX; // grab mouse pixel coords
Y = e.pageY;
a=0; // previous coord.x
b=0; // previous coord.y
A = X, // original coord.x
B = Y; // original coord.y
R=(e.pageX/W * 999>>0)/999;
r=(e.pageY/H * 999>>0)/999;
U=e.pageX/H * 360 >>0;
D=9;
g = 360 * Math.PI / 180;
T = setInterval(f = function (e) { // start looping spectrum
c.save();
c.globalCompositeOperation = "source-over"; // switch to additive color application
if(e!=1) {
c.fillStyle = "rgba(0,0,0,0.02)";
c.fillRect(0, 0, W, H); // resize <canvas> and draw black rect (default)
}
c.restore();
i = 25; while(i --) {
c.beginPath();
if(D > 450 || bool) { // decrease diameter
if(!bool) { // has hit maximum
bool = 1;
}
if(D < 0.1) { // has hit minimum
bool = 0;
}
t -= g; // decrease theta
D -= 0.1; // decrease size
}
if(!bool) {
t += g; // increase theta
D += 0.1; // increase size
}
q = (R / r - 1) * t; // create hypotrochoid from current mouse position, and setup variables (see: http://en.wikipedia.org/wiki/Hypotrochoid)
x = (R - r) * C(t) + D * C(q) + (A + (X - A) * (i / 25)) + (r - R); // center on xy coords
y = (R - r) * S(t) - D * S(q) + (B + (Y - B) * (i / 25));
if (a) { // draw once two points are set
c.moveTo(a, b);
c.lineTo(x, y)
}
c.strokeStyle = "hsla(" + (U % 360) + ",100%,50%,0.75)"; // draw rainbow hypotrochoid
c.stroke();
a = x; // set previous coord.x
b = y; // set previous coord.y
}
U -= 0.5; // increment hue
A = X; // set original coord.x
B = Y; // set original coord.y
}, 16);
}
j.onkeydown = function(e) { a=b=0; R += 0.05 }
d.onmousemove({pageX:300, pageY:290})
}
</script>
</head>
<body style="margin:0px;padding:0px;width:100%;height:100%;overflow:hidden;">
<canvas id="c"></canvas>
</body>
</html>