1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

php riddle - would like to connect to a site as google search referrer

Discussion in 'PHP & Perl' started by hardhat, Jan 5, 2015.

  1. hardhat

    hardhat Junior Member

    Joined:
    Jan 19, 2010
    Messages:
    193
    Likes Received:
    11
    hi php masters,

    i would like to connect to pages like i'm a user that searched something on google and clicked on one of the results and came to my site.

    i use this demo code (i think i found it here or on stackoverflow), but i'm missing something there:
    1,. accept and save cookies
    2. send the referer headers properly.

    can anyone here help or have a similar script in php?

    see the code:
    Code:
    echo geturl('http://mysite.com', 'http://www.google.com?q="searchkeyword"');
    
        function geturl($url, $referer) { 
    
            $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg,text/html,application/xhtml+xml'; 
            $headers[] = 'Connection: Keep-Alive'; 
            $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; 
            $useragent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;  .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'; 
    
            $process = curl_init($url); 
            curl_setopt($process, CURLOPT_HTTPHEADER, $headers); 
            curl_setopt($process, CURLOPT_HEADER, 0); 
            curl_setopt($process, CURLOPT_USERAGENT, $useragent);
            curl_setopt($process, CURLOPT_REFERER, $referer);
            curl_setopt($process, CURLOPT_TIMEOUT, 30); 
            curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
            curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 
    
            $return = curl_exec($process); 
            curl_close($process); 
    
            return $return; 
        }
    
    
    
    
    
    
    
     
  2. capripio

    capripio Regular Member

    Joined:
    Dec 25, 2010
    Messages:
    248
    Likes Received:
    157
    use this and you are good to go.

    Code:
    function get_url_content($url,$proxy = null,$timeout = 0) {
        $ch = curl_init();
        if($proxy != null)
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0";
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        if(($data = curl_exec($ch)) == false){
            return FALSE;
            die();
        }
        curl_close($ch);
        return $data;
    }
    
     
  3. pavan

    pavan Elite Member

    Joined:
    Mar 30, 2008
    Messages:
    1,819
    Likes Received:
    456
    I don't see any problem with the referrer
    for cookies you can use the following code

    Code:
    echo geturl('http://mysite.com', 'http://www.google.com?q="searchkeyword"');
    
        function geturl($url, $referer) { 
    
            $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg,text/html,application/xhtml+xml'; 
            $headers[] = 'Connection: Keep-Alive'; 
            $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; 
            $useragent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;  .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'; 
    		$cookies = "cookies.txt";
            $process = curl_init($url); 
            curl_setopt($process, CURLOPT_HTTPHEADER, $headers); 
            curl_setopt($process, CURLOPT_HEADER, 0); 
            curl_setopt($process, CURLOPT_USERAGENT, $useragent);
            curl_setopt($process, CURLOPT_REFERER, $referer);
            curl_setopt($process, CURLOPT_TIMEOUT, 30); 
            curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
            curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
    		curl_setopt($process, CURLOPT_COOKIEFILE, $cookies);
            curl_setopt($process, CURLOPT_COOKIEJAR, $cookies);
    
            $return = curl_exec($process); 
            curl_close($process); 
    
            return $return; 
        }