Php uses curl to simulate ip addresses and sources for access implementation, curlip

Source: Internet
Author: User

Php uses curl to simulate ip addresses and sources for access implementation, curlip

For websites with limited ip addresses and sources, normal access is not allowed. This article introduces a method to simulate ip addresses and sources by using the php curl class to access websites that restrict ip addresses and sources.

1. Set page restriction ip address and Source Access

Server. php

<? Php $ client_ip = getip (); $ referer = getreferer (); $ allow_ip = '2017. 168.1.100 '; $ allow_referer = 'HTTP: // www.bkjia.com'; if ($ client_ip = $ allow_ip & strpos ($ referer, $ allow_referer) = 0) {echo 'Allow access';} else {echo 'Deny access';} // obtain the visitor's 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;} // obtain the visitor source function getreferer () {if (isset ($ _ SERVER ['HTTP _ referer']) {return $ _ SERVER ['HTTP _ referer'];} return '';}?>

2. Use curl for normal access

<? 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 ($ e Rror = curl_error ($ ch) {die ($ error);} curl_close ($ ch); return $ response;} // call $ url = 'HTTP: // www.example.com/server.php'?response = doCurl ($ url); echo $ response;?>

Return deny access

3. Use curl to simulate ip addresses and sources for access

Analog Source

Curl_setopt ($ ch, CURLOPT_REFERER, 'source ');

Analog ip

Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('client-IP: analog ip', 'x-FORWARDED-FOR: Simulated 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, expires, $ 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); // simulate the source curl_setopt ( $ Ch, CURLOPT_REFERER, $ referer); $ response = curl_exec ($ ch); if ($ error = curl_error ($ ch) {die ($ error );} curl_close ($ ch); return $ response;} // call $ url = 'HTTP: // www.example.com/server.php'your response data = array (); // set IP $ header = array ('client-IP: 192.168.1.100 ', 'x-FORWARDED-FOR: 192.168.1.100'); // set source $ referer = 'HTTP: // www.bkjia.com/'{}response = doCurl ($ url, $ data, $ header, $ referer, 5); echo $ response; ?>

Return allow access

The above php uses curl to simulate ip addresses and sources for access implementation is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support for the customer's house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.