PHP会话开始不起作用?

时间:2014-01-22 23:05:20

标签: php session authentication login web

我正在制作一个允许用户登录的网站,所以当我登录时会登录,但是如果我刷新页面会话结束了吗?我知道不应该这样做。基本上我的问题是我的页面在刷新页面时正在破坏会话。 任何帮助将不胜感激!

这是我的主要登录代码:

if(isset($_POST['username'])&&isset($_POST['password'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $password_hash = md5($password);

    if (!empty($username)&&!empty($password)) {
        $query = "SELECT * FROM employees WHERE username = '$username' AND password = '$password_hash'";
        $query_run = mysql_query($query);
        $mysql_num_rows = mysql_num_rows($query_run);

        if ($mysql_num_rows == 0) {
            echo 'Unable to log in, username and/or password does not match!'.'<br><br>';
            echo '<a href = "#">Try Again!</a>';
        } else if ($mysql_num_rows == 1) {
            $user_id = mysql_result($query_run, 0, 'id');
            $first_name = mysql_result($query_run, 0, 'firstname');
            $last_name = mysql_result($query_run, 0, 'lastname');
            $user_name = mysql_result($query_run, 0, 'username');
            $pass_word = mysql_result($query_run, 0, 'password');
            $email = mysql_result($query_run, 0, 'email');
            $grade = mysql_result($query_run, 0, 'Security Clearance');
            $_SESSION['user_id']=$user_name;
        }
    }
}

继承我的core.php

    ob_start();
session_start();

function loggedin() {
    if(isset($_SESSION['user_id'])&&!empty($_SESSION['user_id'])) {
        return true;
    } else {
        return false;
    }
}

继承我的索引页面(用户在登录时看到的页面)

    require 'core.php';
    require 'connect.inc.php'; 
    // The page to connect to my MySQL db
include 'main_login.inc.php';

    if (loggedin()) {
    // if logged in : do stuff  
    }
} else { 
    // if its not logged in : redirect
    // If User Is Not Logged: Redirect To Jamie Co Home
    header('Location: #');
}

1 个答案:

答案 0 :(得分:0)

我已经通过将会话更改为令牌解决了这个问题,我不确定为什么会这样,但确实如此!