Solved

A PHP function equal to ASP's response.redirect?

Posted on 2009-05-03
8
418 Views
Last Modified: 2012-05-06
Hi experts!

I've been playing around with the header() function in PHP, but I can't get it to work properly.

What I want is my own Redirect() function that I can call WHENEVER I want in my script. So the function should work even if there's already some HTML buffered or not.

I've also tried the http_redirect function, but it's not installed on the webserver at GoDaddy so I can't use that.

This is what my ASP function looks like:
<%

function redirect(tUrl)

	response.redirect tUrl

end function

%>

Open in new window

0
Comment
Question by:JoachimMartinsen
8 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 24289991
header is the correct function

header('location: /index.php');

here's the main problem people have.

there has to be NO OUTPUT to the browser before the header command. This include any 'white space' (blank lines before the <?PHP) It's also most important to check the include files for white space.
0
 
LVL 6

Expert Comment

by:basic612
ID: 24290116
You could look in to output buffering if you wanted to avoid content being sent to the browser before you get a chance to send header redirect.

I'd suggest you could also send some javascript to the browser to carry out your header redirect in case your header one failed. The following function may be of assistance.

However, the best possible method is to ensure that you have not output anything to the browser, then you can send a header redirect which will be most transparent to the end user...

<?php

function custom_redirect($url)

        error_reporting(0); // hide errors if header sent already

        header($url);

        echo <<<EOD

<script type="text/javascript">

<!--

window.location = "$url"

//-->

</script>

EOD;

}

?>

Open in new window

0
 
LVL 6

Expert Comment

by:basic612
ID: 24290120
sorry, typo in last code

<?php

function custom_redirect($url)

        error_reporting(0); // hide errors if header sent already

        header("Location: $url");

        echo <<<EOD

<script type="text/javascript">

<!--

window.location = "$url"

//-->

</script>

EOD;

}

?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24290966
You can also do this.  It works at any point in the code - it does not have to be in the HTML headers.  It has pretty good browser support and does not require JavaScript.

// REDIRECT

echo "<meta http-equiv=\"refresh\" content=\"0;url=$url" />\n";

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:basic612
ID: 24291340
While <META> tags may work for you in the body of the document, they actually belong in the <HEAD> according the the W3C reccomendations

As such I would not advise using the <META> tag in the <BODY> section as you are not guaranteed cross-browser compatibility.

Not any more so than using Javascript at any rate.

The safest method to provide consistent results is to use the headers to redirect, and ensure you do this before your script produces any output.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24291400
@basic612: Completely agree about using the header() command.  OTOH, the meta refresh and the JS methods will work for 95% or more of the clients.  And for the other 5% a link that says, "If you are not redirected, click here" should fill the bill.

Here is a sample of how to produce output that is destined for the browser output, and capture it in the output buffer, then redirect.  An important point to note is that in PHP, header("Location"); is a synchronous part of the script.  The header is sent to the browser and the SCRIPT KEEPS RUNNING.  So it is often very important to use die() or exit in order to stop the script and prevent unexpected consequences.  Omitting the exit statement after header("Location") is a common oversight.

Best regards, ~Ray
<?php // RAY_ob_redirect.php

error_reporting(E_ALL);
 

// START THE OUTPUT BUFFER TO PREVENT DATA FROM GOING TO THE BROWSER

ob_start();
 

// WRITE SOME DATA - THIS DOES NOT GO TO THE BROWSER, IT GETS CAUGHT IN THE BUFFER

echo "<br/>Click here if you are not redirected: <a href=\"http://google.com\">http://google.com</a>\n";
 

// REDIRECT - NOTE THAT THIS IS A SYNCHRONOUS COMMAND

// THE SCRIPT KEEPS RUNNING WHILE IT WAITS FOR THE BROWSER TO REDIRECT!!

header("Location: http://google.com");
 

// NOW FLUSH THE OUTPUT BUFFER

ob_end_flush();
 

// NOW THE SCRIPT IS DONE - YOU _MUST_ STOP THE SCRIPT AFTER A HEADER("Location:") COMMAND

die();

Open in new window

0
 
LVL 6

Author Closing Comment

by:JoachimMartinsen
ID: 31577324
I kind of knew about this. However, you delivered a complete script for me to use.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24296255
Thanks for the points - it's a good question.  Best, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now