Search code examples
phpip-restrictions

After adding IP restriction I get a '403 error' for get_file_contents


I have the following code that determines if an image is an SVG or not so that it displays the source if it is (the SVG can then be coloured using CSS):

if (strpos('url_to_an_image', '.svg') !== false) {
    echo file_get_contents('url_to_an_image'); 
} else {
    echo '<img src="url_to_an_image" />';
}

This worked fine, until I added an IP restriction in htaccess when I get the following error:

file_get_contents(http://example.com/images/icons/icon-email.svg): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden

I have tried the solutions that suggest spoofing a real browser request; they don't work.

Is there another solution for this, or another approach I could use to display SVGs?


Solution

  • You're getting the 403 error because php doesn't seem to have the permission to open this file. What's your exact code to block the IPs? Do not use an URL when it's on your own server, just use an absolute path.