会议不会奏效

时间:2016-05-12 16:12:59

标签: php

我遇到记录用户的会话问题,它总是在用户未登录时显示。

的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,就像会话从未开始...

3 个答案:

答案 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");
}
?>

备注:

  1. 您需要设置$_SESSION值。
  2. 使用散列密码以提高安全性。

答案 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