本文将基于PHP以及mysql设计一个最最基础的登录注册页面,所用到的软件有wampserver以及各种PHP编译器,我选择的是,vscode。
第一部分先写个连接数据库文件。
conn.php
<?php
$conn =@mysqli_connect("localhost","root","") or die("数据库连接出错!");//相应的数据库地址 用户名 和密码
$selected = mysqli_select_db($conn,"regist");
?>
使用这个后面每次使用时,就可以直接调用,一劳永逸。
现在来写注册界面
signup.php
<?php
session_start();
include("conn.php");//连接数据库
$username =isset($_POST['username'])? $_POST['username'] : '';//用户名
$password2 =isset($_POST['password2'])? $_POST['password2'] : '';//密码
$password1 =isset($_POST['password1'])? $_POST['password1'] : '';
if(isset($_POST['signup']))
{if($password2==$password1){
$sql ="INSERT INTO account(username,password) VALUES('$username','$password2')";
mysqli_query($conn,$sql);
echo "<script>alert('注册成功');window.location ='login.php';</script>";
}
else {echo "<script>alert('两次密码不一致!');window.location ='index.php';</script>";}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>注册页面</h2>
<form action ="" method = "post" name ="myform" ousubmit ="return Checked();">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"</td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password1"</td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="password2"</td>
</tr>
<tr>
<td><input type="submit" name="signup" value="注册"></td>
<td><a href ="login.php">登录</a></td>
</tr>
<tr>
<td><a href="index.php">返回首页</a></td>
</tr>
</table>
</form>
</body>
</html>
其实很简单,就是连接数据库,将写入的数据通过sql语言写入数据库。
这里有个问题,我编写过程中一直遇到的就是,再创建表的过程中,会有一个码叫“id”,他应该是随着输入的用户,自增,所以在创建表的时候务必将id 设为主码,然后设置自增,一下是参id integer PRIMARY KEY AUTOINCREMENT。
登录界面
login.php
<?php
error_reporting(0);
session_start();
include("conn.php");
$postUsername =isset($_POST['username'])? $_POST['username'] : '';//用户名
$postpassword =isset($_POST['password'])? $_POST['password'] : '';//密码
$sql ="SELECT username,password FROM account WHERE username ='$postUsername'";
$query =mysqli_query($conn,$sql);
$row =mysqli_fetch_array($query,MYSQLI_ASSOC);
$username =isset($row['username'])? $row['username'] : '';
$password =isset($row['password'])? $row['password'] : '';
$code=$_POST['code'];
$_SESSION["username1"]=$postUsername;
//setcookie('username',$postUsername,time()+3600,'/');
if(isset($_POST['login']))
{
if($code ==$_SESSION['验证码']){
if($username ==$postUsername && $password==$postpassword)
{
echo"<script>alert('登录成功');window.location ='web.php'</script>";
}
else{echo"<script>alert('用户名或密码错误!');history.go(-1)</script>";}
}
else{echo"<script>alert('验证码错误!');history.go(-1)</script>";}
}
$user
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登陆页面</title>
</head>
<body>
<h2>登陆页面</h2>
<form action ="" method = "post" name ="myform" ousubmit ="return Checked();">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"</td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"</td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" style="width:150px;" name="code"</td><td></td>
<td><img src="code.php" class="pull-right" style="cursor:pointer;" onclick="this.src+'?d='+Math.random();" title="点击刷新" alt="code.php"</td>
</tr>
<tr>
<td><input type="submit" name="login" value="登录"></td>
<td><a href ="signup.php">注册</a></td>
</tr>
<tr>
<td><a href="index.php">返回首页</a></td>
</tr>
</table>
</form>
</body>
</html>
在登陆界面的中加入了验证码功能
code.php
<?php
session_start();
//默认返回的是黑色的照片
$image = imagecreatetruecolor(100, 30);
//将背景设置为白色的
$bgcolor = imagecolorallocate($image, 255, 255, 255);
//将白色铺满地图
imagefill($image, 0, 0, $bgcolor);
//空字符串,每循环一次,追加到字符串后面
$captch_code='';
//验证码为随机四个数字
for ($i=0; $i < 4; $i++) {
$fontsize=6;
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
//产生随机数字0-9
$fontcontent = rand(0,9);
$captch_code.= $fontcontent;
//数字的位置,0,0是左上角。不能重合显示不完全
$x=($i*100/4)+rand(5,10);
$y=rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['验证码'] = $captch_code;
//点
for ($i=0; $i < 200; $i++) {
$pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
}
//线
for ($i=0; $i < 3; $i++) {
$linecolor = imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));
imageline($image, rand(1,99), rand(1,29),rand(1,99), rand(1,29) ,$linecolor);
}
header('content-type:image/png');
imagepng($image);
//销毁
imagedestroy($image);