php - 在其他页面 PHP MySql 中获取会话数据
问题描述
请注意,我对 php 和 mysql 非常陌生,我在互联网上找到了登录/注册脚本,现在我试图弄清楚,所以我的问题是,我如何从数据库中获取用户数据并将其显示在其他页面中?请注意,我正在使用会话。
连接到数据库:
$db = mysqli_connect('localhost', 'root', '', 'registration');
注册时设置数据:
$query = "INSERT INTO users (ip, username, email, password)
VALUES('".$_SERVER['REMOTE_ADDR']."', '$username', '$email', '$password')";
完整代码可以在这里找到:
<?php
session_start();
// initializing variables
$ip = "";
$username = "";
$email = "";
$errors = array();
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'registration');
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$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']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
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");
}
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "Email already exists");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO users (ip, username, email, password)
VALUES('".$_SERVER['REMOTE_ADDR']."', '$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}
// ...
// ...
// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}else {
array_push($errors, "Wrong username or password combination");
}
}
}
解决方案
推荐阅读
- python - 程序结束后如何删除按钮?
- javascript - ISO-8601 中的新日期抛出“无效日期”
- c# - Heroku 在尝试发布 .NET Core 应用程序时不会检测到预发布的不同包来源的 NuGet 库
- mysql - 为什么局部变量不保存价值?
- multithreading - 为什么在内核空间中实现的线程很慢?
- angular - 如何使用 FormControl.valueChanges 格式化输入值
- jenkins - 如何在Jenkins管道中替换ant项目的build.xml中的变量?
- lua - 语法末尾的LUA变量?
- node.js - 为什么这个 Mongo 查询返回奇怪的结果?
- c# - 如何通过 WindowsFormsHost 启动 Python Shell 或 Python IDLE?