1

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

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


init.php(在header.phpinclude中的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.phpsession_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 函数输出SESSION['user_id'];login_pro.php,如下所示:

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

确实得到了所需的 user_id。但是,在标头将我重定向回 index.php 后,这并没有结转 - 正如 index.php 的 echo 函数中缺少输出所表明的那样

我该如何解决?

4

1 回答 1

0

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

于 2013-06-21T19:11:58.563 回答