Home > Article > Backend Development > What is the difference between jump and redirect in PHP
Difference: 1. Jump means that the current URL request is successful and a new URL is requested again; while redirection means that the current URL is invalid and is redirected to the new URL. 2. During the jump, the browser will record the current URL and the new URL into the history; redirects will not. 3. Redirect cannot go back to the current URL; jump can.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Jump:
The browser thinks: The current URL request is successful and re-requests a new URL.
The browser will record the current URL and the new URL in the request history.
Rewind can be returned to the current URL. (Regardless of success and error, it is the same)
Syntax implementation: At the browser level, it is implemented by modifying the browser's location.href: location.href=href;
Redirect:
The browser believes that the current URL is invalid and is redirected to a new URL.
The browser will not record it The current URL is entered into the history record,
cannot be rolled back to the current URL.
The syntax implementation is that the server sends a redirect response instruction to the browser,
Through the response header:
header('Location:'. URL), 立即重定向到某个URL header('Refresh: ')
Page redirection must have page jump, page jump does not necessarily have page redirection, that is to say, page redirection is really included in page jump, page redirection Orientation is a sufficient and unnecessary condition for page jump.
Redirect usage examples
Redirect is to redirect various network requests to other locations through various methods. Location (such as: web page redirection, domain name redirection, routing change is also a redirection of the path of the data packet).
When we are building a website, we often encounter situations that require web page redirection:
1. Website adjustment (such as changing the web page Directory structure);
2. The webpage is moved to a new address;
3. The webpage extension is changed (if the application needs to change .php to .Html or .shtml).
In this case, if no redirection is done, the old address in the user's favorites or search engine database can only cause the visiting customer to get a 404 Page error message and loss of access traffic; in addition, some websites that have registered multiple domain names also need to redirect users who visit these domain names to automatically jump to the main website. Sites, etc.
Summarize several page jump methods under PHP
1. Meta tag implementation
Just need to Just add the following sentence to the head. After staying on the current page for 0 seconds, jump to the target page
echo '<meta http-equiv="refresh" content="0;url=https://www.baidu.com">';
2. JavaScript implementation
echo '<script>window.location.href = 'https://www.baidu.com';</script>';
3. PHP page redirection implementation
header('Location: https://www.baidu.com');
The difference between using PHP page redirection and the above two methods is that its http response status code is specified as 3xx. The specific difference involves the HTTP request process, so I won’t go into details here.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between jump and redirect in PHP. For more information, please follow other related articles on the PHP Chinese website!