How to Ignore a Moved-Header with File_Get_Contents in PHP

How do I ignore a moved-header with file_get_contents in PHP?

There is no content there. The redirect happens in the HTTP response before any content would be sent.

The server decides what you get to see (or not).

How to get the real URL after file_get_contents if redirection happens?

You might make a request with cURL instead of file_get_contents().

Something like this should work...

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
if(preg_match('#Location: (.*)#', $a, $r))
$l = trim($r[1]);

Source

prevent javascript execution over file_get_contents

Kudos to jcubic for providing a link to an explanation of why his solution won't work ;)

There are only 2 robust solutions I know of:

1) use a markup language other than HTML which has a provable grammar and does not allow embedded scripting (BBCode?). This still requires that you validate the submission for compliance - but is simpler than for HTML.

2) apply a content security policy which does not allow inline javascript - this would be my preferred solution, not least because you can specify a reporting URL, allowing you to police what is happenning on the browser rather than relying on filtering on the server.

CSS file ignored when loaded with include in PHP

Try header("Content-type: text/css; charset: UTF-8"); with file_get_contents instead of include like this

if(preg_match('/css/',$uri)){
header("Content-type: text/css; charset: UTF-8");
if(!$file = file_get_contents("http://localhost:8080/project_name/webroot/$uri")) return 0;
echo $file;
}

file_get_contents when url doesn't exist

You need to check the HTTP response code:

function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
if(get_http_response_code('http://somenotrealurl.com/notrealpage') != "200"){
echo "error";
}else{
file_get_contents('http://somenotrealurl.com/notrealpage');
}


Related Topics



Leave a reply



Submit