$ _SESSION variables do not work, do not work on other pages

I created a session in

ENTRANCE PAGE

   <?php
session_start();
include 'dbconfig.php';
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM login WHERE username = '$username' AND password= '$password' ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $_SESSION["userid"] = $row['user'];
        echo $_SESSION["userid"];
    }
} else {
    echo "wrong";
}
$conn->close();
?> 

LOGIN FETCHING

function login() {

    var username = $("#username").val();
    var password = $("#password").val();
    if (username == "" || username == null || username == undefined || password == "" || password == null || password == undefined) {
        $('#foremptyvalue').show();
    } else {
        $('#foremptyvalue').hide();
        $('#loader').show();
        jQuery.ajax({
            url: baseurl + "login.php"
            , data: 'username=' + username + '&password=' + password
            , type: "POST"
            , success: function (response) {
                response = $.trim(response);
                if (response == "wrong") {
                    $('#loader').hide();
                    $('#forwronginput').show();
                    $("#username").val('');
                    $("#password").val('');
                } else {
                    $('#loader').hide();
                    $('#forwronginput').hide();
                    location.href = "account.php?id=" + response;
                }

            }
            , error: function () {}
        });
    }

}

header.php

The session variable is not working on this page. I used the code below:

<?php
session_start();
echo session_id();
$session = $_SESSION["userid"];
?>

RESULT BELOW

ptarbkn67poq1gkch2dh6fvqc3 Note : Undefined index: userid in C: \ xampp \ htdocs \ feecounter \ header.php on line 4

I also check if the session save path is available or not, and it is writable. it saves the session to C: \ xampp \ tmp

+4
source share
2 answers

. ... , :

    <?php
    // THIS SHOULD BE THE VERY FIRST LINES OF CODE IN YOUR SCRIPTS
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }
+1

header.php : -

<?php
if (!session_id()) {
    session_start();
}

if(!empty($_SESSION['userid'])){
   echo $_SESSION['userid'];
}else{
   echo 'guest user';
}
+1

All Articles