Hopeless redirect php. PHP redirect external links

Let's say you want to display the page https://example.com/final.php to users who go to the page https://example.com/initial.php. This can be done with a few PHP methods, JavaScript and HTML. In this article, we will cover each of the methods that you can use to redirect PHP to another page.

Here are a few variables we will use:

Using the PHP header() function to redirect a URL

If you want to add a redirect from initial.php to final.php , you can place the following code on the initial.php web page. It sends a new location header to the browser:

Here we use the PHP function header() to create the redirect. You need to place this code before any HTML or text. Otherwise, you will receive an error that the header has already been sent. You can also use output buffering to prevent this error of sending headers. In the following example this method PHP redirects shown in action:

To perform a redirect using the header() function, the ob_start() function must be the first function in the PHP script. This will prevent header errors from occurring.

As additional measure you can add die() or exit() immediately after the header redirect so that the rest of the web page code does not execute. In some cases search robots or browsers may not pay attention to the Location header. What does it contain? potential threats for site security:

To be clear: die() or exit() have nothing to do with redirects. They are used to prevent the rest of the code on the web page from executing.

When redirecting to a page in PHP, it is recommended to use absolute URLs when specifying the Location header value. But relative URLs will work too. You can also use this feature to redirect users to external sites or web pages.

Conclusion JavaScript code-redirect using the PHP echo() function

This is not a pure PHP solution. However, it is also effective. You can use PHP function echo() to output the JavaScript code that will handle the redirect.

If you use this solution, you won't have to use output buffering. This also prevents errors related to sending headers from occurring.

Below are some examples that use different methods JavaScript for redirecting with current page to another:

The only downside to this method of redirecting to another PHP site is that the JavaScript runs on the client side. And your visitors may have JavaScript disabled.

Using HTML meta tags for redirection

You can also use basic HTML to perform a redirect. It may seem unprofessional, but it works. And you don't have to worry about JavaScript being disabled in your browser or having previously sent a headers error:

You can also use the last line from the previous example to automatically refresh the page every "n" seconds. For example, the following code will automatically refresh the page every 8 seconds:

Conclusion

In this article I covered three various methods redirects from index php, as well as their advantages and disadvantages. The specific method to use depends on the objectives of the project.

Translation of the article “How can I do a redirect to different url before page load in PHP” by the friendly project team.

Good Bad

Quick navigation on this page:

If you decide to write a script and do PHP redirect, the advantages of this step are obvious: PHP - server-side language oriented scripts; the redirect will be performed through a script on the server, and not in the visitors' browser. Some redirects can be performed on the client side - through a js redirect (that is, through a JavaScript redirect).

This is a more flexible and versatile approach and you can perform multiple types of redirects in PHP, unlike other methods. Here are the most common types redirects that can be done in PHP: a) 301 PHP redirect (permanent redirect status), b) 302 PHP redirect (temporary redirect status), c) Update.

This article will be useful primarily for novice webmasters who are looking for ways to implement URL redirection if it is not possible using other common solutions such as Htaccess.

Heading PHP language functions

For example, let's say you want to redirect to this URL http://www.somewebsite.com/target.php. In the original PHP pages You should simply call this redirect script:

Also try this simple experiment on your local hosting:

1) Open text editor and enter this code:

Save it as targetpage.php.

2) Open another empty one text file and enter this code:

Save it as originatingpage.php.

3) Now launch your web browser. IN address bar browser enter: http://localhost/originatingpage.php

4) You will notice that after pressing the enter button, this URL: http://localhost/originatingpage.php redirects to http://localhost/targetpage.php and to targetpage.php, and you see the words “Hi this is codex” -x".

One of the most common mistakes may lie in the design html code redirect:

Try this experiment:

Go to the originatingpage.php script and add any HTML tag:

header('Location: http://localhost/targetpage.php');

Let's say you have code like this:

This is a redirect error