This article mainly introduces PHP implementation using Curl analog IP and the source of the method of access, interested friends under the reference, I hope to be helpful to everyone.
For Web sites that have restricted IP and sources, access is not accessible using the normal access method. This article describes a method that uses the PHP Curl class to emulate IP and source, accessing sites that restrict IP and origin.
1. Set page limit IP and source access
server.php
<?php$client_ip = GetIP (); $referer = Getreferer (); $allow _ip = ' 192.168.1.100 '; $allow _referer = ' http://www.jb51.net ' If ($client _ip== $allow _ip && strpos ($referer, $allow _referer) ===0) { echo ' Allow access ';} else{ echo ' Deny Access ';} Get visitor ipfunction GetIP () { if (!empty ($_server[' http_client_ip ')) { $cip = $_server[' http_client_ip ']; } ElseIf (!empty ($_server[' http_x_forwarded_for ')) { $cip = $_server[' http_x_forwarded_for ']; } ElseIf (!empty ($_server[' remote_addr ')) { $cip = $_server[' remote_addr ']; } else{ $cip = '; } return $CIP;} Get visitor Source Function Getreferer () { if (isset ($_server[' http_referer ')) { return $_server[' Http_referer ']; } return ';}? >
2. Normal access Using Curl
<?phpfunction Docurl ($url, $data =array (), $header =array (), $timeout =30) { $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_httpheader, $header); curl_setopt ($ch, Curlopt_post, true); curl_setopt ($ch, Curlopt_postfields, Http_build_query ($data)); curl_setopt ($ch, Curlopt_returntransfer, true); curl_setopt ($ch, Curlopt_timeout, $timeout); $response = curl_exec ($ch); if ($error =curl_error ($ch)) {die ($error); } Curl_close ($ch); return $response;} Call the ' http://www.example.com/server.php '; $response = Docurl ($url); Echo $response; >
Return to Deny access
3. Use Curl to simulate IP and source access
Analog sources
curl_setopt ($ch, Curlopt_referer, ' source ');
Analog IP
curl_setopt ($ch, Curlopt_httpheader, Array (' CLIENT-IP: Analog ip ', ' x-forwarded-for: Analog IP '));
The complete code is as follows:
<?phpfunction Docurl ($url, $data =array (), $header =array (), $referer = ", $timeout =30) { $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_httpheader, $header); curl_setopt ($ch, Curlopt_post, true); curl_setopt ($ch, Curlopt_postfields, Http_build_query ($data)); curl_setopt ($ch, Curlopt_returntransfer, true); curl_setopt ($ch, Curlopt_timeout, $timeout); Analogue source curl_setopt ($ch, Curlopt_referer, $referer); $response = curl_exec ($ch); if ($error =curl_error ($ch)) {die ($error); } Curl_close ($ch); return $response;} Call the ' http://www.example.com/server.php ': $data = Array ();//Set Ip$header = Array ( ' client-ip:192.168.1.100 ' , ' x-forwarded-for:192.168.1.100 ');//Set Source $referer = ' http://www.jb51.net/'; $response = Docurl ($url, $data, $ Header, $referer, 5); Echo $response;? >
Return to allow access
The above is the whole content of this article, I hope that everyone's study has helped.