Anyway....?

Discussion in 'PHP' started by terryuk, Feb 9, 2007.

  1. #1
    IS there any way to add HTML before;

    <?php
    header( 'Location: http://www.yoursite.com/new_page.html' ) ;
    ?>

    ???????
     
    terryuk, Feb 9, 2007 IP
  2. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #2
    You can add html before this tag. I have used it many time if you are getting error - "header already sent" , cause of that error is some other thing.

    Plz paste your code here so i can help
     
    technoguy, Feb 9, 2007 IP
  3. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #3
    Im pretty certain you cant put:
    <?php
    header( 'Location: http://www.yoursite.com/new_page.html' ) ;
    ?>

    after HTML, as that redirect is processed before anything is outputted to the browser (even the HTML beforehand). That is why you get the headers already sent message.
     
    papa_face, Feb 9, 2007 IP
  4. KC TAN

    KC TAN Well-Known Member

    Messages:
    4,792
    Likes Received:
    353
    Best Answers:
    0
    Trophy Points:
    155
    #4
    You can if you use ob_start() at the start of your page. However, it is not recommended as this will increase your overhead.

    If you have ever output HTML before header(), it is advisable to re-design your logic flow.
     
    KC TAN, Feb 9, 2007 IP
  5. terryuk

    terryuk Notable Member

    Messages:
    3,962
    Likes Received:
    319
    Best Answers:
    0
    Trophy Points:
    255
    #5
    <script language="javascript">
    
    var ref   = escape(document.referrer)+" ";
    var entry = escape(unescape(location.href));
    document.write( '<' + 'script language="javascript" src="http://www.linksjuice.com/axroi/script.php?ref=' + ref + '&entry=' + entry+ '"><' + '/script>' );
    </script>
    <?php
    header( 'Location: http://www.linksjuice.com' ) ;
    ?>
    
    Code (markup):
    That's ideally how i'd like it to be so it runs the js, and has SE friendly redirect..

    Someone else told me about ob_start() but i don't get it :(
     
    terryuk, Feb 9, 2007 IP
  6. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #6
    How things are performed:

    Client browser requests your URL. Your HTTP server must return a HTTP status code. If it is a redirect code (3xx value), there is no room for any HTML output.

    So the code
    header( 'Location: http://www.yoursite.com/new_page.html' ) ;
    PHP:
    will return 302 redirect HTTP status code, and header function is not allowed after any output.

    So you can try to:

    1. Perform redirect via javascript (but it will be not SEO-friendly):
    window.location = "http://www.yoursite.com/new_page.html"
    HTML:
    2. Perform redirect via META REFRESH html tag (not SEO-friendly):
    <meta http-equiv="Refresh" content="0; url=http://www.yoursite.com/new_page.html"> 
    HTML:
    3. Change your processing logic
     
    wmtips, Feb 9, 2007 IP
  7. terryuk

    terryuk Notable Member

    Messages:
    3,962
    Likes Received:
    319
    Best Answers:
    0
    Trophy Points:
    255
    #7
    Thanks for the tips, but i was hoping if i could get this to be se friendly...

    I've got;
    
    <?php
    ob_start(); ?><script language="javascript">
    
    var ref   = escape(document.referrer)+" ";
    var entry = escape(unescape(location.href));
    document.write( '<' + 'script language="javascript" src="http://www.linksjuice.com/axroi/script.php?ref=' + ref + '&entry=' + entry+ '"><' + '/script>' );
    </script>
    <?php
     Header( "HTTP/1.1 301 Moved Permanently" ); 
       header( 'Location: http://www.linksjuice.com' ) ;
    ob_end_flush();
    ?>
    Code (markup):
    But now it seems to skip the js :confused:
     
    terryuk, Feb 9, 2007 IP
  8. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #8
    As I said before - no html processing will be performed by browser with header("Location") call. ob_start just starts output capturing and your code can be rewritten without ob_start as:
    
    <?php
      header( 'Location: http://www.linksjuice.com' ) ;
    ?>
    <script language="javascript">
    var ref   = escape(document.referrer)+" ";
    var entry = escape(unescape(location.href));
    document.write( '<' + 'script language="javascript" src="http://www.linksjuice.com/axroi/script.php?ref=' + ref + '&entry=' + entry+ '"><' + '/script>' );
    </script>
    
    PHP:
    But it does not solve problem. Browser does not process additonal html if http redirect instruction found.
     
    wmtips, Feb 9, 2007 IP