Home Backend Development PHP Tutorial PHP跳转Header(“location:”)的注意事项

PHP跳转Header(“location:”)的注意事项

Jun 20, 2016 pm 01:05 PM
header

header(“Location: “) 作为php的转向语句 ,可以使当前页面跳转到其他页面。在使用中需要注意:

1、用法

header(“Location:http://www.scutephp.com/ ”)
Copy after login

2.header前应没有任何输出。

3.如果之前有输出:

(1)会出现类似如下报错:Warning: Cannot modify header information – headers already sent by (output started at …….php:12) in …….php on line N

(2)这时可以使用ob,它可以是在服务器端先存储有关输出,等待适当的时机再输出。如果不使用则为运行一句,输出一句,发现header语句就会报错。

具体的语句有: ob_start(); ob_end_clean();ob_flush();………

4、在header(“Location:http://www.scutepp.com/”)后要及时exit

否则他是会继续执行的,虽然在浏览器端你看不到相应的数据出现,但是如果你进行抓包分析的话,你就会看到下面的语句也是在执行的。而且被输送到了浏览器客户端,只不过是没有被浏览器执行为html而已(浏览器执行了header进行了转向操作)。

所以,标准的使用方法是 :

ob_start();

……..

if (…… ){

ob_end_clean();

header(“Location:http://www.yanfei.info/ ”);

exit;

else{

……….

ob_flush(); //可略
Copy after login

 


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does SpringBoot pass parameters in the Header through Feign calls? How does SpringBoot pass parameters in the Header through Feign calls? May 16, 2023 pm 08:38 PM

[SpringBoot] Passing parameters in the Header through Feign calls How to pass Header parameters through Feign Problem description When we use Feign to request the Api interface of another service in Spring Cloud, there is a need to pass the parameters in the Header. If no special processing is done, it will The parameters in the Header will be lost. Solution 1: Pass it through @RequestHeader(name="headerName"). For example: Feign is defined as follows @FeignClient(name="service-name")pub

What does linux header mean? What does linux header mean? Jul 18, 2023 pm 03:34 PM

The Linux header refers to the beginning of a file or data stream, which is used to contain metadata about the content. By correctly writing and using Header files, developers can better utilize system resources and improve code readability and Maintainability.

How to use PHP header() method to adjust web pages How to use PHP header() method to adjust web pages Mar 28, 2023 pm 01:54 PM

PHP is a powerful programming language that can be used to create dynamic websites and web applications. One of the most powerful features is PHP’s header() method. In this article, we will explore how to use PHP’s header() method to adjust web pages.

How to jump in php header How to jump in php header Dec 02, 2022 am 09:14 AM

How to implement jump in php header: 1. Use "Header("Location:$url");" syntax to implement jump; 2. Use if judgment to implement jump, with jump statements such as "if($_COOKIE[" u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');".

What is the difference between html5 tag head and header? What is the difference between html5 tag head and header? Jan 17, 2022 am 11:10 AM

Differences: 1. The head tag is used to define the head of the document, which is a container for all head elements, and the header tag is used to define the header (introduction information) of the document; 2. All browsers support the head tag, and older versions of browsers None of the browsers support the header tag, and browsers such as IE9+ and above are required to support the header tag.

How Nginx distributes through the identity in the header How Nginx distributes through the identity in the header May 11, 2023 pm 04:01 PM

Nginx can distribute requests to different servers based on custom identifiers in the request headers. Specifically, you can use the map directive to map the custom identifier in the request header to a different backend server address, and then use the proxy_pass directive to forward the request to the corresponding backend server. The following is an example configuration file: http{map$http_my_header$backend{defaultbackend1.example.com;value1backend2.example.com;value2backend3.example.com;}upstreambackend1{serv

Complete list of PHP file download functions: analysis of file download examples of readfile, header, Content-Disposition and other functions Complete list of PHP file download functions: analysis of file download examples of readfile, header, Content-Disposition and other functions Nov 18, 2023 pm 03:26 PM

Complete list of PHP file download functions: File download example analysis of readfile, header, Content-Disposition and other functions. File download is one of the essential functions in Web applications, and PHP, as a widely used Web development language, provides many A function and method to implement file downloading. This article will introduce commonly used file download functions in PHP, including readfile, header, Content-Dispo

How to define Header header information through Nginx How to define Header header information through Nginx May 16, 2023 am 11:52 AM

By modifying the nginx conf file, you can easily customize the HTTPHeader. Nginx uses the ngx_headers_more module to add and delete outbound and inbound Header information. By default, this module is not added to the source code of Nginx. If you want to use related functions, you need to add this module when compiling Nginx. The Nginx in my server did not add this module when compiling. Use -V to view the current Nginx compilation parameters: [root@z-dig~]#nginx-Vnginxversion:www.z-dig.combuiltbygcc4.4.720120313(RedHat4

See all articles