<?php
$title='登入';
include 'inc/header.html';
?>
<style>
form{width: 600px;font-size: 14px;margin: 0 auto;}
fieldset{margin: 15px auto;text-align: left;width: 600px;border-radius: 5px;}
legend{border: 1px solid black;padding: 3px 12px;font-weight: bold;clear: both;}
input{margin-right: 10px; width: 150px; height: 20px;line-height: 20px;border: 1px solid black;}
label{width: 140px;float: left;text-align:right;line-height: 20px;}
p.enter{text-align: center;}
p.enter input{width: 60px;line-height: 25px;height: 25px;position: relative;left: -80px;}
</style>
<form action="" method="post">
<fieldset >
<legend>用户登入</legend>
<p><label for="user">账户名:</label>
<input type="text" name="user" id="user" value="" placeholder="账号/手机/邮箱"> </p>
<p><label for="password">密码:</label>
<input type="password" name="password" id="password" value="" placeholder="密码区分大小写"> </p>
<p class="enter"><input type="submit" name="submit" value="提交">
</fieldset>
</form>
<?php
include 'inc/footer.html';
?>
<?php
if ($_SERVER['REQUEST_METHOD']=='POST')
{
$user=$_POST['user'];
$password=$_POST['password'];
include 'inc/function.php';
list($info,$data)= check_user($db,$user,$password);
if($info)
{
setcookie('id',$data['id'],time()+3600);
setcookie('name',$data['name'],time()+3600);
redirect($page='loggedin.php');
}
else {
$errors=$data;
}
}
if (isset($errors) && !empty($errors)) {
$errors_msg = '<p style="color:red;text-align: center">';
foreach ($errors as $msg) {
$errors_msg .= $msg .'<br>';
}
echo $errors_msg .'</p>';
}
?>点击 "运行实例" 按钮查看在线实例


setcookie(name,value,expire,path,domain,secure)
name 必需。规定 cookie 的名称。
value 必需。规定 cookie 的值。
expire 可选。规定 cookie 的有效期。
path 可选。规定 cookie 的服务器路径。
domain 可选。规定 cookie 的域名。
secure 可选。规定是否通过安全的 HTTPS 连接来传输 cookie。
超全局变量$_COOKIE['name'] 访问cookie值
删除cookie
setcookie('username','admin',time()-3600);
设置一个过去的时间
(登录验证过程)
判断用户是否登入 判断cookie 中是否存在变量,有就是登录,没有就没有登录
isset($_cookie['name']) &&(basename(_FILE_)!='logout.php'); (为什么要判断是不是在退出页?)
输入的表单信息提交和刷新不会丢失 粘性表单 <input> 中value值
redirect_user(loggedin.php)
用session 前先开启会话 session_start();
设置session $_SESSION['名字']=值;
session_destroy();删除服务器上的session
setcookie('PHPSESSID','',time()-3600); 删除浏览器上的COOKIE
session 默认时间是1440秒
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号