Using header to move between pages

I have been using header location to move between my php pages so that the url displayed in the address bar does not change.

When doing so, I get the following error

Warning: session_start(): Cannot send session cache limiter - headers already sent

<?php header('Location: index.php'); ?>

This is what I am using to get me to the account page.

<?php

    if(!isset($_SESSION)) 
    { 
        session_start(); 
    }


/**
 * Check if the user is logged in.
 */
if(!isset($_SESSION['user_id']) || !isset($_SESSION['logged_in'])){
    //User not logged in. Redirect them back to the login.php page.
    header('Location: index.php');
    exit;
}
require_once 'function.php';
?>

And this is the code that seems to be causing the problem.

The above code is in the header and the header is also in the account page that is being loaded.

Any help with this would be appreciated.

Thanks

Seems that there has been something outputted on page before the redirection happens and therefore cannot redirect. I think even a blank line outside the php tags in a php file can cause this

1 Like

Merged from previous topic:-

-----------------------------

This is great - Thanks so much for your help. Means a lot.

I have been moving my code around and have caused myself a bit of an issue.

I am now getting the following.

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Users/adamhewitt/Sites/registration/home.php:27) in /Users/adamhewitt/Sites/registration/header.php on line 5

<div>
<?php require_once 'header.php' ?>

<?php require_once 'footer.php' ?>


</div>

The first div is line 26

  if(!isset($_SESSION)) 
    { 
        session_start(); 
    }

Session start is line 5

Im not sure what I am doing wrong here

If you are using PHP 7 then try this at the top of each page because it is a more strict and gives more error/warning information:.

<?php

# MAXIMUM ERROR REPORTING
	declare(strict_types=1); // PHP 7 specific AND ONLY FILE WIDE
	error_reporting(-1);
	ini_set('display_errrors', 'TRUE');

# TEST SESSION	
  if(!isset($_SESSION)) 
  { 
    session_start(); 
  }
// remaining script 
// DO NOT CLOSE WITH ?>

This part of your error message tells you where you sent some output to the screen, prior to trying to call session_start(). What is on line 27 of home.php? If that code starting with <div> is the home.php file, then there’s your answer - you’ve sent an opening <div> to the browser before you included your header.php file. You must start the session before you send anything to the browser. This is because the session_start() function tries to send information in the page header, and as the name suggests, the page header is sent before the page content. As soon as you send something that will be page content, your header is already gone and you can’t add more to it.

2 Likes

If there is a <div> then presumably there will be at least DOCTYPE, head and body tags somewhere before that.

1 Like

And as @John_Betong suggested it is good practice to not close your PHP tags on your PHP scripts that don’t have any HTML. If you have any PHP include that features an empty line after the closing PHP tag then you will get this problem

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.