PHP implements the method of using curl to simulate IP and source access

Source: Internet
Author: User
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.


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.