Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

Zend_Dojo Dijits and Numeric ID’s
Happy New Year from Edmonds Commerce
Easy Security for PHP Scripts
Secure and Insecure Contents in HTTPS

Most Popular Posts

PHP Email Attachment Function Freelance osCommerce UK Ultimate osCommerce Checkout - Fast and Friendly PHP : Dead Easy Excel Export

PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode

July 18th, 2008
Read More curl

If you are trying to get a curl script which needs follow on location functionality to run on a server which has either open_basedir or safe mode enabled you will get an error message similar to the following:

CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set

After a bit of digging, some kind soul has put a workaround here

Here is how to use the function

PLAIN TEXT
PHP:
  1. function curl($url){
  2. curl_setopt ($go, CURLOPT_URL, $url);
  3. //follow on location problems
  4.  
  5. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')){
  6.  
  7. curl_setopt ($go, CURLOPT_FOLLOWLOCATION, $l);
  8.  
  9. $syn = curl_exec($go);
  10.  
  11. }else{
  12.  
  13. $syn = curl_redir_exec($go);
  14.  
  15. }
  16.  
  17. curl_close($go);
  18. return $syn;
  19. }
  20.  
  21. //follow on location problems workaround
  22.  
  23. function curl_redir_exec($ch)
  24.  
  25. {
  26.  
  27. static $curl_loops = 0;
  28.  
  29. static $curl_max_loops = 20;
  30.  
  31. if ($curl_loops++>= $curl_max_loops)
  32.  
  33. {
  34.  
  35. $curl_loops = 0;
  36.  
  37. return FALSE;
  38.  
  39. }
  40.  
  41. curl_setopt($ch, CURLOPT_HEADER, true);
  42.  
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  44.  
  45. $data = curl_exec($ch);
  46.  
  47. list($header, $data) = explode("\n\n", $data, 2);
  48.  
  49. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  50.  
  51. if ($http_code == 301 || $http_code == 302)
  52.  
  53. {
  54.  
  55. $matches = array();
  56.  
  57. preg_match('/Location:(.*?)\n/', $header, $matches);
  58.  
  59. $url = @parse_url(trim(array_pop($matches)));
  60.  
  61. if (!$url)
  62.  
  63. {
  64.  
  65. //couldn't process the url to redirect to
  66.  
  67. $curl_loops = 0;
  68.  
  69. return $data;
  70.  
  71. }
  72.  
  73. $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
  74.  
  75. if (!$url['scheme'])
  76.  
  77. $url['scheme'] = $last_url['scheme'];
  78.  
  79. if (!$url['host'])
  80.  
  81. $url['host'] = $last_url['host'];
  82.  
  83. if (!$url['path'])
  84.  
  85. $url['path'] = $last_url['path'];
  86.  
  87. $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
  88.  
  89. curl_setopt($ch, CURLOPT_URL, $new_url);
  90.  
  91. return curl_redir_exec($ch);
  92.  
  93. } else {
  94.  
  95. $curl_loops=0;
  96.  
  97. return $data;
  98.  
  99. }
  100.  
  101. }

Feed | Respond | Trackback

Leave a Reply

  • RSS Feed
  • Categories

    • apache
    • barcode
    • business
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • edmondscommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • magento
    • management
    • mod_rewrite
    • mysql
    • oscommerce
    • php
    • plesk
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • scraping
    • search engine optimisation
    • security
    • seo
    • spidering
    • ubuntu
    • Uncategorized
    • vps
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css development directories find firefox google hosts file html javascript keywords links msn mysql myths operators oscommerce paid links paid placement performance php ppc reciprocal linking replace screen scraping search engine optimisation security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • Purashop : SEO Services
    • PHP: Recursive Create Path (if not exists)
    • Javascript Debugging
    • osCommerce Essential Modifications
    • Happy New Year from Edmonds Commerce
    • Magento UK
    • Favourite Firefox Extensions
    • CRELoaded Remove Google Ads -
    • EAN13 Barcode Check Digit with PHP
    • osCommerce Easypopulate File Creator Class

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design