php mysql 用户登录注册示例源代码 共享/下载
最编程
2024-03-13 14:16:29
...
实现用户登录注册系统的步骤:
- 创建数据库
- 创建用户表,包括以下字段:
id, username, email, password, trn_date - 创建注册表单
- 创建登录表单
- 通过使用PHP连接到数据库
- 验证登录的用户
- 创建首页
- 创建仪表板页面
- 创建注销
- 创建CSS文件
下面分别介绍各个步骤的实现过程。
1.创建数据库
要创建一个数据库,登录到phpMyAdmin,然后点击数据库选项卡上,输入数据库名称,然后单击上创建数据库按钮或简单地执行下面的查询。在我来说,我创建的数据库的名称为register。
CREATE DATABASE register;
2.创建数据库表
要创建表,执行SQL中的以下查询。
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`trn_date` datetime NOT NULL,
PRIMARY KEY (`id`)
);
3.创建一个注册表单页面
简单的创建一个名字为registration.php页面并粘贴下面的代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
require('db.php');
// If form submitted, insert values into the database.
//作者:www.manongjc.com
if (isset($_REQUEST['username'])){
// removes backslashes
$username = stripslashes($_REQUEST['username']);
//escapes special characters in a string
$username = mysqli_real_escape_string($con,$username);
$email = stripslashes($_REQUEST['email']);
$email = mysqli_real_escape_string($con,$email);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
$trn_date = date("Y-m-d H:i:s");
$query = "INSERT into `users` (username, password, email, trn_date)
VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";
$result = mysqli_query($con,$query);
if($result){
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<div class="form">
<h1>Registration</h1>
<form name="registration" action="" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register" />
</form>
</div>
<?php } ?>
</body>
</html>
4.创建登录表单
创建名为的login.php页面并粘贴下面的代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
require('db.php');
session_start();
// If form submitted, insert values into the database.
if (isset($_POST['username'])){
// removes backslashes
$username = stripslashes($_REQUEST['username']);
//escapes special characters in a string
$username = mysqli_real_escape_string($con,$username);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE username='$username'
and password='".md5($password)."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION['username'] = $username;
// Redirect user to index.php
////作者:http://www.manongjc.com/article/1552.html
header("Location: index.php");
}else{
echo "<div class='form'>
<h3>Username/password is incorrect.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<div class="form">
<h1>Log In</h1>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input name="submit" type="submit" value="Login" />
</form>
<p>Not registered yet? <a href='registration.php'>Register Here</a></p>
</div>
<?php } ?>
</body>
</html>
5.连接到数据库
创建名为db.php文件并粘贴下面的代码。
<?php
// Enter your Host, username, password, database below.
// I left password empty because i do not set password on localhost.
$con = mysqli_connect("localhost","root","","register");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
6.用户身份验证
创建名为auth.php页面并粘贴下面的代码。
<?php
session_start();
if(!isset($_SESSION["username"])){
header("Location: login.php");
exit(); }
?>
7.创建index.php页面
创建名称的index.php页面并粘贴下面的代码。
<?php
//include auth.php file on all secure pages
include("auth.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome Home</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="form">
<p>Welcome <?php echo $_SESSION['username']; ?>!</p>
<p>This is secure area.</p>
<p><a href="dashboard.php">Dashboard</a></p>
<a href="logout.php">Logout</a>
</div>
</body>
</html>
8.创建仪表板页面
创建名为dashboard.php页面并粘贴下面的代码。
<?php
require('db.php');
include("auth.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dashboard - Secured Page</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="form">
<p>Dashboard</p>
<p>This is another secured page.</p>
<p><a href="index.php">Home</a></p>
<a href="logout.php">Logout</a>
</div>
</body>
</html>
9.创建注销
创建名为logout.php页面并粘贴下面的代码。
<?php
session_start();
// Destroying All Sessions
if(session_destroy())
{
// Redirecting To Home Page
header("Location: login.php");
}
?>
10.创建一个CSS文件
创建名为style.css的页面并粘贴下面的代码。
body {
font-family:Arial, Sans-Serif;
}
.clearfix:before, .clearfix:after{
content: "";
display: table;
}
.clearfix:after{
clear: both;
}
a{
color:#0067ab;
text-decoration:none;
}
a:hover{
text-decoration:underline;
}
.form{
width: 300px;
margin: 0 auto;
}
input[type='text'], input[type='email'],
input[type='password'] {
width: 200px;
border-radius: 2px;
border: 1px solid #CCC;
padding: 10px;
color: #333;
font-size: 14px;
margin-top: 10px;
}
input[type='submit']{
padding: 10px 25px 8px;
color: #fff;
background-color: #0067ab;
text-shadow: rgba(0,0,0,0.24) 0 1px 0;
font-size: 16px;
box-shadow: rgba(255,255,255,0.24) 0 2px 0 0 inset,#fff 0 1px 0 0;
border: 1px solid #0164a5;
border-radius: 2px;
margin-top: 10px;
cursor:pointer;
}
input[type='submit']:hover {
background-color: #024978;
}
至此,一个简单的php mysql用户登录注册系统已经完成,希望本文章对刚踏入php网站开发的新手有所帮助。
上一篇: npm 帐户创建、测试、登录
下一篇: 用户登录/注销的 WinCC 脚本应用