会员系统—注册与登陆(源代码分享)

摸索着用PHP语言做了一个会员系统。

以下是1.0.0版

首页 index.html

<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6 lt8"> <![endif]-->
<!--[if IE 7 ]> <html lang="en" class="no-js ie7 lt8"> <![endif]-->
<!--[if IE 8 ]> <html lang="en" class="no-js ie8 lt8"> <![endif]-->
<!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
<!-- 陌风网络实验室版本号:1.0.0 --> 
<head>
<meta charset="UTF-8" />
<!-- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> -->
<title>陌风网络实验室</title>
<meta name="author" content="陌风" />
<link rel="shortcut icon" href="../favicon.ico"> 
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
</head>
<body>
<div class="container">
<header>
<h1>陌风网络实验室</h1>
<nav class="codrops-demos">
<span></span>
</nav>
</header>
<section> 
<div id="container_demo" >
<a class="hiddenanchor" id="toregister"></a>
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="login" class="animate form">
<form action="sign.php" method="post" autocomplete="on"> 
<h1>登陆</h1> 
<p> 
<label for="username" class="uname" data-icon="u" > 电子邮件或用户名:</label>
<input id="username" name="username" required type="text" placeholder="myusername or mymail@mail.com"/>
</p>
<p> 
<label for="password" class="youpasswd" data-icon="p"> 密码:</label>
<input id="password" name="password" required type="password" placeholder="eg. X8df!90EO" /> 
</p>
<p class="keeplogin"> 
<input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" /> 
<label for="loginkeeping">保持登陆状态</label>
</p>
<p class="login button"> 
<input type="submit" value="Login" /> 
</p>
<p class="change_link">
还没有账号?
<a href="#toregister" class="to_register">加入我们</a>
</p>
</form>
</div>

<div id="register" class="animate form">
<form action="signup.php" method="post" autocomplete="on"> 
<h1> 注册 </h1> 
<p> 
<label for="usernamesignup" class="uname" data-icon="u">你的用户名:</label>
<input id="usernamesignup" name="usernamesignup" required type="text" placeholder="mysuperusername690" />
</p>
<p> 
<label for="emailsignup" class="youmail" data-icon="e" > 你的电子邮件:</label>
<input id="emailsignup" name="emailsignup" required type="email" placeholder="mysupermail@mail.com"/> 
</p>
<p> 
<label for="passwordsignup" class="youpasswd" data-icon="p">你的密码:</label>
<input id="passwordsignup" name="passwordsignup" required type="password" placeholder="eg. X8df!90EO"/>
</p>
<p> 
<label for="passwordsignup_confirm" class="youpasswd" data-icon="p">请再次输入你的密码:</label>
<input id="passwordsignup_confirm" name="passwordsignup_confirm" required type="password" placeholder="eg. X8df!90EO"/>
</p>
<p class="signin button"> 
<input type="submit" value="Sign up"/> 
</p>
<p class="change_link"> 
已经有账号了 ?
<a href="#tologin" class="to_register"> 点击去登陆 </a>
</p>
</form>
</div>

</div>
</div> 
</section>
</div>
<?php
include 'foot.php';
?>
</body>
</html>

注册页面 signup.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>注册</title>
</head>
<?php
require './sql_config.php';// 数据库配置
$usernamesignup = $_POST['usernamesignup'];
$passwordsignup = $_POST['passwordsignup'];
$emailsignup = $_POST['emailsignup'];
// 创建连接
$conn = new mysqli(SQLservername, SQLusername, SQLpassword, SQLdbname);
// 检测连接
if ($conn->connect_error) {
die("连接数据库失败: " . $conn->connect_error);
}

$sql = "INSERT INTO user (name, password, email)
VALUES ('$usernamesignup', '$passwordsignup', '$emailsignup')";

if ($conn->query($sql) === TRUE) {
echo "注册成功!";
} else {
echo "错误: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
<body>
<p><a href="https://netlab.sunan.me/#tologin">返回登陆</a>
<?php
include './foot.php';
?>
</body>
</html>

登陆处理页面 sign .php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>个人中心</title>
</head>
<?php
require('sql_config.php'); // 数据库配置
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['username']; //用来验证邮件是否符合
// 创建连接 
$conn = new mysqli(SQLservername, SQLusername, SQLpassword, SQLdbname); 
// 检测连接 
if ($conn->connect_error) { 
die("连接数据库失败: " . $conn->connect_error); 
} 
session_start();
//开启session
if (($username == '') || ($password == '')) {
// 若为空,视为未填写,提示错误,并3秒后返回登录界面
header('refresh:3; url=index.html');
echo "用户名或密码不能为空,系统将在3秒后跳转到登录界面,请重新填写登录信息!";
exit;
}
else{
$sql_username = "SELECT * FROM user WHERE username = '$username' and password='$password'";//检测数据库是否有对应的username和password的sql
$sql_email = "SELECT * FROM user WHERE username = '$email' and password='$password'";//检测数据库是否有对应的email和password的sql
$result_username = mysql_query($sql_username);//执行sql,正确返回true
$result_email = mysql_query($sql_email);//执行sql,正确返回true
}
if(($result_username)||($result_email)){
# 用户名和密码都正确,将用户信息存到Session中
$_SESSION['username'] = $username;
$_SESSION['islogin'] = 1;
// 处理完附加项后跳转到登录成功的首页
header('location:welcome.php');
} else{
header('refresh:3; url=index.html');
echo "用户名或密码错误,系统将在3秒后跳转到登录界面,请重新填写登录信息!";
exit;
}

?>
<body>
</body>
</html>

登陆成功页面 welcome.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户中心</title>
</head>
<?php
require './sql_config.php';
$conn = new mysqli(SQLservername, SQLusername, SQLpassword, SQLdbname); // 创建连接
//检测连接
if ($conn->connect_error) { 
die("连接数据库失败: " . $conn->connect_error); 
} 
session_start();
echo "Hi,".$_SESSION['username'];
echo "</br>恭喜你成为陌风网络实验室的小白鼠,庆祝一下吧!";
include './foot.php';
?>
<body>
</body>
</html>

小细节页面:页面底部 foot.php

<?php
echo <<< ONE
<p>版本号:1.0.0</p>
<hr/>
<p align="center">Copyright 陌风网络实验室. All Rights Reserved.  </p>
ONE;
?>

PS :事先需要注册一下数据库表格用来储存注册用户数据的。

附PHP脚本注册部分关键源码:

// 创建表
$sql = "CREATE TABLE user (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
name VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
echo "表uesr成功创建";
} else {
echo "创建错误: " . $conn->error;
}

$conn->close();

End

一切完成了。

会员系统会后续补充完善,欢迎交流学习。

PHP是世界上最好的语言。(下面可以留言哦)

为您推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注