tags:

views:

45

answers:

3

In my web admin area I have using very simple logic:

session_start(); ob_start();
if(!isset($_SESSION['user'])){
    header("Location: login.php");
}
contents...
ob_end_flush();

Yes this is working perfect, redirect to login page. But the comic point is that I can see the content of index.php (that protected!!!) here What is wrong? Thanks in advance

+4  A: 

Put an ob_end_clean and exit after the header call to prevent any further execution/output:

if (!isset($_SESSION['user'])) {
    header("Location: login.php");
    ob_end_clean();
    exit;
}
Gumbo
+8  A: 

Abra kadabra

if(!isset($_SESSION['user'])){
    header("Location: login.php");
    die("GET LOST YO");
}
Robus
Better without the message, but killing the script fixed the problem easily.
animuson
*Shrug*, browsers shouldnt see this anyway, since you're getting redirected. And if somebody's sneaky then... well, you just told him to get lost, as he always should have :P
Robus
Even though it doesn't matter in this case it's still not a good idea to use `die()` anywhere in a website.
Gnarly
@Gnarly It's the same really http://pl.php.net/die
Robus
The same as what? What are you talking about?
Gnarly
@Gnarly Uh, thought you were criticizing die() in favor of exit(). Never mind then :P
Robus
+1  A: 

You should make all script content in IF.

session_start(); 
ob_start();
if(isset($_SESSION['user'])){
    contents...
}
else {
    header("Location: login.php");
ob_end_flush();
Alexander.Plutov