我遇到记录用户的会话问题,它总是在用户未登录时显示。
的index.php
browser.get('https://www.google.co.uk/');
expect(element.all(by.xpath("//a[text()='Terms']")).count()).toBeGreaterThan(0);
home.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,但在那之后,home.php将我重定向到index.php,就像会话从未开始...
答案 0 :(得分:2)
请参阅底部代码中的注释。您忘记在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
值。答案 1 :(得分:1)
您只需检查会话是否存在,您需要设置会话数组的值。
$_SESSION['value'] = 'Item';
然后检索它
echo $_SESSION['value']
希望有所帮助。
答案 2 :(得分:0)
在您成功登录后离开home.php
之前,您需要设置一个SESSION
,以便在您的login page
index.php
中获取$_SESSION['email'] = $id
的值。
为此,请在登录页面header("Location: home.php?=$id");
中index.php
之前添加data<-data.frame(Var=c(1,2,3,4),
Fac1=rep(c("A","B"),2),
Fac2=rep(c("Blue","Red"),each=2))
require(rCharts)
n1 <- nPlot(Var~Fac2, group = "Fac1", data = data, type = "multiBarChart")
n1
。