简体   繁体   中英

PHP session do not carry after header even with session_start(); on every page

I know this problem is very common, and the usual answer is to place session_start; at the beginning of every page and script. I've done that and still to no prevail. I've spent literally a whole 6 hours trying to find the mistake, but came to no avail, any pointers would be appreciated.

The relevant codes are below, but just to break it down. There is an init.php file that contains all the functions, connections and session_start(); and this is include d into the top of every page, before any other code.


init.php ( include d in header.php , before any HTML) [EDITED to include error reporting]

<?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(); is at the beginning of the code from 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 (within init.php which contains 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;
};
?>

The login form is included into index.php and sends data to 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);
}
?>

If I place a die function to output SESSION['user_id']; into login_pro.php , like so:

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

I do get the desired user_id. But this is not carried forward after the header redirected me back to index.php - as indicated by the lack of output in the echo function at index.php

How do I fix this?

I have now resolved this problem. It has to do with my hosting provider. I have posted a full explanation to help others here . If someone deem this question should be deleted, please feel free, I am not very familiar with Stackoverflow traditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM