0
votes

PHP session value lost after header redirection in php

Our code Login.php

    <?php
session_start();
include('./includes/variables.php');
include_once('includes/custom-functions.php');
$fn = new custom_functions;

if (isset($_POST['btnLogin'])) {

    // get username and password
    $username = $db->escapeString($fn->xss_clean($_POST['username']));
    $password = $db->escapeString($fn->xss_clean($_POST['password']));

    // set time for session timeout
    $currentTime = time() + 25200;
    $expired = 3600;

    // create array variable to handle error
    $error = array();

    // check whether $username is empty or not
    if (empty($username)) {
        $error['username'] = "*Username should be filled.";
    }

    // check whether $password is empty or not
    if (empty($password)) {
        $error['password'] = "*Password should be filled.";
    }

    // if username and password is not empty, check in database
    if (!empty($username) && !empty($password)) {

        // change username to lowercase
        $username = strtolower($username);
        //encript password to sha256
        //$password = md5($password);

        // get data from user table
        $sql_query = "SELECT * FROM admin WHERE username = '" . $username . "' AND password = '" . $password . "'";
        
        $db->sql($sql_query);
        /* store result */
        $res = $db->getResult();
//      print_r($res);
//      die();
        $num = $db->numRows($res);
        // Close statement object
        if ($num == 1) {
            $_SESSION['id'] = $res[0]['id'];
            $_SESSION['role'] = $res[0]['role'];
            $_SESSION['user'] = $username;
            $_SESSION['timeout'] = $currentTime + $expired;
            //print_r($_SESSION);
            //die();
            header("location: home.php");
            exit();
        } else {
            $error['failed'] = "<span class='label label-danger'>Invalid Username or Password!</span>";
        }
    }
}
?>

Home.php

<?php session_start();
 print_r($_SESSION);
    
   
    ?>

Output : array()

We tried the following method

  1. Made sure session_start(); is called before any sessions are being called
  2. After the header redirect, end the current script using exit();
  3. Made sure cookies are enabled in the browser we were using to test it on.
  4. Made sure didn't delete or empty the session
  5. Made sure file extension is .php
1
Are you using the session_start() in the page where you are setting $_SESSION array ? - Kishen Nagaraju
yes, in the first line of the page. - Linu S
Check your log for "Headers already sent" warnings, and fix the problem that causes them. - Barmar
we checked display errors already , it shows errors of blank session values but not showing why session is not activated/working! - Linu S

1 Answers

0
votes

You have to include you file in which you have initialized session For example first file named phpcodeonly.php:

session_start() //put it in start
if(login success){

$_SESSION['email']= $email
}

your other file.php:

include 'phpcodeonly.php'; //on top

<h1> Welcome <?php echo  $_SESSION['email']?> </h1>