批改状态:合格
老师批语:
首页index.php
<?php
header("content-type:text/html;charset=utf-8");
//加载头部文件
include 'top.php';
//加载中间主体文件
include 'middle.php';
//加载底部文件
include 'foot.php';
?>点击 "运行实例" 按钮查看在线实例
2.头部文件cookie存值
<div style="width: 100%; height: 70px;">
<div style="float: left; width: 50%;height: 70px;">
<style type = "text/css">
a {font-size:16px}
a:link {color: blue; text-decoration:none;}
a:active:{color: red; }
a:visited {color:purple;text-decoration:none;}
a:hover {color: red; text-decoration:underline;}
</style>
<?php if( !isset($_COOKIE['account']) ){ ?>
<p>
<a style="color: red; margin-left: 10%;" href="login.php">亲,请登录</a>
<a style="color: #0080C0;; margin-left: 20px;" href="register.php">注册</a>
<?php }else{ ?>
<p >
<a style="color: red; margin-left: 10%;" href="login.php">欢迎您,<?php echo $_COOKIE['account']?></a>
<a style="color: #0080C0;; margin-left: 20px;" href="quit.php">退出</a>
<?php } ?>
</p>
</div>
<div style="float: right;height: 70px;margin-right: 100px;"><p>联系电话:033-88652356 </p></div>
</div>点击 "运行实例" 按钮查看在线实例
3.头部文件session存值
<!--session存值页面-->
<div style="width: 100%; height: 70px;">
<div style="float: left; width: 50%;height: 70px;">
<style type = "text/css">
a {font-size:16px}
a:link {color: blue; text-decoration:none;}
a:active:{color: red; }
a:visited {color:purple;text-decoration:none;}
a:hover {color: red; text-decoration:underline;}
</style>
<?php
session_start();
if( !isset($_SESSION['account']) ){ ?>
<p>
<a style="color: red; margin-left: 10%;" href="login.php">亲,请登录</a>
<a style="color: #0080C0;; margin-left: 20px;" href="register.php">注册</a>
<?php }else{ ?>
<p >
<a style="color: red; margin-left: 10%;" href="login.php">欢迎您,<?php echo $_COOKIE['account']?></a>
<a style="color: #0080C0;; margin-left: 20px;" href="quit.php">退出</a>
<?php } ?>
</p>
</div>
<div style="float: right;height: 70px;margin-right: 100px;"><p>联系电话:033-88652356 </p></div>
</div>点击 "运行实例" 按钮查看在线实例
4.用户登录页面
<!--登录页面-->
<?php header("content-type:text/html;charset=utf-8");?>
<form method="post" action="login_In.php">
<label for="account">用户名:</label>
<input type="text" name="account" id="account"/>
<label for="password">密码:</label>
<input type="text" name="password" id="password"/>
<input type="submit" value="登录" />
</form>点击 "运行实例" 按钮查看在线实例
5.处理用户提交数据页面
<?php
header("content-type:text/html;charset=utf-8");
//处理登录提交数据
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if($_POST){
$account = trim($_POST['account']);//去掉两边空格
$password = trim($_POST['password']);//去掉两边空格
//非空判断
if(strlen($account) == 0){
echo '<script> alert(\'用户名不能为空!\');javascript:history.back(-1); </script>';
exit;
}
if(strlen($password) == 0){
echo '<script> alert(\'密码不能为空!\');javascript:history.back(-1); </script>';
exit;
}
/*连接数据库验证数据*/
//pdo方式连接
include 'connect.php';
//准备sql语句
$sql = "SELECT `password` FROM `userinfo` WHERE `account`=:account";
//创建预处理对象
$stmt = $pdo->prepare($sql);
//绑定参数
$stmt->bindParam(':account',$account);
$stmt->bindColumn('password',$pass);
//执行并获取结果
if($stmt->execute()){
$stmt->fetch(PDO::FETCH_COLUMN);
if($pass == $password){
//密码正确,将账户和密码存入cookie中,跳转到首页页面
setcookie('account',$account);
//session 存数据
//session_start();
//$_SESSION['account'] = $account;
header("location:index.php");
}else{
//密码不正确,返回登录页重新填写
echo '<script> alert(\'密码错误!\');javascript:history.back(-1); </script>';
exit;
}
}else{
//用户名不正确重新输入,跳转到登录
echo '<script> alert(\'用户名错误!\');javascript:history.back(-1); </script>';
exit;
}
}else{
echo '<script> alert(\'登录未成功,请稍后重试!\');javascript:history.back(-1); </script>';
exit;
}
}else{
die('非法提交!');
}
?>点击 "运行实例" 按钮查看在线实例
6.连接数据库文件
<?php
/*PDO连接数据库*/
$dsn = 'mysql:host=localhost;dbname=test';
$user = 'root';
$pass = 'root';
try{
//设置数据库字符集
$set_char = array(PDO::ATTR_PERSISTENT=>true,PDO::ATTR_ERRMODE=>2,PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8');
//连接数据库
$pdo = new PDO($dsn,$user,$user,$set_char);
}catch(PDOException $e){
//捕捉错误信息
die('连接失败:'.$e->getMessage());
}
?>点击 "运行实例" 按钮查看在线实例
7.中间主体和底部文件
<div style="width: 100%;min-height: 650px; background-color: lightblue;"></div> <p style="text-align: center;">邮箱:kf@php.com 电话:033-88652356 </p> <p style="text-align: center;">地址:陕西省西安市碑林区西北工业大学</p> <p style="text-align: center;">陕ICP备15006021号-1 Copyright © 2018 亨氏牛网 All Rights Reserved</p>
点击 "运行实例" 按钮查看在线实例
8.退出功能文件
<?php
//退出销毁cookie
setcookie('account','',time()-3600);
header("location:index.php");
//退出销毁session
//session_start();
//unset($_SESSION['account']);
?>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号