I recently in the topic of learning php .. There is such a library as curl ...

I decided to try to make a mirror on localhost, I found a code plus .htaccess filled in on one of the blogs, but the result is this: when loading the first page, everything is displayed normally, it happens that the styles and js fall, but it does not matter; as soon as I try to follow the link to the redirect direct to an external page or 404 (((

I decided to try to write it myself, and that’s what happened:

$http = 'http://'; $domain = 'xxxx.ru'; $var = $_SERVER['REQUEST_URI']; $host = $_SERVER['HTTP_HOST']; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $http.$domain.$var); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $tuData = curl_exec($ch); $tuData = str_replace($domain , $host , $tuData); echo $tuData; 

    1 answer 1

    Throw the .htaccess and index.php files into the site root, inside index.php in the $base variable, change the address of the site that you will be proxying.

    index.php

     <?php session_start(); ob_start(); /* config settings */ $base = "http://www.bbc.co.uk"; //set this to the url you want to scrape $ckfile = '/tmp/simpleproxy-cookie-'.session_id(); //this can be set to anywhere you fancy! just make sure it is secure. /* all system code happens below - you should not need to edit it! */ //work out cookie domain $cookiedomain = str_replace("http://www.","",$base); $cookiedomain = str_replace("https://www.","",$cookiedomain); $cookiedomain = str_replace("www.","",$cookiedomain); $url = $base . $_SERVER['REQUEST_URI']; if($_SERVER['HTTPS'] == 'on'){ $mydomain = 'https://'.$_SERVER['HTTP_HOST']; } else { $mydomain = 'http://'.$_SERVER['HTTP_HOST']; } // Open the cURL session $curlSession = curl_init(); curl_setopt ($curlSession, CURLOPT_URL, $url); curl_setopt ($curlSession, CURLOPT_HEADER, 1); if($_SERVER['REQUEST_METHOD'] == 'POST'){ curl_setopt ($curlSession, CURLOPT_POST, 1); curl_setopt ($curlSession, CURLOPT_POSTFIELDS, $_POST); } curl_setopt($curlSession, CURLOPT_RETURNTRANSFER,1); curl_setopt($curlSession, CURLOPT_TIMEOUT,30); curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt ($curlSession, CURLOPT_COOKIEJAR, $ckfile); curl_setopt ($curlSession, CURLOPT_COOKIEFILE, $ckfile); //handle other cookies cookies foreach($_COOKIE as $k=>$v){ if(is_array($v)){ $v = serialize($v); } curl_setopt($curlSession,CURLOPT_COOKIE,"$k=$v; domain=.$cookiedomain ; path=/"); } //Send the request and store the result in an array $response = curl_exec ($curlSession); // Check that a connection was made if (curl_error($curlSession)){ // If it wasn't... print curl_error($curlSession); } else { //clean duplicate header that seems to appear on fastcgi with output buffer on some servers!! $response = str_replace("HTTP/1.1 100 Continue\r\n\r\n","",$response); $ar = explode("\r\n\r\n", $response, 2); $header = $ar[0]; $body = $ar[1]; //handle headers - simply re-outputing them $header_ar = explode("\n",$header); foreach($header_ar as $k=>$v){ if(!preg_match("/^Transfer-Encoding/",$v)){ $v = str_replace($base,$mydomain,$v); //header rewrite if needed header(trim($v)); } } //rewrite all hard coded urls to ensure the links still work! $body = str_replace($base,$mydomain,$body); print $body; } curl_close ($curlSession); 

    .htaccess

     Options +FollowSymLinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* /index.php [L] 

    Fully working PHP proxy within a single domain with support for cookies. This solution is also an excellent and easy to understand (due to the abundance of comments) an example of how this should work.

    This code is not mine, I took it from the Google Code repository and slightly corrected for compatibility with PHP 7.

    • Thank you so much for your help, but it didn’t work as it should for me ((is it possible that there are problems with LAN? I use openserver ... - Vinotti
    • Can you describe the problem in more detail? For example, what errors occurred to the script execution process. - neluzhin
    • I start on LAN, trying to follow the links, and after clicking on any of them, 404 climbs .. - Vinotti
    • Unfortunately, I am not familiar with the Open Server, so tell me: do you have Apache there? If yes, then set AllowOverride All for your site in the Apache settings to get the .htaccess file. - neluzhin
    • Changed, gives this here Function split () is deprecated in W: \ domains \ localhost \ sock \ index.php on line 75, - Vinotti