[英]Session won't work
我對登錄用戶的會話有問題,因為用戶沒有登錄,所以會一直顯示。
的index.php
<?php
session_start();
if (!isset($_SESSION['email'])) {
if (isset($_POST['email']) && isset($_POST['pass'])) {
$email = mysqli_real_escape_string($con, $_POST['email']);
$pass = mysqli_real_escape_string($con, $_POST['pass']);
$sqli = "SELECT * FROM users WHERE email='$email' AND pass='$pass' LIMIT 1";
$result = mysqli_query($con, $sqli);
if ($result && mysqli_num_rows($result) == 1) {
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
header("Location: home.php?=$id");
exit();
}
}
}
} else {
header("Location: home.php");
}
?>
<form action="index.php" method="POST">
<input name="email" type="text"/>
<input name="pass" type="password"/>
<input type="submit" name="submit" value="Submit">
</form>
home.php有:
<?php
session_start();
if (!isset($_SESSION['email'])) {
header("Location: index.php");
} else {
echo "welcome";
}
?>
現在,當我登錄時,我轉到home.php,但在那之后,home.php將我重定向到index.php,就像會話從未開始...
請參閱底部代碼中的注釋。 您忘記在POST
后設置會話,並且此處不需要while
循環:
<?php
session_start();
if (!isset($_SESSION['email'])) {
if (isset($_POST['email']) && isset($_POST['pass'])) {
$email = mysqli_real_escape_string($con, $_POST['email']);
$pass = mysqli_real_escape_string($con, $_POST['pass']);
$sqli = "SELECT * FROM users WHERE email='$email' AND pass='$pass' LIMIT 1";
$result = mysqli_query($con, $sqli);
if ($result && mysqli_num_rows($result) == 1) {
/* Authenticated User */
/* Since you are getting only one row, you don't need a `while` loop. */
$row = mysqli_fetch_array($result);
$id = $row['id'];
/* Actually set the session variable. */
$_SESSION["email"] = $id;
header("Location: home.php?=$id");
exit();
}
}
} else {
header("Location: home.php");
}
?>
筆記:
$_SESSION
值。 您只需檢查會話是否存在,您需要設置會話數組的值。
$_SESSION['value'] = 'Item';
然后檢索它
echo $_SESSION['value']
希望有所幫助。
在成功登錄后離開home.php
之前,您需要設置一個SESSION
來獲取您的index.php
中login page
中的值。
為此,在header("Location: home.php?=$id");
之前添加$_SESSION['email'] = $id
header("Location: home.php?=$id");
在登錄頁面( index.php
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.