How to do a php re-direct?

Discussion in 'PHP' started by Davey Crocket, Oct 22, 2005.

  1. #1
    I inserted the following code into a blank index.php file

    <?php
    header("Location: http://www.eselfhypnosis.com/self-help/");
    ?>



    Now when I go to www.eselfhypnosis.com I get this message:

    Warning: Cannot modify header information - headers already sent by (output started at /home/eselfhyp/public_html/index.php:1) in /home/eselfhyp/public_html/index.php on line 2


    Can anyone tell me what I'm doing wrong? I just want to redirect the main index file to my forum directory which is called "self-help" www.eselfhypnosis.com/self-help

    Thanks!:)
     
    Davey Crocket, Oct 22, 2005 IP
  2. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #2
    Have you got a carriage return before th <?php tag?

    The error message that you are seeing is because some html has already been output.
     
    dct, Oct 22, 2005 IP
  3. Homer

    Homer Spirit Walker

    Messages:
    2,396
    Likes Received:
    150
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You may want to try this
    <?php/** * Place in a blank PHP page */// Change to the URL you want to redirect to$URL="http://www.example.com";header ("Location: $URL");?>
    HTML:
     
    Homer, Oct 22, 2005 IP
  4. blinxdk

    blinxdk Peon

    Messages:
    660
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can't have output before doing the redirect or you'll get that error.
     
    blinxdk, Oct 22, 2005 IP
  5. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Also, there is a space after location: and before the URL; not sure if that is an issue or not.
     
    TheHoff, Oct 22, 2005 IP
  6. Avatar

    Avatar Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Why go through the hassle of using PHP for this, there are loads of great Javascript redirectors.
     
    Avatar, Oct 23, 2005 IP
  7. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Welcome to Digital Point!

    Because a user can turn Javascript off or a browser might not be compatible. They can't argue a PHP-redirect since it is server side. It really isn't a hassle and in fact is quicker. With Javascript you have to have a full HTML page.. PHP needs 1 line.
     
    TheHoff, Oct 23, 2005 IP
  8. Davey Crocket

    Davey Crocket Well-Known Member

    Messages:
    690
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    128
    #8
    I changed the code to what homer suggested


    Now when I go to www.eselfhypnosis.com I just get a bank page. I attached the index.php file to this post...am I suppost to have <html> and <head> tags? or should it be completly blank.:confused:
     

    Attached Files:

    Davey Crocket, Oct 23, 2005 IP
  9. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Totally blank page. The only code on the page should be the <?php ?> line. Having HTML code on the page (before the PHP redirect) will cause it to fail. I believe it is also good manners to put an exit statement after the redirect. Your whole file will be:

    
    <?php
    header("location:http://www.eselfhypnosis.com/self-help");
    exit;
    ?>
    
    PHP:
     
    TheHoff, Oct 23, 2005 IP
  10. Davey Crocket

    Davey Crocket Well-Known Member

    Messages:
    690
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    128
    #10
    Thanks so much for explaining. It works perfectly now!
     
    Davey Crocket, Oct 23, 2005 IP
  11. Avatar

    Avatar Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    thnx.

    good point. I'll remember this.
     
    Avatar, Oct 24, 2005 IP
  12. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #12
    To avoid the "headers already sent" errors you can add
    
    <?php
    ob_start();
    ?>
    
    PHP:
    to the start of the page.
     
    dave487, Oct 24, 2005 IP
  13. webdesigners123

    webdesigners123 Peon

    Messages:
    282
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Make sure nothing is been send the use before calling this line. Sometime, a space char at the file header can do the effect
     
    webdesigners123, Oct 25, 2005 IP