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.
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
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
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.
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