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)".
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: https://www.php.cn');
-
Grammar format 2:
header('Location: https://www.php.cn', true, 302);
One more thing, if you want to adapt to HTTPS/HTTP, this is enough:
header('Location: //www.php.cn', 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 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=http://www.php.cn/'); //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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor