How to set response header information in php

王林
Release: 2023-03-12 18:08:02
Original
4689 people have browsed it

The method for php to set response header information is to use the fsockopen() function to set it, for example [

How to set response header information in php

The operating environment of this article: windows10 system, php7, thinkpad t480 computer.

In PHP, if we need to set the response header information of the request server, we can use fsockopen and curl components. Some friends may instantly think of the header() function, but the header() function can only be used to set The client's response header information cannot set the server's header information. Let's give an example to illustrate.

For example:

1. Usage of header function

    header('WWW-Authenticate: Negotiate');
    header('User-Agent:Mozilla/5.0);
Copy after login

Multiple headers need to be written directly and cannot be connected together

2. Usage of fsockopen function

1, php

<?php
$fp = fsockopen("test.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET /2.php HTTP/1.1\r\n";
    $out .= "Host: test.com\r\n";
    $out .= "name:longqiqi\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>
Copy after login

2, php

print_r(getallheaders());
Copy after login
Copy after login

will return the header information of the request you set

3. Use of curl component

1, php

<?php
function FormatHeader($url, $myIp = null,$xml = null)
{
 // 解悉url
 $temp = parse_url($url);
 $query = isset($temp[&#39;query&#39;]) ? $temp[&#39;query&#39;] : &#39;&#39;;
 $path = isset($temp[&#39;path&#39;]) ? $temp[&#39;path&#39;] : &#39;/&#39;;
 
 $header = array (
 "POST {$path}?{$query} HTTP/1.1",
 "Host: {$temp[&#39;host&#39;]}",
 "Content-Type: text/xml; charset=utf-8",
 &#39;Accept: */*&#39;,
 "Referer: http://{$temp[&#39;host&#39;]}/",
 &#39;User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)&#39;,
 "X-Forwarded-For: {$myIp}",
 "Content-length: 380",
 "Connection: Close"
 );
 
 return $header;
}
 
$interface = &#39;http://test.com/2.php&#39;;
$header = FormatHeader($interface,&#39;10.1.11.1&#39;);
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $interface);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  //设置头信息的地方
curl_setopt($ch, CURLOPT_HEADER, 0);    //不取得返回头信息
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
 
var_dump($result);
?>
Copy after login

2 , php

print_r(getallheaders());
Copy after login
Copy after login

will return the header information of your own request

Recommended learning: php training

The above is the detailed content of How to set response header information in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template