如何将页面内容加载到自身中而不导致无限循环?
我正在尝试了解如何运行以下 php:
$html = file_get_contents(curPageURL()); //where curPageURL() returns the current page URL
而不导致无限循环
编辑
我需要将整个渲染页面的内容放在一个变量中。
我尝试过:
if(!isset($_COOKIE['page_fetch'])){
$html = file_get_contents(curPageURL());
setcookie('page_fetch',$html, time()+20,'/');
} else {
$html = $_COOKIE['page_fetch'];
}
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'];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定您为什么想要这样做,但是您可以传递一个变量来告诉它停止吗?
I'm not sure why you'd want to, but could you pass a variable telling it to stop?
这不可能不导致无限循环。
你的问题可以用这个虚构的例子来解释:
所以答案可能是:你不想要这个!您可能正在尝试做其他事情,但没有很好地解释它。因此,首先要说明您实际上想要实现的目标。
There is no way this would not result in an infinite loop.
What your asking could be explained with this fictional example:
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.
你不能。根据定义,它是无限递归的。
我想您想要的是当前页面的稍微不同的版本(即不会再次加载自身的版本)。有关如何做到这一点,请参阅 newtron 的答案。
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.
我不知道 cookie 的问题是什么,可能是最大大小或内容从客户端发送到服务器时超时。
但是,由于您需要服务器端的值,为什么不将会话变量与时间值结合使用,以便知道它是否已过期?
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?