Php redirect to another page. How to set a redirect to another URL in PHP before the page loads


Anyone can send. But redirecting correctly is a whole art. But it is even more difficult to redirect users to the right path in the Internet. A redirect to php is best suited for this.

What kind of redirect?

In web programming, situations arise when you need to redirect a user following a link to another address. Of course, at first glance, implementing such a redirect looks a little “illegal.” In practice, such a redirect is in demand not only among attackers, but also among honest webmasters:

In what cases may a redirect be required:

  • When the site engine is replaced, the architecture of the entire resource changes as a result. Then the problem arises of how to make a redirect;
  • When the structure of a resource is redrawn, entire sections or one material are added, deleted or transferred. While this process is happening, it is temporarily possible to redirect the user to the desired section;
  • If the site has recently changed its Domain name– after changing the domain name, the old one will still appear in the search results. In this case, the user is redirected to new domain will be implemented automatically by the search engine;
  • During the authorization process, as a rule, on a large website there are two groups of users: ordinary visitors and resource administrators. In this case, it makes sense to implement a redirect for each user according to his rights and role. After authorization, the site administrator or moderators go to the administrative part of the resource, and visitors go to the user part of the resource.
Features of redirect to php

Unlike others php languages has some advantages in implementing a redirect:

  • Php is server language programming. Therefore, the redirection will not occur in the html code of the pages displayed in the browser, but in the script located on the server;
  • Redirecting to php can be implemented in several ways. Which greatly expands its application;
  • Thanks to data processing on the server, redirection implemented with using php, less susceptible to the effects of search engine filters.

To redirect in PHP, the header() function is used. It is used to send http header. Its syntax is:

void header (string $string [, bool $replace = true [, int $http_response_code ]])

Arguments accepted by the function:


  • string $string – header line;

There are two types of this argument. The first one is for sending the connection status code. It starts with "HTTP/". The other type sends a status code (REDIRECT 302) to the client browser along with the header. This argument begins with "Location:"


  • bool $replace is an optional attribute of type bool . Responsible for overriding the previous header. If set to true , the previous header or headers of the same type will be replaced. If the argument is set to false , then the header will not be rewritten. By default, this value is set to true ;
  • http_response_code – the argument forces the HTTP response code. Installation code will pass succeeds provided that the string argument is not empty.

The HTTP status code is part top line server response. The code consists of three numbers, followed by an explanatory inscription on English language. The first digit is responsible for the status class. The redirect corresponds to codes from 300 to 307. Full description can be found in the relevant technical documentation.

When using the header() function to redirect external links great importance has the location of its call. In the code it should be located above all html tags:


Using the header() redirect

To demonstrate the function on local server you need to create two files. Let's call one of them redirect.php , and the other redirect2.php . Inside the first one we will place a function call in the following format:

In another file we put the line:

echo "Hello! You are in the redirect2.php file";


A few more practical examples of using a redirect to php:

  • Force code transfer http status– when using the first argument of the header() function of type “location”, by default the status code “302” (temporarily moved) is passed to the header. This can become a problem when moving a resource to another domain name. In search engines, such temporary redirection may take longer. After all, the search engine constantly analyzes the status code. And it says “temporarily moved”. Example of forced rewrite of status code "302" to "301" (permanently moved):

Rewriting is also possible in two stages. The first line rewrites the status code, and the second redirects to a new address:

  • Using redirection of external links to redirect depending on the user's role. The role is determined during the authentication procedure. The value to be processed is written to the $who variable:

Press me

Redirect3.php file code:


Well, here we are, learning the basics of redirecting in PHP. Now you can safely take on the task of redirecting users in the right direction. The main thing is not to make a mistake in the direction, otherwise you will lead all your users to someone else’s site...

How often do you need to redirect the user to another page? I'm sure it's not uncommon. Several methods are used to accomplish this task.

Redirecting to javascript

On the page you want to redirect the user from, set the following code:

window.location.href = "http://www.site"

Instead of http://www.site, specify the page to which the user should be redirected. The method does not work if javascript is not enabled in the browser. But, as practice shows, almost all site users have javascript enabled. But at the same time it is disabled for search bots.

Redirecting using the refresh meta tag

Let's take advantage html tags. The tag indicating the page refresh time looks like this:

The tag in this form will force the page to be refreshed in the browser after 5 seconds, replacing the page address with the new one specified in the tag.
But we are not satisfied with 5 seconds, so the final version of the tag will be as follows:

< meta http-equiv = "refresh" content = "0;url=http://www.сайт" />

The disadvantage of redirecting with tags and javascript is that if the site is moved, the domain is changed, or files are moved, the article remains the same for the user, but not for search engines. Therefore, when moving a site, you absolutely cannot use these methods, since the main site will fall out of the search results due to its emptiness, and the new site will not appear in the results due to “plagiarism.” Yes, yes, search engines will evaluate the site as plagiarism. And this site stole content from your previous site. Plagiarists are significantly lower in search results. Don't make mistakes.

PHP tool redirection

Everything is simple here. You need to send the appropriate http header

Sad experience tells us that server status should not be neglected.
One of my articles was often found in search engine results. But I decided to move it to another address. As a result, address A was changed to address B. However, I did not send any headers for visitors to address A. After some time, search bots found page B. They indexed it, saw that it coincided with page A, and ignored it. Then we went to page A, saw that it had disappeared, and deleted it from our own database.
As a result, my article dropped out of search engine results.

Therefore, do not forget to specify the necessary http headers when transferring materials.

Forwarding by means apache server(.htaccess)

Perhaps the most convenient way redirects. An example of a redirect to a new domain:

Thus, we redirect the request from the old.htm file, located in the root directory of the site, to the rewrite.html file, located in the newcategory directory.

As you have already noticed (and those who didn’t have time still have one last chance), RewriteRule supports regular expressions, which allows you to do whatever you want with redirection.
For example, I made a .htm file with an image (RewriteRule ^(.*).htm$ $1.jpg), i.e. from the address dog.htm the browser redirected me to the image dog.jpg. I don’t want to describe all the capabilities of the mod_rewrite module, with whose help we were now working. Many tutorials have been written about this, but if you want, you can look it up yourself Additional information about it. (For example, in, however, in English.)

The advantage of this method is that it is painless. That is, the transfer of materials and subsequent redirection from the old address to the new one will not in any way affect the site’s position in search engines. The system works the same as php redirect except it doesn't require installed php. But there is also a minus: redirection via mod-rewrite loads the server a little more than the same redirection via php. And the mod-rewrite module is not always installed on Apache. Although most hosters now install it, there are still isolated clinical cases where this module is not installed.

Choose the method that suits you best and install it on your site!

(PHP 4, PHP 5, PHP 7)

header — Send a raw HTTP header

Description

header (string $header [, bool $replace = TRUE [, int $http_response_code ]]) : void

header() is used to send a raw HTTP header. See the » HTTP/1.1 specification for more information on HTTP headers.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include , or require , functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.


Parameters

The header string.

There are two special-case header calls. The first is a header that starts with the string " HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code.

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT(302) status code to the browser unless the 201 or a 3xx status code has already been set.

Replace

The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSE as the second argument you can force multiple headers of the same type. For example:

Http_response_code

Forces the HTTP response code to the specified value. Note that this parameter only has an effect if the header is not empty.

Return Values

No value is returned.

Changelog Version Description
5.1.2 This function now prevents more than one header to be sent at once as a protection against header injection attacks.
Examples

Example #1 Download dialog

If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.







2024 gtavrl.ru.