// 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 register WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
}
$qry = "SELECT * from register";
$result = mysqli_query($qry);
$row = mysqli_fetch_array($result);
$id = $row[0];
if (mysqli_num_rows($results) == 1) {
//here i want to change session from username to id
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
?>
此代码也可以工作,但我想将用户名 session 替换为 id session 。请帮助我如何用 id 替换它。 在这里我想将 session 设置为用户名的 id。所以请帮助我如何获得代码的解决方案。
最佳答案
只需复制此代码: 替换您的代码
$_SESSION['username'] = $username;
到
$_SESSION['id'] = $username;
看起来像:
if (mysqli_num_rows($results) == 1) {
//here i want to change session from username to id
$_SESSION['id'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}else {
array_push($errors, "Wrong username/password combination");
}
关于php - 如何在php中将 session 从用户名更改为id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51442552/