-3

I am just trying to write some code to check if the session is exist. If the answer is no, redirect the page to the login page. Moreover I want to write this code for multiple website. So that whatever the domain name is never be a fact for me. here I write some codes:

//$_SESSION['license'] -> this will be created after anyone log in
//and will be set its value to 15497852.
if(isset($_SESSION['license']) && $_SESSION['license'] == '15497852'){
    // DO SOMETHING
} else {
    // here i want to create link like: http://stackoverflow.com/?df=login
    header('Location: '.$_SERVER['PHP_SELF'].'?df=login');
    die();
}

but this code is not working where I am not seeing anything wrong.

Black Cobra
  • 31
  • 2
  • 9
  • Is there an error written in the page when this happens? – NoLifeKing Mar 24 '12 at 07:56
  • Did you put any content on the output before the header() call? – xea Mar 24 '12 at 07:59
  • no... not at all... i know that i can't output any thing before the header() tag... – Black Cobra Mar 24 '12 at 08:01
  • Add an `error_reporting(E_ALL)` at the very top of the page, it should print out an error then. And as the others have said, if there's any other code before (presenting output and such), it will not work. – NoLifeKing Mar 24 '12 at 08:01
  • @BlackCobra include your entire code - all of it before this statement – Manse Mar 24 '12 at 08:01
  • possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – mario Mar 24 '12 at 08:09
  • before this code i just write a single line to start a session. session_start(); nothing more... and last of all i just require_once a php file... but it is after the header() tag... – Black Cobra Mar 24 '12 at 08:09

2 Answers2

4

From the header() doc on php.net

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

Check there is nothing being output before this code runs ... even <html> for example before this line will cause it not to work

Manse
  • 37,765
  • 10
  • 83
  • 108
  • there is no html tags and no outputs... no chance... just a single line before it to start s session. – Black Cobra Mar 24 '12 at 08:06
  • Are you sure the `session` is empty/doesnt contain your value - ie its not actually calling the code ? try removing the `if` statement and just executing the `header` line – Manse Mar 24 '12 at 08:08
  • You request this page ... its sets the header to the same page + query string ... then what does the code do ? – Manse Mar 24 '12 at 08:15
  • Maybe there is a BOM character? Copy/past the code in notepad and save it as the original file. Do you get the error again? And a space before ` – Wouter J Mar 24 '12 at 09:19
  • @BlackCobra what was the problem ? – Manse Mar 25 '12 at 20:05
  • sorry friends, I forgot to share the solve. I think the url was the problem... I was checking the codes in my local pc using wamp server... when I check the same codes in my main linux server, everything is ok... now I am not getting any error message... – Black Cobra Mar 26 '12 at 05:39
0

I think the url was the problem... I was checking the codes in my local pc using wamp server... when I check the same codes in my main linux server, everything is ok... now I am not getting any error message...

Black Cobra
  • 31
  • 2
  • 9