I am trying get my head around how you can run the following php:
$html = file_get_contents(curPageURL()); //where curPageURL() returns the current page URL
without causing an infinite loop
EDIT
I need to have the contents of the entire, rendered page, in a variable.
I have tried:
if(!isset($_COOKIE['page_fetch'])){
$html = file_get_contents(curPageURL());
setcookie('page_fetch',$html, time()开发者_开发问答+20,'/');
} else {
$html = $_COOKIE['page_fetch'];
}
I'm not sure why you'd want to, but could you pass a variable telling it to stop?
if (!$_REQUEST['stop']) {
$url = curPageURL() . '?stop=1';
$html = file_get_contents($url);
}
There is no way this would not result in an infinite loop.
What your asking could be explained with this fictional example:
function hi() {
echo '<div>';
hi();
echo '</div>';
}
So the answer likely is: you don't want this! You're likely trying to do something else, but not explaining it well. So start with stating what you're actually trying to achieve.
You can't. It is, by definition, infinitely recursive.
What I guess you want is a slightly different version of the current page (i.e. one which doesn't load itself again). See answer from newtron for how to do that.
I don't know what the problem is with the cookie, perhaps a maximum size or a time-out when the content is sent from the client to the server.
But as you need the value on the server-side, why don't you use a session variable in combination with a time value so that you know if it has expired?
精彩评论