首先创建一个数据库"register" ,再创建一个信息表"users",表结构如下
效果图:
登录:
注册:
注册测试:
登录测试:
登录成功:
所有文件如下:
database.php:
<meta charset="UTF-8">
// $connection = mysqli_connect('localhost','root','') or die('mysql连接服务器失败!');
// $db_selected = mysqli_select_db($connection,'registert') or die('mysql数据库连接失败');
// mysqli_query($connection,'set names utf8');
header("content-type:text/html;charset=utf8");
$connection = null;
function getConnection(){
$hostname = 'localhost';
$database = 'register';
$userName = 'root';
$password = '';
global $connection;
$connection = @mysqli_connect($hostname,$userName,$password,$database)or die(mysqli_error());
mysqli_query($connection,'set names utf8');
@mysqli_select_db($connection,$database) or die(mysqli_error());
}
function closeConnection(){
global $connection;
if($connection){
mysqli_close($connection) or die(mysqli_error());
}
}
?>
file.php:
<meta charset="UTF-8">
function upload($file,$filePath){
$error = $file['error'];
switch ($error) {
case 0:
$fileName = $file['name'];
$fileTemp = $file['tmp_name'];
$dest = $filePath.'/'.$fileName;
move_uploaded_file($fileTemp,$dest);
return '文件上传成功!';
case 1:
return '文件过大!';
case 2:
return '文件超过了form表单中的限制值!';
case 3:
return '附件只有部分被上传!';
case 4:
return '没有选择文件上传!';
}
}
?>
login.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<link rel="stylesheet" href="bootstrap.css" />
<style>
body{
background: url("../images/index/home_66.jpg") no-repeat;
}
.form-horizontal{
margin-left: 30px;
}
.regis{
margin-left: 30px;
}
h1{
color: white;
}
</style>
</head>
<body>
<div class="container" style="margin: 120px auto;">
<h1 class="text-center">用户登录</h1><br>
<hr width="100%">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title text-center">用户登录</h3>
</div>
<form method="post" class="form-horizontal" action="login.php">
<div class="panel-body form-horizontal text-center">
<div class="form-group">
<label >
用户名:
<input type="text" class="form-control" name="userName" id="userName">
</label>
</div>
<div class="form-group">
<label>
密码:
<input type="text" class="form-control" name="password" id="password">
</label>
</div>
<div class="form-group">
<label>
验证码:<input type="text" name="" size="8">
<img src="randcode_pro.php" id="code_img" name="code_img" onclick="this.src='randcode_pro.php?rand='+Math.random()" />
<a href="javascript:void(0)" onclick="document.getElementById('code_img').src='randcode_pro.php?r='+Math.random()">换一个</a>
</label>
</div>
<div class="form-group">
<input type="submit" value="登录" class=" login btn btn-success " onclick="form.action='login.php'" id="login" />
<input type="submit" value="注册" class=" login btn btn-success " onclick="form.action='register.html'" id="register" />
</div>
</div>
</form>
</div>
</div>
</body>
</html>
register.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户注册</title>
<link rel="stylesheet" href="bootstrap.css" />
<style>
body{
background: url("../images/index/home_66.jpg") no-repeat;
}
input{
color: #000;
}
</style>
</head>
<body>
<div class="container" style="margin: 80px auto;background-color:white; padding-bottom: 50px;">
<h3 class="text-center">用户注册</h3><br>
<hr width="100%">
<form action="register.php" class="myform" method='post' >
<div class="col-md-6 ">
<div class="form-group" >
<label class="">*用户名:</label>
<input type="text" class="form-control" name="userName" id="userName" />
</div>
<div class="form-group" style="margin-left: 0px;">
<label class="">*登录密码:   </label>
<input type="password" class="form-control" name="password" id="password" />
</div>
<div class="form-group" style="margin-left: 0px;">
<label class="">*确认密码:   </label>
<input type="password" class="form-control" name="confirm" name="confirm" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="">您的性别是:</label>
<input type="radio" name="sex" id="radio1"
value="男" checked="checked" />男
<input type="radio" name="sex" id="radio2" value="女" />女
</div>
<div class="form-group">
<label class="">个人爱好:</label>
<input type="checkbox" id="interests" name="interests[]" value="音乐" />音乐
<input type="checkbox" name="interests[]" value="游戏" /> 游戏
<input type="checkbox" name="interests[]" value="电影" />电影
</div>
<div class="form-group">
<label class="">个人相片:</label>
<input type="file" id="myimg" name="myimg" size="60" />
</div>
<div class="form-group" >
<label class="">备注信息:</label>
<textarea name="infor" id="infor" type="text" rows="3" cols="20" >
</textarea>
</div>
</div>
<div class="input-group form-group" style="margin-left: 0px;">
<input type="submit" name="sub" id="sub" value="提交表单" />
<input type="submit" name="rew" id="rew" value="重新填写" />
</div>
</form>
</div>
</body>
</html>
register.php:
<meta charset="UTF-8">
header("content-type:text/html;charset=utf8");
include ("database.php");
include("file.php");
if (empty($_POST)) {
exit("你提交的表单数据超过post_max_size的最大值!
");
}
$password = $_POST['password'];
$confirm= $_POST['confirm'];
if ($password!=$confirm) {
exit("输入的密码和确认密码不相同!");
}
$userName = $_POST['userName'];
//判断用户名是否被占用
if (!$userName || !$password || !$confirm) {
exit("用户名和密码不能为空!");
}
$userNameSQL = "select * from users where userName='$userName'";
getConnection();
$res = mysqli_query($connection,$userNameSQL);
if (mysqli_num_rows($res)>0) {
closeConnection();
exit("用户名已经被占用,请更换其他用户名!");
}
//获取用户输入的其他信息
$sex = $_POST['sex'];
if (empty($_POST['interests'])) {
$interests = "";
}else{
$interests = implode(";",$_POST['interests']);
}
$rew = @$_POST['rew'];
$myimg= @$_FILES['myimg']['name'];
$registerSQL = "insert into users values(null,'$userName','$password','$sex','$interests','$myimg','$rew')";
$message = @upload($_FILES['myimg'],"uploads");
if ($message=="文件上传成功!"||$message=="没有选择上传附件!") {
mysqli_query($connection,$registerSQL);
$userID = mysqli_insert_id($connection);
echo "用户信息注册成功!
";
}else{
exit($message);
}
//数据库再去获取用户信息
$userSQL = "select * from users where user_id=$userID";
$userRes = mysqli_query($connection,$userSQL);
if($user=mysqli_fetch_array($userRes)){
echo "你注册的用户名为:".$user['userName'];
header("refresh:30;url=index.html");
echo "3秒后,自动跳至首页";
}else{
exit("用户信息注册失败!");
}
closeConnection();
?>
login.php:
<meta charset="UTF-8">
include ("database.php");
//获取表单提交的数据
$userName = $_POST['userName'];
$password = $_POST['password'];
//连接数据库服务
getConnection();
//判断输入的用户名和密码是否正确
$sql = "select * from users where userName='$userName' and password='$password'";
$res = mysqli_query($connection,$sql);
if(mysqli_num_rows($res)>0){
header("location:loginsuccess.html");
header("loaction:../index.html");
}else{
echo "用户名和密码输入错误!登录失败!";
}
closeConnection();
loginsuccess.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h3>欢迎登录成功!</h3>
<a href="../index.html">返回首页</a>
</body>
</html>
随机码:
randcode_pro.php:
header("content-type:image/jpeg");
session_start(); //启动session
$image = imagecreatetruecolor(100,30); //创建一个宽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));
$data='1234567890qwertyuiopasdfghjklzxcvbnm';
$fontcontent=substr($data,rand(0,strlen($data))-1,1);
$captch_code.=$fontcontent;
$x=($i*100/4)+ rand(5,10);
$y=rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['authcode']=$captch_code;
//随机点,生成干扰点
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,rand(50,120),rand(50,120),rand(50,120));
imagesetpixel($image,rand(1,99),rand(1,99),$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);
}
imagejpeg($image);
imagedestory($image);
?>