Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.87 KB | None | 0 0
  1. <?php
  2. function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) {
  3.     $mr = $maxredirect === null ? 5 : intval($maxredirect);
  4.     if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
  5.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
  6.         curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
  7.     } else {
  8.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
  9.         if ($mr > 0) {
  10.             $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  11.  
  12.             $rch = curl_copy_handle($ch);
  13.             curl_setopt($rch, CURLOPT_HEADER, true);
  14.             curl_setopt($rch, CURLOPT_NOBODY, true);
  15.             curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
  16.             curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
  17.             do {
  18.                 curl_setopt($rch, CURLOPT_URL, $newurl);
  19.                 $header = curl_exec($rch);
  20.                 if (curl_errno($rch)) {
  21.                     $code = 0;
  22.                 } else {
  23.                     $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
  24.                     if ($code == 301 || $code == 302) {
  25.                         preg_match('/Location:(.*?)\n/', $header, $matches);
  26.                         $newurl = trim(array_pop($matches));
  27.                     } else {
  28.                         $code = 0;
  29.                     }
  30.                 }
  31.             } while ($code && --$mr);
  32.             curl_close($rch);
  33.             if (!$mr) {
  34.                 if ($maxredirect === null) {
  35.                     trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING);
  36.                 } else {
  37.                     $maxredirect = 0;
  38.                 }
  39.                 return false;
  40.             }
  41.             curl_setopt($ch, CURLOPT_URL, $newurl);
  42.         }
  43.     }
  44.     return curl_exec($ch);
  45. }
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement