|
-
Nov 18th, 2002, 09:10 PM
#1
Thread Starter
Lively Member
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?
-
Nov 19th, 2002, 06:53 AM
#2
Frenzied Member
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.
-
Nov 19th, 2002, 06:54 AM
#3
Fanatic Member
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.
-
Nov 19th, 2002, 06:56 AM
#4
Frenzied Member
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?)
-
Nov 19th, 2002, 07:15 AM
#5
Fanatic Member
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
-
Nov 19th, 2002, 12:51 PM
#6
Frenzied Member
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.
-
Nov 19th, 2002, 12:58 PM
#7
Stuck in the 80s
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.
-
Nov 19th, 2002, 01:10 PM
#8
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 == 1 ) {
?>
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>
-
Nov 19th, 2002, 01:13 PM
#9
Stuck in the 80s
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.
-
Nov 19th, 2002, 01:17 PM
#10
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, ......"
-
Nov 19th, 2002, 01:19 PM
#11
Stuck in the 80s
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.
-
Nov 19th, 2002, 01:21 PM
#12
Stuck in the 80s
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|