我有一个基本的注册和登录表单,当用户注册时,他们的信息将被发送到数据库,当用户使用相同的用户名和密码时,他们就可以登录该网站。登录后,用户将被定向到帐户页面,我在其中尝试获取登录用户的信息,例如名字和姓氏。
账户.PHP
<?php
require("classes/functions.php");
if (!isset($_SESSION["user"])) {
header("location: login.php");
exit();
}
?>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-7 col-lg-4 mb-5 mb-lg-0 wow fadeIn">
<div class="card border-0 shadow">
<img src="" alt="...">
<div class="card-body p-1-9 p-xl-5">
<div class="mb-4">
<h3 class="h4 mb-0"><?php echo $_SESSION["user"];?></h3>
<span class="text-primary">CEO & Founder</span>
</div>
<ul class="list-unstyled mb-4">
<li class="mb-3"><a href="#!"><i class="far fa-envelope display-25 me-3 text-secondary"></i>[email protected]</a></li>
<li class="mb-3"><a href="#!"><i class="fas fa-mobile-alt display-25 me-3 text-secondary"></i>+012 (345) 6789</a></li>
<li><a href="#!"><i class="fas fa-map-marker-alt display-25 me-3 text-secondary"></i>205 Main Street, USA</a></li>
</ul>
<ul class="social-icon-style2 ps-0">
<li><a href="#!" class="rounded-3"><i class="fab fa-facebook-f"></i></a></li>
<li><a href="#!" class="rounded-3"><i class="fab fa-twitter"></i></a></li>
<li><a href="#!" class="rounded-3"><i class="fab fa-youtube"></i></a></li>
<li><a href="#!" class="rounded-3"><i class="fab fa-linkedin-in"></i></a></li>
</ul>
</div>
<a class="btn btn-primary" href="#" role="button">Edit Profile</a>
</div>
</div>
注册
function registerUser($fname, $surname, $email, $password, $confirm_password, $username){
$mysqli = connect();
$args = func_get_args();
$args = array_map(function($value){
return trim($value);
}, $args);
foreach ($args as $value) {
if (empty($value)) {
return "All fields are required";
}
}
foreach ($args as $value) {
if (preg_match("/([<|>])/", $value)) {
return "<> characters are not allowed";
}
}
$stmt = $mysqli->prepare("SELECT email FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
if ($data != NULL) {
return "Email already exists";
}
if (strlen($username) > 50) {
return "Username is to long";
}
$stmt = $mysqli->prepare("SELECT username FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
if ($data != NULL) {
return "Username already exists, please use a different username";
}
if (strlen($fname) > 50) {
return "First name is to long";
}
if (strlen($surname) > 50) {
return "Surname is to long";
}
if (strlen($password) > 50) {
return "Password is to long";
}
if ($password != $confirm_password) {
return "Password don't match";
}
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $mysqli->prepare("INSERT INTO users(fname, surname, email, password, username) VALUES(?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $fname, $surname, $email, $hashed_password, $username);
$stmt->execute();
if ($stmt->affected_rows != 1) {
return "An error occurred. Please try again";
}else{
return "success";
}
}
登录
function loginUser($username, $password){
$mysqli = connect();
$username = trim($username);
$password = trim($password);
if ($username == "" || $password == "") {
return "Both fields are required";
}
$username = filter_var($username, FILTER_SANITIZE_STRING);
$password = filter_var($password, FILTER_SANITIZE_STRING);
$sql = "SELECT username, password FROM users WHERE username = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
if ($data == NULL) {
return "Wrong username or password";
}
if (password_verify($password, $data["password"]) == FALSE) {
return "Wrong username or password";
}else{
$_SESSION['user'] = $username;
header("location: account.php");
exit();
}
}
我尝试了很多方法,包括谷歌搜索和在这里搜索,但没有任何效果。我希望显示登录用户的名字、姓氏、电子邮件地址等
提前致谢。
您必须从数据库中获取数据。您可以从您存储在login.php页面上的
$_SESSION["user"]
(但正如评论中提到的,记得在account.php顶部调用session_start()
)找到用户。
这是一个粗略的例子:
<?php
session_start();
require("classes/functions.php");
if (!isset($_SESSION["user"])) {
header("location: login.php");
exit();
}
$sql = "SELECT * FROM users WHERE username = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $_SESSION["user"]);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
?>
然后在 HTML 中使用
$data
显示用户信息。