search
HomeBackend DevelopmentPHP ProblemHow to set response header information in php

How to set response header information in php

Sep 18, 2021 pm 04:35 PM
phpresponse header

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);

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);
}
?>

2, php

print_r(getallheaders());

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);
?>

2 , php

print_r(getallheaders());

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!

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version