0

I've explored the various solutions to this question on Stack Overflow though none seem to work for me.

Here is my problem. After running a php script on my website, I've added a header function to redirect me to another page like this:

header("Location: https://example.com/examplepage.php");
exit();

After this redirect, examplepage.php tells me that there are no session variables set (even though they are).

If I visit another page on the website after the redirect, the session variables seem to appear again and everything works fine again. So I'm assuming this is an issue with the header redirect.

Does anyone know what could be causing this issue?

Attempted Fixes

  • Including session_start() immediately after opening a php tag on all pages
  • Including exit() after the header redirect
  • Removing all whitespace before and after the <?php and ?> tags

PHP Snippet of examplepage.php

<?php
    session_start();
    include '../scripts/dbh.php';

    //checks if the user is logged in
    if((isset($_SESSION['id']))){
        //do stuff
    }
    else{
        //redirect user to login page as they are not logged in
        //this header is executed as it thinks $_SESSION['id'] is not set
        //note that this code is functional, only doesn't work after the header redirect mentioned
        header("Location: https://example.com/login");
    }
?>
Jaxon Crosmas
  • 437
  • 1
  • 4
  • 14

0 Answers0