Jos
Jos

Reputation: 21

Header doesn't work in combination with session_start?

To avoid illegal use I check the login status at the beginning of the code. I do this as follows:

if (!isset($_SESSION['loggedin'])){
  header('Location:http://www.name.nl/prg/login.php');
  exit();
} 

This works. But if I use this code first it doesn't work.

ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT'])).'/name.nl/tmp');
session_start();
if (!isset($_SESSION['loggedin'])){
  header('Location:http://www.name.nl/prg/login.php');
  exit();
} 

Am I missing something? I looked for 2 days now, but can't find a reason/solution. It seems to me that the header function should work after ini_set and session_start. I mean it is common code?

Upvotes: 1

Views: 367

Answers (2)

Jos
Jos

Reputation: 21

Well I found the cause. I had an "echo statement" before the header statement. After deleting this statement it works fine. Thx for your comment anyway!

Upvotes: 0

Chris Hodges
Chris Hodges

Reputation: 9

Try this first: ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']).'/name.nl/tmp'));

(Move parentheses)

Try looking here: http://php.net/manual/en/function.session-save-path.php

Not sure if this will help or not but it seems related to where your session folder is at and then pointing to the proper directory/folder in your code.

The solution that worked for them: ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session'));

Your current session save:

ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT'])).'/name.nl/tmp');

Upvotes: 1

Related Questions