SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    819
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    pls help with PHP redirect..

    in Java this is very simple..
    Code:
    response.sendRedirect(url);
    I don't get this whole thing with the headers in PHP..

    wherever I put my redirect code ( header("")... ) I get this "content has already been sent" error.. ( I also include

    Code:
    header("HTTP/1.1 301 Moved Permanently");
    before actual redirect line, and the exit(); afterwards ....

    in JSP you can put the redirect stmt wherever you want (I need to put it inside a conditional..) how on earth does this work in PHP?? no response.redirect()??? (there's no $_RESPONSE object in PHP? I can't find one..)

    thank you...

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTTP headers need to be sent before the response body to the client. So you must issue your header() statement before echoing or output anything.

    E.g. This won't work
    PHP Code:
    <p>You're being redirected</p>

    <?php
    header
    ("Location: http://somewherelse");
    exit();
    ?>

  3. #3
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Expanding on what cranial-bore said, you need to put the header before any output, even a space.

    This won't work
    PHP Code:
    <html>
    <head>
    </head>
    <body>
    <?php header('location:http://someothersite.com'); ?>
    </body>
    </html>
    This won't work (notice the line (space) between the PHP tags?)
    PHP Code:
    <?php session_start(); ?>

    <?php header('location:http://someothersite.com'); ?>
    <html>
    <head>
    </head>
    <body>
    <?php header('location:http://someothersite.com'); ?>
    </body>
    </html>
    This will work
    PHP Code:
    <?php
    session_start
    ();
    header('location:http://someothersite.com');
    ?>
    <html>
    <head>
    </head>
    <body>
    </body>
    </html>
    You could use output buffering, but that doesn't always work.
    PHP Code:
    <html>
    <head>
    </head>
    <body>
    <?php
    ob_start
    ();
    header('location:http://someothersite.com');
    ob_end_clean();
    ?>
    </body>
    </html>
    One thing that I use quite often is to just echo a meta refresh instead, which works like a charm
    PHP Code:
    <html>
    <head>
    </head>
    <body>
    <?php echo '<meta http-equiv="refresh" content="0;url=http://someothersite.com">'?>
    </body>
    </html>
    One final note, this also applies to $_COOKIE. Keep that in mind as you code your login system, etc.

  4. #4
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    819
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    so there's no way to do a redirect in PHP unless you do it at the very top of the code? you can't do it inside a function or a conditional? oh man..

    so I tried the meta tag option.. although it seems like a such a quirky way of doing it...;-) but well, it works... however, the loading back-and-forth from thank-you pg is kind of "jerky" in PHP... if you go here,
    FRANCES DEL RIO -- PHOTOBLOG -- SIGN UP FOR UPDATES and hit 'submit' w/o filling out anything.. you will see that right away you see the error message.. now this error msg is coming from the back-end, I don't have any client-side validation here (I do a redirect back to the form and send error msg in the query string.. you get the picture... ) now in JSP this happens so fast you don't see the thank-you pg load at all...

    but on PHP
    www.francesdelrio.com/php/form_test.php

    doing exact same thing (with the meta tag.. again, hit 'submit' w/o filling anything out) I see the thank-you page load then quickly it goes back to the form.. the whole thing looks kind of "jerky" compared to how smoothly it runs in JSP... (in PHP this happens whether or not I include ' exit() ' after meta-tag, not sure if I need that..)

    this is code in PHP:

    Code:
    if (!emailok($email)) {
        echo '<meta http-equiv="refresh" content="0;url=form_test.php?err=please+type+in+correct+email+address">'; 
    }
    anybody can shed some light? is there a way to make this work as smoothly in PHP as it works in JSP? (in PHP I put redirect code as far up as I could in thank-you pg; first thing I do is get email address then put conditional to redirect if it's incorrect or empty...)

    thank you...

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    a page reload is a page reload, and has nothing to do with the server, and consequently, nothing to do with the language used.

    I dont see the thank you page load on either one, so...

    The most obvious answer is 'dont use a seperate thank you page'. Send the data to the same page, and output the correct response.

  6. #6
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    so there's no way to do a redirect in PHP unless you do it at the very top of the code? you can't do it inside a function or a conditional? oh man..
    Sure you can. Just need to make sure you don't output anything before the header() call.

    Various strategies can be employed. You can use output buffering to capture any out before it actually get's sent. Then if you do need to redirect the output buffer is just discarded.

    And while PHP does not ship with a standard Response object, maybe frameworks have one. Example: Symfony - Home

  7. #7
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do your checking at the very top of your script and output the message, or redirect there.

    PHP Code:
    <?php
    if(isset($_POST['submit'])) {
        if(
    $_POST['email'] == '') {
            echo 
    'Error: Please enter your email address';
        }
        
    // do other stuff or redirect
        
    header('location:somepage.php');
    }
    ?>
    <html>
    <body>
    </body>
    </html>
    You could also store everything in output buffering and echo it in the body.

    PHP Code:
    <?php
    // start the buffer
    ob_start();
    if(isset(
    $_POST['submit'])) {
        if(
    $_POST['email'] == '') {
            echo 
    'Error: Please enter your email address';
        }
        
    // do other stuff or redirect
        
    header('location:somepage.php');
    }
    // get the buffered contents
    $contents ob_get_contents();
    // stop buffering
    ob_end_clean();
    ?>
    <html>
    <body>
    <?php echo $contents?>
    </body>
    </html>
    All of this is extremely rudimentary, but should at least make the point. I don't use output buffering very often as I do the majority of my code before the <html> tag.

  8. #8
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    819
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't use output buffering very often as I do the majority of my code before the <html> tag.
    as I mentioned in my OP, I put my header() code above all other code also (before <doctype> tag) and it still didn't work.. now I copied yr code verbatim (1st option) and got this error:

    Warning: Cannot modify header information - headers already sent by (output started at /homepages/37/d371485734/htdocs/dev/contact/thankyou.php:3) in /homepages/37/d371485734/htdocs/dev/contact/thankyou.php on line 12

    again, put yr code AT THE VERY TOP, before any other code.. I don't get this... code:


    Code:
    <?php
    		
    			
    	if (isset($_POST['submit'])) {
    		echo "from form";
    	}
    	
    	
    	if($_POST['email'] == '') {
    		echo 'Error: Please enter your email address';
    	}
    	// do other stuff or redirect
    	header('location:index.php');
    	
    	
    	//  other PHP code...
    	
    ?>
    
    <!DOCTYPE html PUBLIC     etc.....

  9. #9
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    819
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    oh brother...

    immediately after posting all this I realized problem:

    on one server am getting that error, on another server not..

    on my server (francesdelrio.com) am not getting this header error
    www.francesdelrio.com/php/form_test.php

    on my friend's server yes...
    http://andreahegeman.com/dev/contact/form_test.php

    oh brother..... well, I guess option is either submit form to same page (as someone above suggested... I have done this in JSP and I know how it works...) or simply do the client-side validation before submission (this is first time am doing back-end for a client; I have to make sure it's done right, but at the same time want to keep it as simple as possible..)

    I guess to check for whether form is submitted, as mentioned here, would be:

    Code:
    if (isset($_POST['submit'])) {
    (submit button has to have name 'submit' I assume? ;-)

    or check whether there's a referrer???

    Code:
    if ($_SERVER['HTTP_REFERER'] == "") {
       // etc.
    }
    but still curious.. why would this PHP redirect work on one server and not on another? do I need to call my friend's server's tech support?

    thank you...

  10. #10
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are you redirecting? Is it just to show a success or error message?

  11. #11
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Never trust HTTP_REFERER. Ever.

  12. #12
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Never trust HTTP_REFERER. Ever.
    What he said.

    If you want to check a referring link you're better off to code that into your system so that you send the link, either to a SESSION or as URL vars, or whatever. Some browsers don't support HTTP_REFERER so it's totally unreliable.

    For example, if you had a visitor go to a member's only page on your site, such as movies.php and they weren't logged in, you could store movies.php in a SESSION, and then after the user logs in you could redirect them to movies.php and unset the SESSION.

  13. #13
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    if(isset($_POST['submit'])) {
        if(
    $_POST['email'] == '') {
            echo 
    'Error: Please enter your email address';
        }
        
    // do other stuff or redirect
        
    header('location:somepage.php');
    }
    ?>
    <html>
    <body>
    </body>
    </html>
    dont u think we should hv an "else" clause? else, if no email address typed in, we will get header already sent....error INSTEAD of 'Error: Please enter your email address'
    I've never been born, nor will I die.
    I am just a passer-by who stumbled across a planet called Earth,
    for a short period of visiting......

  14. #14
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by leelong View Post
    dont u think we should hv an "else" clause? else, if no email address typed in, we will get header already sent....error INSTEAD of 'Error: Please enter your email address'
    It was just an example. it's up to maya90 to do all the correct checking, redirecting etc.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •