Results 1 to 12 of 12

Thread: Redirecting not working in php.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 1999
    Location
    Oshawa, Ontario, Canada
    Posts
    88

    Angry Redirecting not working in php.

    I'm trying to get this stupid php to redirect after it send mail. Heres the php code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" type="text/css" href="css/main.css" />
    <title>Survey Results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head><body bgcolor="#EBECED">
    <p><b><big>
    <?php
    function ValidateMail($Email) {
    global $HTTP_HOST;
    $result = array();
    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {
    $result[0]=false;
    $result[1]="The email address entered is not properly formatted. Please press the back button and correct your email address!";
    return $result[1];

    } else if ($Email == "[email protected]") {
    $result[1]="Please enter an e-mail address! Press your back button to make this correction.";
    return $result[1];
    }
    else

    $result[0]=true;
    $result[1]="$Email appears to be valid.";
    return $result[0]; } // end of function

    $Returned = ValidateMail($Email);
    if ($Returned == 1 ) {
    print ("Processing your request...");
    mail("[email protected]", $Subject, "Name: $Name \n\n $Message", "From: $Email");
    header("Location: http://www.example.com/");
    exit;
    }
    else
    print ($Returned);
    ?>
    </b>
    </big>
    </p>
    </body>
    </html>

    It doesn't want to redirect to the address....whats wrong here?
    -|Kn|gHt|
    MSN - [email protected]

  2. #2
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    You're outputting text before you're sending the headers aren't you? Headers must go before all text. You can use the headers_sent function to check if headers have already been sent, but regardless the headers have to be sent first.

  3. #3
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    once you have output in the file you cannot use headers.

    So in your case it would be best to either place all your PHP code at the top with the headers or to use <meta> tags to redirect.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  4. #4
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Also remember though that some browsers don't support the meta refresh, so I'd go with the php headers thing (which I think all browser support don't they?)

  5. #5
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    browsers do not have a choice whether or not they support it, its on the server-side so the server will execute it as long as there isn't any output beforehand
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  6. #6
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    But aren't headers (at least the location one) interpretted by the browser, for instance content-type - I'm pretty sure that's interpretted by the browser, so it knows whether the content is html and the charset.

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Rick Bull
    But aren't headers (at least the location one) interpretted by the browser, for instance content-type - I'm pretty sure that's interpretted by the browser, so it knows whether the content is html and the charset.
    Yes, you are correct. PHP sends the header to the browser, and it's up to the browser to work from there.

    The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless some 3xx status code has already been set.
    So PHP does handle the header(), but it's sent back to the broswer. If the broswer does not allow or support something, then I think that the PHP functionality will not work.

    I haven't tested it though, so I really don't know. That's just my best guess.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    hrmmm...... could you send the new header location if you close the HTML doc first?????

    maybe something like this?
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" type="text/css" href="css/main.css" />
    <title>Survey Results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head><body bgcolor="#EBECED">
    <p><b><big>
    <?php
    function ValidateMail($Email) {
    global 
    $HTTP_HOST;
    $result = array();
    if (!
    eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$Email)) {
    $result[0]=false;
    $result[1]="The email address entered is not properly formatted. Please press the back button and correct your email address!";
    return 
    $result[1];

    } else if (
    $Email == "[email protected]") {
    $result[1]="Please enter an e-mail address! Press your back button to make this correction.";
    return 
    $result[1];
    }
    else

    $result[0]=true;
    $result[1]="$Email appears to be valid.";
    return 
    $result[0]; } // end of function

    $Returned ValidateMail($Email);
    if (
    $Returned == ) {
    ?>
    Processing your request...
    </b>
    </big>
    </p>
    </body>
    </html>
    <?php
    mail
    ("[email protected]"$Subject"Name: $Name \n\n $Message""From: $Email");
    header("Location: [url]http://www.example.com/[/url]");
    exit;
    }
    else
    print (
    $Returned);
    ?>
    </b>
    </big>
    </p>
    </body>
    </html>
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by techgnome
    hrmmm...... could you send the new header location if you close the HTML doc first?????
    No. Once output is sent to the screen, you're done for. It's not an HTML thing. Once output is being sent, the headers are sent and closed. You can't send headers after they've already been set.

    There's no way around it.

    And why would you want to do that anyways? It will just begin to output and then redirect before the user can even see it.

    Just makes no sense to me.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by The Hobo
    No. Once output is sent to the screen, you're done for. It's not an HTML thing. Once output is being sent, the headers are sent and closed. You can't send headers after they've already been set.

    There's no way around it.

    And why would you want to do that anyways? It will just begin to output and then redirect before the user can even see it.

    Just makes no sense to me.
    No different than the "Please wait while your search is being conducted...." screen I get when I come into VBF and click on "See New Posts"
    Or the "Thank you for your post, ......"
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by techgnome
    No different than the "Please wait while your search is being conducted...." screen I get when I come into VBF and click on "See New Posts"
    Or the "Thank you for your post, ......"
    Yes, it's very different. Those are using Meta Refresh. They have a time-out so the user can actually see the message. With the header() call, it's instant, depending on the connect speed.

    You can not use header() after output has been sent to the screen.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    And don't try to argue it, either. I see your lack of knowledge of the header function in the code you posted:

    Code:
    header("Location: <a href="http://www.example.com/" target="_blank">...
    First, you're going to get a T_STRING error because of your quotes, and second, that'd be like tyoing <a href="www.aol.com"> in the Address bar of your browser.

    Trust me, it's just not possible.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width