How to Redirect in PHP (Page or URL)

How to Redirect in PHPYou can use this simple PHP script to redirect a user from the page they entered to a different web page or to different website. This will show you a better way to redirect in PHP. Using this method, they can be seamlessly transferred to the new page without having to click a link to continue.

When designing a web site, many times you must change the location of a web page. However, if the page is popular, your visitors may have already linked to it. In addition, the Search Engines have most-likely already indexed the page.

So, How to redirect in PHP?

We’re going to use simple HTML redirect as mentioned in this tutorial (How to redirect in HTML) and PHP header() function.

function redirect($location,$delay=0){
	if (headers_sent()){
		echo "<meta http-equiv='refresh' content='".$delay."; url=".$location."' />";
	} else {
		header('Location: ' . $location);

Now you can call this function anywhere in your script.


Functions used: