要求:完成以下页面的制作
1、login.php:登录页面,存在权限类别(user和admin),登录成功后重定向到admin.php/user.php。
2、admin.php:admin用户登录后访问的页面,只有admin用户登录后能够访问,若为非
admin权限用户需重定向到登录页面。
3、user.php:user用户登录后访问的页面,只有user用户登录后能够访问,若为非user权
限用户需重定向到登录页面。
4、logout.php:退出登录,退出登录后将重定向到登录页面。
5、register.php:注册页面,实现账户注册。
0x1登录
HTML部分:login.html
<!DOCTYPE html><html></html><head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/></head><body> <h3 style="text-align:center">登录</h3> <form action="./login.php" method="POST" style="text-align:center"> <input type="text" name="username" placeholder="用户名" required="required"/> <br/> <input type="password" name="password" placeholder="密码" required="required"/> <br/> <input type="submit" name="submit" value="登录"/> <p style="text-align:center">没有账号?<a href="register.php"><b>立即注册!</b></a></p> </form></body></html>
PHP部分:login.php
文件版
<?php if($_POST['submit']=="登录"){ if($_POST['username']=="admin"){ setcookie('username', $_POST['username'], time()+10086); setcookie('password', $_POST['password'], time()+10086); header("Location:admin.php"); die; } $dir = opendir("./acount"); $username_md5_file = md5($_POST['username']).".txt"; $flag = 0; while(($filename=readdir($dir))!=false){ if($filename==$username_md5_file){ $flag = 1; break; } } closedir($dir); if(!$flag){ echo "用户不存在,请先注册!"; echo "<meta http-equiv=\"Refresh\" content=\"1;url=login.php\"/>"; } else{ setcookie('username', $_POST['username'], time()+10086); setcookie('password', $_POST['password'], time()+10086); header("Location:user.php"); } } else include "./login.html"; ?>
注释:1、PHP cookie:https://www.runoob.com/php/php-cookies.html
2、PHP内嵌HTML(较多)时可以用"include":include简介
3、PHP页面跳转可以有两种方法:header头和meta标签。
数据库版(数据库名为user_information,下有表register,表含两个字段username和password)
<?php header('Content-type:text/html;charset=utf-8'); if($_POST['submit']=="登录"){ if($_POST['username']=="admin"){ setcookie('username', $_POST['username'], time()+10086); setcookie('password', $_POST['password'], time()+10086); header("Location:admin.php"); die; } $connect = mysql_connect('localhost', 'root', 'root'); //连接服务器 if(!$connect) die("数据库连接失败!"); //else // echo "数据库连接成功!"; mysql_select_db("user_information", $connect); //选择数据库 $words = "SELECT * FROM register WHERE username='$_POST[username]'"; $rezult = mysql_query($words); $value = mysql_fetch_array($rezult); //将查询结果取为数组 $name = $value['username']; $pass = $value['password']; if($name!==$_POST['username']){ echo "用户不存在,请先注册!"; echo "<meta http-equiv=\"Refresh\" content=\"3;url=login.php\"/>"; } else{ if($pass!==$_POST['password']){ echo "密码错误!"; echo "<meta http-equiv=\"Refresh\" content=\"3;url=login.php\"/>"; die; } setcookie('username', $_POST['username'], time()+10086); setcookie('password', $_POST['password'], time()+10086); header("Location:user.php"); } } else include "./login.html"; ?>
0x2登录成功
admin.php:
<?php if (isset($_COOKIE["username"])&&$_COOKIE["username"]=="admin"&&$_COOKIE["password"]=="admin"){ echo "欢迎您,".$_COOKIE["username"]."!<br/>"; echo "这里什么也没有,你不用再看了!"."<br/>"; echo "<a href=\"logout.php\">退出登录</a>"; } else{ echo "权限不足或账户错误!"."<br/>"; echo "3秒后将自动为您跳转到首页。"; echo "<meta http-equiv=\"Refresh\" content=\"3;url=login.php\"/>"; }?>
注释:1、isset函数:https://www.php.net/manual/zh/function.isset.php
user.php:
<?php if (isset($_COOKIE["username"])){ echo "欢迎您,".$_COOKIE["username"]."!<br/>"; echo "空空如也!"."<br/>"; echo "<a href=\"logout.php\">退出登录</a>"; } else{ echo "账户错误!"."<br/>"; echo "3秒后将自动为您跳转到首页。"; echo "<meta http-equiv=\"Refresh\" content=\"3;url=login.php\"/>"; }?>
0x3退出登录
logout.php:
<?php setcookie('username', '', time()-1); setcookie('password', '', time()-1); header("Location:login.php");?>
0x4注册
HTML部分:register.html
<!DOCTYPE html><html></html><head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/></head><body> <h3 style="text-align:center">注册</h3> <form action="./register.php" method="POST" style="text-align:center"> <input type="text" name="username" placeholder="用户名" required="required"/> <br/> <input type="password" name="password" placeholder="密码" required="required"/> <br/> <input type="submit" name="submit" value="注册"/> <p style="text-align:center">已有账号?<a href="login.php"><b>立即登录!</b></a></p> </form></body></html>
PHP部分:register.php
文件版
<?php if($_POST['submit']=="注册"){ $username = $_POST['username']; $password = $_POST['password']; $filename = md5($username).".txt"; $path = "./acount/$filename"; if(file_exists($path)){ echo "该用户已存在!"."<br/>"; echo "<a href=login.php>返回登录</a>"; } else{ $file = fopen($path, "w"); $content = "username:".md5($username)."\n"."password:".md5($password); fwrite($file, $content); echo "注册成功!"."<br/>"; echo "<a href=login.php>立即登录</a>"; } } else include "./register.html"?>
注释:1、file_exists函数:https://www.w3school.com.cn/php/func_filesystem_file_exists.asp
附·dalao版代码:
<?phpfunction writeAccount($username,$password){ if(!file_exists("./account/")) mkdir("./account/"); $filename=md5($username); $dir=scandir("./account/"); foreach($dir as $name){ if($name==$filename) echo "<h4>账户已注册!请重新注册</h4>"; echo '<meta http-equiv="Refresh" content="3;url=register.php"/>'; } try{ file_put_contents('./account/'.$filename , $username."\n".$password); }catch(Exception $e){ echo "<h4>抱歉!账户存储失败!请尝试重新注册</h4>"; echo '<meta http-equiv="Refresh" content="3;url=register.php"/>'; }}function readAccount($inputUsername,$inputPassword){ $filename=md5($inputUsername); $dir=scandir("./account/"); $accountExist=FALSE; foreach($dir as $name){ if($name===$filename){ $accountExist=TRUE; } } if($accountExist){ $content=explode("\n" , file_get_contents('./account/'.$filename)); $username=$content[0]; $password=$content[1]; if($username===$inputPassword&&$password===$inputPassword){ setcookie('username', $username , time()+10086); setcookie('password', $password , time()+10086); echo "<h4>欢迎访问——用户${username}</h4>"; if($inputPassword==="admin") echo '<meta http-equiv="Refresh" content="3;url=admin.php"/>'; else echo '<meta http-equiv="Refresh" content="3;url=user.php"/>'; } }else{ echo "<h4>账户未注册!请先注册</h4>"; echo '<meta http-equiv="Refresh" content="3;url=register.php"/>'; }}#writeAccount("admin","admin");#readAccount("admin","admin")?>
数据库版
<?php header('Content-type:text/html;charset=utf-8'); if($_POST['submit']=="注册"){ $username = $_POST['username']; $password = $_POST['password']; $connect = mysql_connect('localhost', 'root', 'root'); //连接服务器 if(!$connect) die("数据库连接失败!"); //else // echo "数据库连接成功!"; mysql_select_db("ccdb", $connect); //选择数据库 $words = "INSERT INTO register (username, password) VALUES ('$username', '$password')"; $rezult = mysql_query($words); if($rezult){ echo "注册成功!"; echo "<meta http-equiv=\"Refresh\" content=\"3;url=login.php\"/>"; } else{ echo "注册失败!"; echo "<meta http-equiv=\"Refresh\" content=\"3;url=register.php\"/>"; } mysql_close($connect); //切断连接 } else include "./register.html" ?>
模板链接:友情链接
操作指南:(以本地操作为例,本地使用的是PHPstudy集成环境)
1、在PHPstudy应用文件夹下的WWW文件夹内新建demo文件夹,将所有源码文件放入demo文件夹,(再在demo文件夹内新建acount<用于存放注册信息>文件夹)。
2、开启PHPstudy。访问localhost/demo/login.php,即可开启一系列操作。默认管理员账密为"admin admin",管理员无需注册即可访问admin.php,普通用户需注册方可访问user.php。