在 Facebook、Instagram 等社交网站中,已登录用户的用户名和个人资料图片显示在网站的标题中,并且该标题保持不变,无论用户打开了哪个网页。可以使用会话变量创建此类功能。
会话变量仅在用户会话处于活动状态时存在。会话完成后,会话变量被销毁。这些对于每个访问者都是唯一的,通常用于在用户登录后存储用户特定的信息,例如用户名、个人资料图片等。
会话变量用于在PHP显示登录的用户信息。
项目说明及代码:
这是一个简单的注册系统。注册表。 PHP页面询问用户所需的用户名、电子邮件和密码,然后将输入的数据发送到数据库中,一旦点击提交按钮。在此之后,用户被重定向到索引。 PHP页面,其中显示欢迎消息和登录用户的用户名。
第一步是创建一个数据库,然后在其中创建一个表。数据库名为“registration”,表名为“users”。 “用户”表将包含 4 个字段。
- id – 主键 – 自增
- 用户名 – varchar(100)
- 电子邮件 – varchar(100)
- 密码 – varchar(100)
‘id’ 将是主键,这意味着它对于每个注册用户都是唯一的。它还将为每个新注册自动增加。用户名、电子邮件和密码的数据类型将为 varchar。大小可以根据需要进行调整,但是 100 就足够了。
表的 SQL 代码:
sql
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL
)
html
0) : ?>
php
hostname,
// username, password, database name
$db = mysqli_connect('localhost', 'root', '', 'registration');
// Registration code
if (isset($_POST['reg_user'])) {
// Receiving the values entered and storing
// in the variables
// Data sanitization is done to prevent
// SQL injections
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// Ensuring that the user has not left any input field blank
// error messages will be displayed for every blank input
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
// Checking if the passwords match
}
// If the form is error free, then register the user
if (count($errors) == 0) {
// Password encryption to increase data security
$password = md5($password_1);
// Inserting data into table
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
// Storing username of the logged in user,
// in the session variable
$_SESSION['username'] = $username;
// Welcome message
$_SESSION['success'] = "You have logged in";
// Page on which the user will be
// redirected after logging in
header('location: index.php');
}
}
// User login
if (isset($_POST['login_user'])) {
// Data sanitization to prevent SQL injection
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
// Error message if the input field is left blank
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
// Checking for the errors
if (count($errors) == 0) {
// Password matching
$password = md5($password);
$query = "SELECT * FROM users WHERE username=
'$username' AND password='$password'";
$results = mysqli_query($db, $query);
// $results = 1 means that one user with the
// entered username exists
if (mysqli_num_rows($results) == 1) {
// Storing username in session variable
$_SESSION['username'] = $username;
// Welcome message
$_SESSION['success'] = "You have logged in!";
// Page on which the user is sent
// to after logging in
header('location: index.php');
}
else {
// If the username and password doesn't match
array_push($errors, "Username or password incorrect");
}
}
}
?>
html
Login and Registration
System - LAMP Stack
Login Here!
php
Registration system PHP and MySQL
Register
html
Homepage
Home Page
CSS
* {
margin: 0px;
padding: 0px;
}
body {
font-size: 120%;
background: #F8F8FF;
}
.header {
width: 30%;
margin: 50px auto 0px;
color: white;
background: #5F9EA0;
text-align: center;
border: 1px solid #B0C4DE;
border-bottom: none;
border-radius: 10px 10px 0px 0px;
padding: 20px;
}
form, .content {
width: 30%;
margin: 0px auto;
padding: 20px;
border: 1px solid #B0C4DE;
background: white;
border-radius: 0px 0px 10px 10px;
}
.input-group {
margin: 10px 10px 10px 10px;
}
.input-group label {
display: block;
text-align: left;
margin: 5px;
font-size: 20px;
}
.input-group input {
height: 32px;
width: 95%;
padding: 5px 10px;
font-size: 15px;
border-radius: 10px;
border: 1px solid gray;
}
.btn {
cursor: pointer;
padding: 12px;
font-size: 16px;
color: white;
background: #23585a;
border: none;
border-radius: 10px;
}
.error {
width: 92%;
margin: 0px auto;
padding: 10px;
border: 1px solid #a94442;
color: #a94442;
background: #f2dede;
border-radius: 5px;
text-align: left;
}
.success {
color: #3c763d;
background: #dff0d8;
border: 1px solid #3c763d;
margin-bottom: 20px;
}
错误。 PHP
html
0) : ?>
解释:错误。 PHP文件负责保存系统的错误信息。假设用户输入了错误的用户名和密码组合,那么在这种情况下,错误消息将存储在 $error 变量中,然后将使用 ‘echo; 显示给用户; PHP 的函数。
服务器。 PHP
PHP
hostname,
// username, password, database name
$db = mysqli_connect('localhost', 'root', '', 'registration');
// Registration code
if (isset($_POST['reg_user'])) {
// Receiving the values entered and storing
// in the variables
// Data sanitization is done to prevent
// SQL injections
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// Ensuring that the user has not left any input field blank
// error messages will be displayed for every blank input
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
// Checking if the passwords match
}
// If the form is error free, then register the user
if (count($errors) == 0) {
// Password encryption to increase data security
$password = md5($password_1);
// Inserting data into table
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
// Storing username of the logged in user,
// in the session variable
$_SESSION['username'] = $username;
// Welcome message
$_SESSION['success'] = "You have logged in";
// Page on which the user will be
// redirected after logging in
header('location: index.php');
}
}
// User login
if (isset($_POST['login_user'])) {
// Data sanitization to prevent SQL injection
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
// Error message if the input field is left blank
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
// Checking for the errors
if (count($errors) == 0) {
// Password matching
$password = md5($password);
$query = "SELECT * FROM users WHERE username=
'$username' AND password='$password'";
$results = mysqli_query($db, $query);
// $results = 1 means that one user with the
// entered username exists
if (mysqli_num_rows($results) == 1) {
// Storing username in session variable
$_SESSION['username'] = $username;
// Welcome message
$_SESSION['success'] = "You have logged in!";
// Page on which the user is sent
// to after logging in
header('location: index.php');
}
else {
// If the username and password doesn't match
array_push($errors, "Username or password incorrect");
}
}
}
?>
说明:会话是使用 session_start() 方法启动的。之后,声明变量并创建错误数组。它将存储所有错误消息。服务器。 PHP页面然后连接到之前创建的“注册”数据库。用户点击注册表上的“注册”按钮后。 PHP按钮,将输入的数据发送到数据库,这样就完成了一次新的注册。但是,在此之前完成表单验证以确保用户正确填写表单。所有字段都是必需的,不能留空。
第 18 – 21 行: mysqli_real_escape_string在将数据发送到数据库之前对特殊字符进行转义。这对于 SQL 注入的数据库安全至关重要。
第 25 – 27行:这些行确保用户填写所有输入框,以及“密码”和“确认密码”是否匹配。如果两个密码都匹配,则代码进一步运行。
第 29 – 32 行:检查密码是否匹配。
第 35 – 46 行:如果到这一点为止的错误数为零,则密码将被“md5”加密,并将输入的数据发送到数据库。注册过程完成后,用户名存储在会话变量中,用户被重定向到索引。 PHP页面,要求他输入登录凭据。
第 50 – 80 行:首先在 sanitized 中输入用户名和密码。这对于提高数据库安全性至关重要,因为它消除了任何 SQL 注入的机会。如果用户名或密码字段留空,用户会收到错误消息。
如果发现在此代码点之前的错误数为 0,则运行数据库检查。如果发现用户输入的用户名存在于数据库中,则用户成功登录。然后将用户重定向到“index.html”。 PHP’ 页面。
登录。 PHP
html
Login and Registration
System - LAMP Stack
Login Here!
说明:系统登录页面。用户必须输入用户名和密码才能成功登录。按下登录按钮后,登录代码写入服务器。 PHP页面运行,它完成所有后端工作,比如检查用户名和密码是否匹配。
登记。 PHP
PHP
Registration system PHP and MySQL
Register
说明:该页面包含注册页面的HTML编码。服务器。 PHP’ 和 ‘ 错误。 PHP’ 页面分别包含在第 01 行和第 15 行中。这是使注册系统的后端工作所必需的。要求用户输入用户名、电子邮件和密码以创建帐户。填写完输入字段后,输入的数据将发送到数据库表中。
指数。 PHP
html
Homepage
Home Page
解释:
第 01 – 19 行:存储在会话变量中的用户名现在显示回给用户。可以使用unset($_SESSION[“products”])或session_destroy()销毁此会话变量。但是, session_destroy()将立即销毁所有会话变量。要仅销毁 ‘username’ 会话变量,最好使用unset($_SESSION[“products”])取消设置变量。
第 34 – 42 行:这确保只有登录的用户才能访问此页面。
第 45 – 50 行:这会在用户登录后向用户显示个性化的欢迎消息。
CSS文件
CSS
* {
margin: 0px;
padding: 0px;
}
body {
font-size: 120%;
background: #F8F8FF;
}
.header {
width: 30%;
margin: 50px auto 0px;
color: white;
background: #5F9EA0;
text-align: center;
border: 1px solid #B0C4DE;
border-bottom: none;
border-radius: 10px 10px 0px 0px;
padding: 20px;
}
form, .content {
width: 30%;
margin: 0px auto;
padding: 20px;
border: 1px solid #B0C4DE;
background: white;
border-radius: 0px 0px 10px 10px;
}
.input-group {
margin: 10px 10px 10px 10px;
}
.input-group label {
display: block;
text-align: left;
margin: 5px;
font-size: 20px;
}
.input-group input {
height: 32px;
width: 95%;
padding: 5px 10px;
font-size: 15px;
border-radius: 10px;
border: 1px solid gray;
}
.btn {
cursor: pointer;
padding: 12px;
font-size: 16px;
color: white;
background: #23585a;
border: none;
border-radius: 10px;
}
.error {
width: 92%;
margin: 0px auto;
padding: 10px;
border: 1px solid #a94442;
color: #a94442;
background: #f2dede;
border-radius: 5px;
text-align: left;
}
.success {
color: #3c763d;
background: #dff0d8;
border: 1px solid #3c763d;
margin-bottom: 20px;
}
图片表示:
如何运行这个项目?
该项目的源代码可以从这个 GitHub 存储库中获得。
下载并解压项目后,按照给定的步骤运行程序:
- 下载所有文件,或将存储库克隆到本地系统中。
- 创建一个名为“registration”的数据库和一个名为“users”的表。上面已经提供了表的MySQL代码。
- 使用 XAMP 或 WAMP 在本地主机上运行系统。
- 确保运行 Apache 和 MySQL 服务器所需的端口是空闲的。如果没有,那么您将不得不更改端口号。
HTML 是网页的基础,用于通过构建网站和 Web 应用程序进行网页开发。您可以按照此 HTML 教程和 HTML 示例从头开始学习 HTML。
PHP是一种专门为 Web 开发设计的服务器端脚本语言。您可以按照此PHP教程和PHP示例从头开始学习PHP 。