How to set the referer address in php: 1. Forge the referer address through CURL; 2. Set the referer address using SOCKET; 3. Use the "file_get_contents" method to forge and set the referer address.
Recommended: "PHP Video Tutorial"
Use PHP to forge the referer address
Many times we use the referer address. By judging where the previous page came from, we can learn a lot of information, but now the referer is not that reliable data because we can forge the referer address. Here we introduce the implementation methods of CURL, SOCKET, and file_get_contents respectively. The detailed codes are as follows:
CURL method
$ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, "http://www.hujuntao.com"); curl_setopt ($ch, CURLOPT_REFERER, "http://www.hujuntao.com/"); curl_exec ($ch); curl_close ($ch);
SOCKET method
$server = ‘www.hujuntao.com’; $host = ‘www.hujuntao.com’; $target = ‘index.php’; $referer = ‘http://www.hujuntao.com/’; // Referer $port = 80; $fp = fsockopen($server, $port, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)\n"; } else { $out = "GET $target HTTP/1.1\r\n"; $out .= "Host: $host\r\n"; $out .= "Referer: $referer\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); }
file_get_contents method
$opt=array(‘http’=>array(‘header’=>"Referer: $refer")); $context=stream_context_create($opt); $file_contents = file_get_contents($url,false, $context);
Through the above In the code, we disguise the referer address as http://www.hujuntao.com. You can write a piece of code:
$_SERVER['HTTP_REFERER'];
to view this referer address. It’s that simple, so the referer is not reliable. Data.
The above is the detailed content of How to set a fake referer address in PHP. For more information, please follow other related articles on the PHP Chinese website!