无法启动用户的会话

时间:2014-01-30 11:58:09

标签: php session logging

我无法启动会话,即使此代码在其他网站中正确运行,但在我现在构建的网站中它将无法正常工作。这是代码:

if(isset($_POST["username"])&& isset($_POST["password"])) {
    $manager = preg_replace('#[^A-Za-z0-9\@\-\_\|\,\.\\p{L}\\\\]#i', '', $_POST["username"]);//filter everything but numbers and letters
    $password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]);// filter everything but numbers and letters
    // Connect to the Mysql database
    $q = "SELECT * FROM users WHERE username='$manager' AND password='$password' LIMIT 1";
    $r = mysqli_query($dbc, $q);
    $userCount = mysqli_num_rows($r);

    if($userCount == 1 ) {

            while ($user = mysqli_fetch_assoc($r)) {

                $id = $user['id'];
                $pass = $user['password'];  
                $us = $user['username'];

            }

            $_SESSION["id"] = $id;
            $_SESSION["manager"] = $us;
            $_SESSION["password"] = $pass;
            header("location:index.php?page=business");
            exit(); 

    } else { 
        echo 'That information is incorrect,try again <a href="index.php">Click Here</a>';
    }   

} 

直到这部分代码

$id = $user['id'];
$pass = $user['password'];  
$us = $user['username'];

一切正常,但在我设置会话变量之后:

$_SESSION["id"] = $id;
$_SESSION["manager"] = $us;
$_SESSION["password"] = $pass;
header("location:index.php?page=business");
exit(); 

没有找到会话,任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

在PHP中使用会话时,需要session_start()才能访问$ _SESSION全局。

您使用此功能吗?如果没有,请将其放入并测试。

答案 1 :(得分:0)

不要忘记使用会话开始,如下所示:

session_cache_expire(20);
        session_start(); 
        session_unregister($username_session);
        session_register($username_session);

$_SESSION[$start_session] = time();
        $validation = true;
        $_SESSION[$username_session] = $user;

我的所有网站,我都使用此代码进行登录,您可以尝试在登录时使用!

if ($_POST['btnLogin']) {

$texto = mysql_query("SELECT user, password from " . $site . "User");


$user = $_POST["user"];
$pwd = md5($_POST["pwd"]);

$users = array();
$pwds = array();

while ($row = mysql_fetch_array($texto)) {
    array_push($users, $row['user']);
    array_push($pwds, $row['password']);
}
$size = count($users);
$validation = false;
$i = 0;
do {
    if ($user == $users[$i] && $pwd == $pwds[$i]) {
        session_cache_expire(20);
        session_start(); // NEVER FORGET TO START THE <strong class="highlight">SESSION</strong>!!!
        session_unregister($username_session);
        session_register($username_session);
        $inactive = 1200;
        if (isset($_SESSION[$start_session])) {
            $session_life = time() - $_SESSION[$start_session];
            if ($session_life > $inactive) {
                header("Location: login.php");
            }
        }
        $_SESSION[$start_session] = time();
        $validation = true;
        $_SESSION[$username_session] = $user;

        header("Location: index.php");
    }
    $i++;
} while ($i < $size && $validation == false);
if ($validation == false) {
    $mensagemerro = 'Login Error!';
}
if (!mysql_close($conn)) {
    die('Could not close BD:' . mysql_error());
   }
}
?>

答案 2 :(得分:0)

当你打算使用SESSION变量时,这意味着你将开始或恢复一个SESSION。

所以为了在进程

之前启动或恢复SESSION使用session_start()