PHP Redirect?

Discussion in 'PHP' started by mokimofiki, Sep 18, 2008.

  1. #1
    I am having trouble saying if a variable in the session is not set then redirect back to the login page any help would be great thank you :)

    <?php session_start(); 
    
    if(!isset($_SESSION['username']))
      {
           header("Location: login.php"); 
      } 
    ?>
    Code (markup):
    This doesn't work any thoughts?
     
    mokimofiki, Sep 18, 2008 IP
  2. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't see why your code shouldn't work :/
    try print_r($_SESSION) on the session, to check if its been set or not..
     
    lui2603, Sep 18, 2008 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    This is the error that I get:

    Warning: Cannot modify header information - headers already sent by (output started at /home/mofiki/public_html/testsite/header.php:3) in /home/mofiki/public_html/testsite/header.php on line 6
     
    mokimofiki, Sep 18, 2008 IP
  4. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ah, the reason its not working is because headers have to be declared before any other content is echoed out or something..
    try putting it at the top of the script & including header.php and stuff after
     
    lui2603, Sep 18, 2008 IP
    mokimofiki likes this.
  5. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #5
    Got it thank you very much for your quick replies it works great now .... the little issues are always the most annoying :)
     
    mokimofiki, Sep 18, 2008 IP