准备环境
- window2008
- phpstudy
- Mysql数据库
实验要求
-
第一步:bbs首页
index.php
<html> <head> <meta charset = "utf-8"> <title>首页--bbs</title> </head> <body> <h1>wcp-bbs</h1> <a href = "./member/register.php" target = "_blank">register</a> <br /> <a href = "./member/login.php" target = "_blank">login</a> <hr /> </body> </html>
代码运行
第二步:点击注册,填写信息
register.php
<html> <head> <meta charset = "utf-8"> <title>register--bbs</title> </head> <body> <h1>wcp -- bbs</h1> <form action = "./addUser.php" #将表单信息提交的文件路径 method = "post" #提交的方式 target = "_blank" #打开新的页面 > 用户名:<input type = "text" name = "userName"><br /> 密码:<input type = "password" name = "userPass1"><br /> 确认密码:<input type = "password" name = "userPass2"><br /> <input type = "submit" name = "userSubmit" value = "注册"> </form> <hr /> </body> </html>
代码运行
第三步:注册信息判断,存入数据库
addUser.php
<meta charset ="utf-8"> <?php include "../mysqlcon/dblink.php" ?> <?php //var_dump($_POST); if (isset($_POST['userSubmit'])){//判断是否点击提交 $userName = $_POST['userName']; $userPass1 = $_POST['userPass1']; $userPass2 = $_POST['userPass2']; if(isset($userName)){//判断用户名不为空 if(isset($userPass1) && isset($userPass2) && $userPass1 === $userPass2){//判断两次密码相同,且不为空 //信息存入数据库 $sql = "insert into users(name,passwd) value( '".$userName."','".md5($userPass1)."' )"; //echo $sql; if(mysqli_query($link,$sql)){ echo "注册成功,<a href = './index.php' target = '_blank'>返回个人中心</a>"; setcookie("name",$userName); }else{ echo "sql error"; } } else{ echo "密码输入有误 <br />"; echo "<a href='./register.php'>请重新注册</a>"; } }else{ echo "用户名不能空<br />"; echo "<a href='./register.php'>请重新注册</a>"; } }else{ echo "请正确提交<br />"; header("Location:./register.php"); } ?> <?php mysqli_close($link);//关闭数据库连接 ?>
代码运行
代码分析
数据库连接
<?php $dbHost = "127.0.0.1"; $dbUser = "root"; $dbPass = "456.com"; $dbName = "wcp"; $link = mysqli_connect($dbHost,$dbUser,$dbPass,$dbName); if (!$link){ echo mysqli_connect_error(); } ?>
第四步:返回个人主页,进行登录
index.php
<html> <head> <meta charset="utf-8"> <title>首页--个人中心</title> </head> <body> <h1>wcp--bbs</h1> <?php if(isset($_COOKIE['name'])){ echo "欢迎您,".$_COOKIE['name']; echo "<br />"; echo "<a href = './login.php'>登录</a><br />"; echo "<a href = './logout.php'>注销用户</a><br />"; }else{ echo "<a href = '../index.php'></a>"; } ?> <hr /> </body> </html>
代码运行
第五步:进行登录
login.php
<html> <head> <meta charset = "utf-8"> <title>login--bbs</title> </head> <body> <h1> bbs</h1> <form action = "./judgeUser.php" method = "post" target = "_blank" > 用户名:<input type = "text" name = "userName"><br /> 密码:<input type = "password" name = "userPass"><br /> <input type = "submit" name = "userSubmit" value = "登录"> </form> <hr /> </body> </html>
代码运行
第五步:登录信息判断,与数据库匹配
judgeUser.php
<meta charset="utf-8"> <?php session_start(); include "../mysqlcon/dblink.php"; if(isset($_POST['userSubmit'])){ $username = $_POST['userName']; $userpass = $_POST['userPass']; if(isset($username) && isset($userpass)){ $sql = "select passwd from users where name='".$username."'"; if(!mysqli_query($link,$sql)){ echo "sql语句有误"; }else{ $pass1 = mysqli_fetch_row(mysqli_query($link,$sql))[0]; $pass2 = md5($userpass); if($pass1 == $pass2){ echo "登录成功,返回<a href='./index.php'>个人中心</a>"; $_SESSION['name'] = $username; }else{ echo "用户名或密码错误,请<a href='./login.php'>重新登录</a>"; } } }else{ echo "用户名或密码为空,请<a href='./login.php'>重新输入</a>"; } } mysqli_close($link); ?>
代码运行
- 人生匆匆,一切都是过眼云烟。
注册信息存入Mysql数据库中
生成验证码图片
头像上传到mysql数据库
登录与mysql信息匹配
编写留言板