正在从 MYSQL 数据库中检索用户名
Retrieving username from MYSQL database
已回答的问题:
So I've made a login and signup feature to my website, but now I would
like to add the username's and ranks to the sidebar.. But I can't
figure out how to do this, and this is why I am asking you for help.
Signup.inc.php:
<?php
session_start();
include '../dbh.php';
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
if (empty($fname)) {
header("Location: ../parts/signup.php?error=empty");
exit();
}
if (empty($lname)) {
header("Location: ../parts/signup.php?error=empty");
exit();
}
if (empty($uid)) {
header("Location: ../parts/signup.php?error=empty");
exit();
}
if (empty($pwd)) {
header("Location: ../parts/signup.php?error=empty");
exit();
} else {
$sql = "SELECT uid FROM users WHERE uid='$uid'";
$result = mysqli_query($conn, $sql);
$uidcheck = mysqli_num_rows($result);
if ($uidcheck > 0) {
header("Location: ../parts/signup.php?error=username");
exit();
} else {
$enpwd = password_hash($pwd, PASSWORD_DEFAULT);
$sql = "UPDATE users (fname, lname, uid, pwd)
VALUES ('$fname', '$lname', '$uid', '$enpwd')";
$result = mysqli_query($conn, $sql);
header("Location: ../index.php");}
}
----- Login.inc.php :
<?php
session_start();
include '../dbh.php';
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM users WHERE uid='$uid'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$hash_pwd = $row['pwd'];
$hash = password_verify($pwd, $hash_pwd);
if ($hash == 0 ) {
header("Location: ../index.php?error=empty");
} else {
$sql = "SELECT * FROM users WHERE uid='$uid' AND pwd='$hash_pwd'";
$result = mysqli_query($conn, $sql);
if (!$row = mysqli_fetch_assoc($result)) {
echo "Your username or password is incorrect!";
} else {
$_SESSION['id'] = $row['id'];
}
header("Location: ../pages/dashboard.php");
}
?>
--------- Sidebar:
<?php
session_start();
if(!isset($_SESSION['id'])){ //if login in session is not set
header("Location: ../index.php");
}
include '../include/idtouser.sinc.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>LoginSystem</title>
<link rel="stylesheet" href="../css/global.css" type="text/css">
</head>
<body>
<header id="closeNav">
<nav>
<div onclick="toggleNav()" id="star"><span class="hamburgerico">☰</span></div>
<div class="profilePicture"></div>
<div id="username">Username</div>
<div id="rank"><p>Rank</p></div>
<ul>
<li><a href="#">Dashboard</a></li>
<li><a href="#">Projects</a></li>
<li><a href="#">Placeholder</a></li>
<li><a href="#">Placeholder</a></li>
<li><a href="#">Placeholder</a></li>
<li><a href="../include/logout.inc.php">Log Out</a></li>
</ul>
</nav>
</header>
What would be the correct way for me to do this?
未回答的问题:
并且——有没有办法在将我的排名 (1,2,3,4,5) 发布到侧边栏之前将它们转换为实际排名名称?
在登录页面中,添加:
$_SESSION['uid'] = $row[1];
假设行[1]包括用户id或用户名
然后在边栏中:
<?php echo $_SESSION['uid']; ?>
我不知道等级是干什么用的,但你可以用与用户名类似的方式回显等级
已回答的问题:
So I've made a login and signup feature to my website, but now I would like to add the username's and ranks to the sidebar.. But I can't figure out how to do this, and this is why I am asking you for help. Signup.inc.php:
<?php session_start(); include '../dbh.php'; $fname = $_POST['fname']; $lname = $_POST['lname']; $uid = $_POST['uid']; $pwd = $_POST['pwd']; if (empty($fname)) { header("Location: ../parts/signup.php?error=empty"); exit(); } if (empty($lname)) { header("Location: ../parts/signup.php?error=empty"); exit(); } if (empty($uid)) { header("Location: ../parts/signup.php?error=empty"); exit(); } if (empty($pwd)) { header("Location: ../parts/signup.php?error=empty"); exit(); } else { $sql = "SELECT uid FROM users WHERE uid='$uid'"; $result = mysqli_query($conn, $sql); $uidcheck = mysqli_num_rows($result); if ($uidcheck > 0) { header("Location: ../parts/signup.php?error=username"); exit(); } else { $enpwd = password_hash($pwd, PASSWORD_DEFAULT); $sql = "UPDATE users (fname, lname, uid, pwd) VALUES ('$fname', '$lname', '$uid', '$enpwd')"; $result = mysqli_query($conn, $sql); header("Location: ../index.php");} }
----- Login.inc.php :
<?php session_start(); include '../dbh.php'; $uid = $_POST['uid']; $pwd = $_POST['pwd']; $sql = "SELECT * FROM users WHERE uid='$uid'"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); $hash_pwd = $row['pwd']; $hash = password_verify($pwd, $hash_pwd); if ($hash == 0 ) { header("Location: ../index.php?error=empty"); } else { $sql = "SELECT * FROM users WHERE uid='$uid' AND pwd='$hash_pwd'"; $result = mysqli_query($conn, $sql); if (!$row = mysqli_fetch_assoc($result)) { echo "Your username or password is incorrect!"; } else { $_SESSION['id'] = $row['id']; } header("Location: ../pages/dashboard.php"); } ?>
--------- Sidebar:
<?php session_start(); if(!isset($_SESSION['id'])){ //if login in session is not set header("Location: ../index.php"); } include '../include/idtouser.sinc.php'; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>LoginSystem</title> <link rel="stylesheet" href="../css/global.css" type="text/css"> </head> <body> <header id="closeNav"> <nav> <div onclick="toggleNav()" id="star"><span class="hamburgerico">☰</span></div> <div class="profilePicture"></div> <div id="username">Username</div> <div id="rank"><p>Rank</p></div> <ul> <li><a href="#">Dashboard</a></li> <li><a href="#">Projects</a></li> <li><a href="#">Placeholder</a></li> <li><a href="#">Placeholder</a></li> <li><a href="#">Placeholder</a></li> <li><a href="../include/logout.inc.php">Log Out</a></li> </ul> </nav> </header>
What would be the correct way for me to do this?
未回答的问题: 并且——有没有办法在将我的排名 (1,2,3,4,5) 发布到侧边栏之前将它们转换为实际排名名称?
在登录页面中,添加:
$_SESSION['uid'] = $row[1];
假设行[1]包括用户id或用户名
然后在边栏中:
<?php echo $_SESSION['uid']; ?>
我不知道等级是干什么用的,但你可以用与用户名类似的方式回显等级