Author Topic: [SOLVED] Redirect  (Read 722 times)

0 Members and 1 Guest are viewing this topic.

Offline RAHTopic starter

  • Enthusiast
  • Posts: 60
    • View Profile
[SOLVED] Redirect
« on: September 08, 2007, 09:20:50 AM »
Hi,

I am using the following code on a contact form:

Code: [Select]
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.

Offline GuiltyGear

  • Addict
  • Posts: 1,692
  • Gender: Male
    • View Profile
    • Laboratori.al - Creative Web Geeks
Re: Redirect
« Reply #1 on: September 08, 2007, 09:33:37 AM »

Offline RAHTopic starter

  • Enthusiast
  • Posts: 60
    • View Profile
Re: Redirect
« Reply #2 on: September 08, 2007, 09:37:35 AM »
I tried the following however it seems to cause a loop as it's used later on in the script:

Code: [Select]
printf("<meta http-equiv=refresh content=\"0; url=$url2\">");

Offline BlueSkyIS

  • Fanatic
  • Posts: 4,261
    • View Profile
Re: Redirect
« Reply #3 on: September 08, 2007, 10:35:36 AM »
"I have tried multiple methods but due to the headers being sent on this contact form I get the usual "headers already sent" error."

Don't send any output to the browser before your header("location: someurl.html");
deprecated (adj.) Used typically in reference to a computer language to mean a command or statement in the language that is going to be made invalid or obsolete in future versions.

Head Shops and Grow Stores

Offline RAHTopic starter

  • Enthusiast
  • Posts: 60
    • View Profile
Re: Redirect
« Reply #4 on: September 08, 2007, 12:30:38 PM »
Hi,

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

Code: [Select]
$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.

Offline BlueSkyIS

  • Fanatic
  • Posts: 4,261
    • View Profile
Re: Redirect
« Reply #5 on: September 08, 2007, 04:45:59 PM »
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.

deprecated (adj.) Used typically in reference to a computer language to mean a command or statement in the language that is going to be made invalid or obsolete in future versions.

Head Shops and Grow Stores

Offline RAHTopic starter

  • Enthusiast
  • Posts: 60
    • View Profile
Re: Redirect
« Reply #6 on: September 08, 2007, 05:42:11 PM »
I've tried a header() redirect and it gives the same "headers already sent" error.  Is there some redirect function in PHP which overcomes this issue?

Offline BlueSkyIS

  • Fanatic
  • Posts: 4,261
    • View Profile
Re: Redirect
« Reply #7 on: September 08, 2007, 05:46:12 PM »
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.
« Last Edit: September 08, 2007, 05:47:18 PM by BlueSkyIS »
deprecated (adj.) Used typically in reference to a computer language to mean a command or statement in the language that is going to be made invalid or obsolete in future versions.

Head Shops and Grow Stores

Offline RAHTopic starter

  • Enthusiast
  • Posts: 60
    • View Profile
Re: Redirect
« Reply #8 on: September 08, 2007, 07:11:41 PM »
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.

Code: [Select]
<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>

Offline BlueSkyIS

  • Fanatic
  • Posts: 4,261
    • View Profile
Re: Redirect
« Reply #9 on: September 08, 2007, 07:16:01 PM »
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.
deprecated (adj.) Used typically in reference to a computer language to mean a command or statement in the language that is going to be made invalid or obsolete in future versions.

Head Shops and Grow Stores

Offline BlueSkyIS

  • Fanatic
  • Posts: 4,261
    • View Profile
Re: Redirect
« Reply #10 on: September 08, 2007, 07:33:10 PM »
also don't forget location in your headers:

header("location: $url1");
deprecated (adj.) Used typically in reference to a computer language to mean a command or statement in the language that is going to be made invalid or obsolete in future versions.

Head Shops and Grow Stores

Offline RAHTopic starter

  • Enthusiast
  • Posts: 60
    • View Profile
Re: Redirect
« Reply #11 on: September 08, 2007, 07:45:51 PM »
Hi,

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

Code: [Select]
$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).

Offline BlueSkyIS

  • Fanatic
  • Posts: 4,261
    • View Profile
Re: Redirect
« Reply #12 on: September 08, 2007, 07:48:16 PM »
what happens before the code you are showing us? the code you're displaying shouldn't even compile with the extra bracket at the end. Where are the <?php and ?> tags?
deprecated (adj.) Used typically in reference to a computer language to mean a command or statement in the language that is going to be made invalid or obsolete in future versions.

Head Shops and Grow Stores