即使使用session_start(),PHP会话也不会在标头之后携带;在每一页上

时间:2013-06-09 20:43:33

标签: php session

我知道这个问题很常见,通常的答案是将session_start;放在每个页面和脚本的开头。我已经做到了,但仍然没有占上风。我花了整整6个小时试图找到错误,但无济于事,任何指针都会受到赞赏。

相关代码如下,但只是为了分解。有一个 init.php 文件,其中包含所有函数,连接和session_start();,这是include每个页面的顶部, 之前任何其他代码。


init.php header.php 中的include d,任何HTML之前的 [已包含在内错误报告]

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'database/connect.php'; //code for connecting to database
require 'functions/general.php'; //contains one sanitize function
require 'functions/users.php'; //user-specific functions (see below)

$errors = array();
?>
//rest of the head and opening body tag

index.php session_start();位于 init.php 代码的开头

<?php include 'includes/overall/header.php'; ?>
<?php include 'assets/nivo/nivo.php'; ?>
<p>plain text
</p>
<?php
echo ($_SESSION['user_id']);
?>

<?php include 'includes/overall/footer.php'; ?>
<?php include 'includes/overall/scripts.php'; ?>
</body>
</html>

core / functions / users.php (在 init.php 中包含session_start();

<?php
function logged_in() {
    return (isset($_SESSION['user_id'])) ? true : false;
}

function user_exist($username) {
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `user` WHERE `username` = '$username'"), 0) == 1) ? true : false;
};
function user_active($username) {
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `user` WHERE `username` = '$username' AND `active` = 1"), 0) == 1) ? true : false;
};

function user_id_from_username($username) {
    $username = sanitize($username);
    return mysql_result(mysql_query("SELECT `user_id` FROM `user` WHERE `username` = '$username'"), 0, 'user_id');
};

function login($username, $password) {
    $user_id = user_id_from_username($username);
    $username = sanitize($username);
    $password = md5($password);

    return  (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `user` WHERE `username` = '$username' AND `password` = '$password'"), 0) == 1) ? $user_id : false;
};
?>

登录表单为included index.php ,并将数据发送到 login_pro.php

login_pro.php

<?php
include 'core/init.php';
if (empty($_POST) === false) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (empty($username) === true || empty($password) === true) {
        $errors[] = 'Please enter a username and password';
    } else if (user_exist($username) === false) {
        $errors[] = 'User does not exist';
    } else if (user_active($username) === false) {
        $errors[] = 'Please activate your account';
    } else {
        $login = login($username, $password);
        if ($login === false) {
            $errors[] = 'This username/password combination is incorrect';
        } else {
            $_SESSION['user_id'] = $login;
            header("Location: index.php");
            exit();
        };
    };

    print_r($errors);
}
?>

如果我将die函数输出到 login_pro.php 中输出SESSION['user_id'];,就像这样:

} else {
                $_SESSION['user_id'] = $login;
                die($_SESSION['user_id']);
                header("Location: index.php");
                exit();
            };

获取所需的user_id。但是在标题重定向回index.php之后,这并没有继续进行 - 正如 index.php

中echo函数缺少输出所示

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我现在已经解决了这个问题。它与我的托管服务提供商有关。我已经发布了一个完整的解释来帮助其他人here。如果有人认为这个问题应该删除,请随意,我对Stackoverflow的传统不是很熟悉。