Jump to content

[SOLVED] Redirect


RAH

Recommended Posts

Hi,

 

I am using the following code on a contact form:

 

if(!$validation){
// Redirect to page code needs to go here
}

 

I have tried multiple methods but due to the headers being sent on this contact form I get the usual "headers already sent" error.

 

How do I get around this?

 

Thanks.

Link to comment
Share on other sites

Hi,

 

OK, I'm now using the following at the end of the script:

 

$url = "http://www.yahoo.com";
$url2 = "http://www.google.com";
if(!$validation){
printf("<meta http-equiv=refresh content=\"0; url=$url2\">");
}else{
printf("<meta http-equiv=refresh content=\"0; url=$url\">"); 
}}

 

If validation is true then it should redirect to yahoo.com.  If validation fails it should redirect to google.com.  If validation is true then it works however if validation fails it doesn't redirect.

Link to comment
Share on other sites

do you mean to have that extra curly bracket at the end?

 

personally, i don't let the browser control anything if i can help it. that means i don't send a META tag and then assume the browser is going to redirect. imo, you're better off putting a header() redirect in; send the page to the browser, not the browser to the page.

 

 

Link to comment
Share on other sites

yes, use header("location: $url") before anything is output to the browser. If there is anything before the first php bracket <?, then that is included in "anything output to the browser". Any echo's, anything at all output to the browser before a header() is invalid. Perform all of your logic before you have to decide whether and where to send the user. Then use header(), or not if you want to continue processing the page. Also, don't forget to put an exit; after any header()'s.

Link to comment
Share on other sites

Hi,

 

The code in it's current state doesn't redirect the user if validation fails for some reason.  And if validation is true then the headers already sent error is present.

 

<body><?php



$toaddr = "test@test.com";
$subject = "subject here";
$realname = $_POST['realname'];
$email = $_POST['email'];
$address = $_POST['address'];
$date = $_POST['date'];
$time = $_POST['time'];
$telno = $_POST['telno'];
$details = $_POST['details'];

$validation = true; 


$realname = $_POST['realname'];
if($realname == ""){
$validation = false;
}
$email = $_POST['email'];
if($email == ""){
$validation = false;
}
$address = $_POST['address'];
if($address == ""){
$validation = false;
}
$date = $_POST['date'];
if($date == ""){
$validation = false;
}
$time = $_POST['time'];
if($time == ""){
$validation = false;
}
$telno = $_POST['telno'];
if($telno == ""){
$validation = false;
}
$details = $_POST['details'];
if($details == ""){
$validation = false;
}
if(!$validation){
echo "Error: Please ensure all fields are entered.";

}else{
mail(Email details here)

?>

<?php

$url = "http://www.yahoo.com";
$url2 = "http://www.google.com";
if(!$validation){
header($url2);
exit;
}else{
header($url1) ;
exit;
}}

?></body>

Link to comment
Share on other sites

you get headers already sent because of the <BODY> tag. That tag gets sent to the browser before the PHP code even starts. The headers are sent BEFORE ANYTHING, including the <BODY> tag. Once you put any output whatsoever before header() it's too late to use header(). Remove the <BODY> tags.

Link to comment
Share on other sites

Hi,

 

I removed the body tags and updated the code as follows:

 

$url = "http://www.yahoo.com";
$url2 = "http://www.google.com";
if(!$validation){
header("location: $url2");
exit;
}else{
header("location: $url1") ;
exit;
}}

 

Both issues still remain (headers already sent error if validation is true and no redirect if validation is false).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.