总结:
登录验证逻辑:
首页登录按钮状态:如果已经登录,显示已登录,如果没有登录,显示未登录。
如果没有登录,则点击登录按钮,进入登录页面,填写信息进行提交
登录成功后进入到已登录界面,此界面会显示登陆者的信息,并有退出登录按钮
点击退出登录,则跳转到登录页面,再次填写登录信息,进行提交登录
写代码流程:
1共通文件夹包括:共同头部,底部,共同函数以及数据库
2首页,登录页,已登录页,退出页面,四个页面
以下为cookie方式 首页: <?php $page_title = '首页'; include ('inc/header.php'); echo '<h2 style="color:red">我是首页</h2>'; if ((isset($_COOKIE['user_id'])) && basename($_SERVER['PHP_SELF']) != 'logout.php') { echo '<a href="logout.php">退出</a>'; } else { echo '<a href="login.php">登录</a>'; } include ('inc/footer.php'); ?> 登录页:<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { //加载公共函数库 require ('inc/function.php'); //连接数据库 require ('inc/connect.php'); //验证登录 //$check=true/false; $data=['user_id'=>$user_id, 'user_name'=>$user_name]; list($check, $data) = check_login($dbc, $_POST['email'], $_POST['password']); //验证通过 if ($check) { //设置cookies setcookie('user_id', $data['user_id']); setcookie('user_name', $data['user_name']); //跳转页面 redirect_user('loggedin.php'); } else { //验证失败 $errors = $data; } //关闭数据库连接 mysqli_close($dbc); } //加载 include('login_page.php'); ?> <?php /** * 登录页面并报告错误 * 设置当前页面的标题 * 在login.php中调用 */ $page_title = '用户登录'; //加载头部文件 include('inc/header.php'); //打印错误信息 if (isset($errors) && !empty($errors)) { $errors_msg = '<p style="color:red">'; foreach ($errors as $msg) { $errors_msg .= $msg.'<br>'; } echo $errors_msg.'</p>'; } ?> <h2 style="color:red">用户登录</h2> <form action="login.php" method="post"> <p> <label for="email">邮箱:</label> <!--使用粘性表单技术在文本框中显示用户之前输入的内容,提升用户体验--> <input type="email" name="email" id="email" value="<?php echo isset($_POST['email'])?$_POST['email']:'' ?>"> </p> <p> <label for="password">密码:</label> <input type="password" name="password" id="password" value="<?php echo isset($_POST['password'])?$_POST['password']:'' ?>"> </p> <p><button type="submit" name="submit" id="submit">登录</button></p> </form> <?php include ('inc/footer.php'); //加载底部 ?> 已经登录页面: <?php if (!isset($_COOKIE['user_id'])) { require ('inc/function.php'); //跳转到默认首页 redirect_user(); } //如果已经登录 //设置页面标题 $page_title = '已经登录'; include ('inc/header.php'); //打印欢迎信息,并提供退出功能 echo <<< "WELCOME" <h2 style="color:red">登陆成功</h2> <p>欢迎您: {$_COOKIE['user_name']}</p> <p><a href="logout.php">退出</a></p> WELCOME; //加载底部 include ('inc/footer.php'); ?> 退出页面 <?php if (!isset($_COOKIE['user_id'])) { require ('inc/function.php'); //跳转到默认首页 redirect_user(); } else { //删除cookies setcookie('user_id', '', time()-3600); setcookie('user_name','', time()-3600); } //设置页面标题 $page_title = '已经登录'; include ('inc/header.php'); //打印退出信息,并提供登录功能 echo <<< "WELCOME" <h2 style="color:red">退出成功</h2> <p><a href="login.php">登录</a></p> WELCOME; include ('inc/footer.php'); ?>
点击 "运行实例" 按钮查看在线实例
以下为session方式:
首页:
<?php
session_start(); //开启会话
$page_title = '首页';
include ('inc/header.php');
echo '<h2 style="color:red">我是首页</h2>';
//if ((isset($_COOKIE['user_id'])) && basename($_SERVER['PHP_SELF']) != 'logout.php') {
if ((isset($_SESSION['user_id'])) && basename($_SERVER['PHP_SELF']) != 'logout.php') {
echo '<a href="logout.php">退出</a>';
} else {
echo '<a href="login.php">登录</a>';
}
include ('inc/footer.php');
?>
登录页面:
<?php
//开启会话
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//加载公共函数库
require ('inc/function.php');
//连接数据库
require ('inc/connect.php');
//验证登录
//$check=true/false; $data=['user_id'=>$user_id, 'user_name'=>$user_name];
list($check, $data) = check_login($dbc, $_POST['email'], $_POST['password']);
//验证通过
if ($check) {
//设置cookies
// setcookie('user_id', $data['user_id']);
// setcookie('user_name', $data['user_name']);
//设置session会话
$_SESSION['user_id'] = $data['user_id'];
$_SESSION['user_name'] = $data['user_name'];
//跳转页面
redirect_user('loggedin.php');
} else {
//验证失败
$errors = $data;
}
//关闭数据库连接
mysqli_close($dbc);
}
//加载
include('login_page.php');
?>
<?php
/**
* 登录页面并报告错误
* 设置当前页面的标题
* 在login.php中调用
*/
$page_title = '用户登录';
//加载头部文件
include('inc/header.php');
//打印错误信息
if (isset($errors) && !empty($errors)) {
$errors_msg = '<p style="color:red">';
foreach ($errors as $msg) {
$errors_msg .= $msg.'<br>';
}
echo $errors_msg.'</p>';
}
?>
<h2 style="color:red">用户登录</h2>
<form action="login.php" method="post">
<p>
<label for="email">邮箱:</label>
<!--使用粘性表单技术在文本框中显示用户之前输入的内容,提升用户体验-->
<input type="email" name="email" id="email" value="<?php echo isset($_POST['email'])?$_POST['email']:'' ?>">
</p>
<p>
<label for="password">密码:</label>
<input type="password" name="password" id="password" value="<?php echo isset($_POST['password'])?$_POST['password']:'' ?>">
</p>
<p><button type="submit" name="submit" id="submit">登录</button></p>
</form>
<?php include ('inc/footer.php'); //加载底部 ?>
已登录页面:
<?php
//开启会话
session_start();
//if (!isset($_COOKIE['user_id'])) {
if (!isset($_SESSION['user_id'])) {
require ('inc/function.php');
//跳转到默认首页
redirect_user();
}
//如果已经登录
//设置页面标题
$page_title = '已经登录';
include ('inc/header.php');
//打印欢迎信息,并提供退出功能
echo <<< "WELCOME"
<h2 style="color:red">登陆成功</h2>
<p>欢迎您: {$_SESSION['user_name']}</p>
<p><a href="logout.php">退出</a></p>
WELCOME;
//加载底部
include ('inc/footer.php');
?>
退出页面:
<?php
//开启会话
session_start();
if (!isset($_SESSION['user_id'])) {
require ('inc/function.php');
//跳转到默认首页
redirect_user();
} else {
//删除cookies
// setcookie('user_id', '', time()-3600);
// setcookie('user_name','', time()-3600);
//删除会话
$_SESSION = [];
session_destroy();
setcookie('PHPSESSID', '', time()-3600);
}
//设置页面标题
$page_title = '已经登录';
include ('inc/header.php');
//打印退出信息,并提供登录功能
echo <<< "WELCOME"
<h2 style="color:red">退出成功</h2>
<p><a href="login.php">登录</a></p>
WELCOME;
include ('inc/footer.php');
?>