Home>Article>Backend Development> What is 302 error in php

What is 302 error in php

青灯夜游
青灯夜游 Original
2022-04-22 16:36:14 5244browse

In PHP, 302 is not an error, but an HTTP response status code, which means "temporary redirection", indicating that the visited page is temporarily jumped to other pages due to various needs; header() can be used To implement 302 jump, the syntax is "header('Location: jump url',true,302)".

What is 302 error in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

302 represents temporary transfer (Temporarily Moved ), is an HTTP response status code and is not an error.

302 means temporary redirection, which means that the visited page is temporarily jumped to other pages due to various needs.

In php, header() can be used to implement 302 jump.

has two syntax formats:

  • Grammar format 1:

header('HTTP/1.1 302 Moved Permanently'); header('Location: //m.sbmmt.com');
  • Grammar format 2:

header('Location: //m.sbmmt.com', true, 302);

One more thing, if you want to adapt to HTTPS/HTTP, this is enough:

header('Location: //m.sbmmt.com', true, 302);

Extended knowledge:

HTTP--3xx (Redirect) response status code

To complete the request, further action is required. Typically, these status codes are used for redirects. Google recommends that you use no more than 5 redirects per request. You can use Webmaster Tools to see if Googlebot is having trouble crawling the redirected page. The Web Crawl page under Diagnostics lists URLs that Googlebot was unable to crawl due to redirect errors.

  • 300 (multiple choices): In response to the request, the server can perform a variety of operations. The server can select an action based on the requester (user agent) or provide a list of actions for the requester to choose from.

  •  301 (Permanently Moved): The requested web page has been permanently moved to a new location. When the server returns this response (in response to a GET or HEAD request), it automatically forwards the requester to the new location. You should use this code to tell Googlebot that a page or website has been permanently moved to a new location.

  •  302 (Temporary Move): The server is currently responding to requests from a web page in a different location, but the requester should continue to use the original location to respond to future requests. This code is similar to the 301 code that responds to get and head requests. It will automatically redirect the requester to a different location. However, this code should not be used to tell Googlebot that a web page or website has moved, because Googlebot will continue to crawl the original location. and indexed.

  • 303 (View Other Locations): The server returns this code when the requester should use separate get requests to different locations to retrieve the response. For all requests outside the head, the server will automatically go to other locations;

  • 304 (Unmodified): The requested web page has not been modified since the last request. When the server returns this response, no web page content is returned; the server should be configured to return this response (called the if-modified-Since HTTP header) if the web page has not changed since the requester's last request. The server can tell googlebot that the page has not changed since the last time it was crawled, thereby saving bandwidth and overhead.

  • 305 (Using Proxy): The requester can only use a proxy to access the requested web page. If the server returns this response, it also indicates that the requester should use a proxy.

  • 307 (Temporary Redirect): The server is currently responding to the request from a web page in a different location, but the requester should continue to use the original location to respond to future requests. This code is the same as the response get and The 8aa2918506a11e8a886e85949f1eb0795db79b134e9f6b82c0b36e0489ee08ed code requested by head is similar and will automatically transfer the requester to a different location, but it should not tell googlebot that a certain page or website has moved, because googlebot will continue to crawl original location and indexed.

7 ways to use PHP header

1. Jump to page

header('Location:'.$url); //Location和":"之间无空格。

2. Declare content-type

header('content-type:text/html;charset=utf-8');

3. Return response status code

header('HTTP/1.1 404 Not Found');

4. Execute jump after a certain time

header('Refresh: 10; url=//m.sbmmt.com/'); //10s后跳转。

5. Control browser cache

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache");

6. . Perform http verification

header('HTTP/1.1 401 Unauthorized'); header('WWW-Authenticate: Basic realm="Top Secret"');

7. Perform download operation

header('Content-Type: application/octet-stream'); //设置内容类型 header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件 header('Content-Transfer-Encoding: binary'); //设置传输方式 header('Content-Length: '.filesize('example.zip')); //设置内容长度

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is 302 error in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn