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
PHP Twitter API - How to Pull in Multiple Users Tweets
How to Select MySQL Query with Foreign Language
Cookies Not Working on Different Pages
Download Multiple Files as a Zip Folder Using PHP
Do I Need to Escape My Variables If I Use MySQLi Prepared Statements
What Is the ASP.NET Equivalent to PHP's Echo
Installing Mcrypt on Osx 10.8.2 - PHP 5.3.15 with Homebrew
What Do \R and \N Mean in PHP (And Other Languages)
Symfony: How to Refresh the Authenticated User from the Database
Php: Populating an Array with the Names of the Next 12 Months
How to Verify the Requesting Server in PHP
Perl Equivalent of PHP's Escapeshellarg
Read and Write to a File While Keeping Lock
Calling PHP Function from Jquery
Mysql: Group by Two Columns and Sum