Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示页面,直到满足条件,然后重定向_Php_Loops_Redirect_Conditional Statements - Fatal编程技术网

Php 显示页面,直到满足条件,然后重定向

Php 显示页面,直到满足条件,然后重定向,php,loops,redirect,conditional-statements,Php,Loops,Redirect,Conditional Statements,我有一个保留页,一旦该页被创建,它就会重定向到另一页 目前,我的解决方案是每5秒刷新一次保留页面,每次检查是否创建了新页面。问题是页面会闪烁 有没有一种方法可以在页眉中使用一个简单的while循环来实现这一点,但仍然显示保留页面html。谢谢当前代码如下 <?php ob_start(); $id = escapeshellarg($_GET['id']); $id = str_replace("'", "", $id); $url = 'http://sub.testxxx.com/'.

我有一个保留页,一旦该页被创建,它就会重定向到另一页

目前,我的解决方案是每5秒刷新一次保留页面,每次检查是否创建了新页面。问题是页面会闪烁

有没有一种方法可以在页眉中使用一个简单的while循环来实现这一点,但仍然显示保留页面html。谢谢当前代码如下

<?php
ob_start();
$id = escapeshellarg($_GET['id']);
$id = str_replace("'", "", $id);
$url = 'http://sub.testxxx.com/'. $id . '/index.html';
$handle = @fopen($url,'r');
if(!$handle) {
header("Refresh: 5; url=http://testxxx.com/loading.php?id=".$id);
ob_flush;
} else {
sleep(5);
header("Location: http://sub.testxxx.com/".$id);
}
?>
<html>
...........

</html>
<?
ob_flush;
?>

我将其分为两部分:1)一个服务器端脚本(php)检查页面是否已创建,2)一个客户端脚本(javascript)每隔n秒通过ajax调用php脚本。如果php脚本返回true,js会将客户端重定向到新的目标。无需重新加载整个页面-且无闪烁;)

您需要一些示例代码吗?

checkPage.php:

<?php
$id = escapeshellarg($_GET['id']);
$id = str_replace("'", "", $id);
$url = 'http://sub.testxxx.com/'. $id . '/index.html';
$handle = @fopen($url,'r');
if(!$handle) {
return true;
} else {
return false;
}
?>

我会用javascript调用而不是php刷新页面。谢谢你的帮助。恐怕由于我的无知,我有几个问题要问。1.checkpage.php会每隔5秒检查一次新页面的存在吗?2.我想使用page.php?id=xxx,其中xxx是作为$id-fyi捕获的someId变量。id被放入数据库中。有没有办法将这个变量传递给Ajax,以便checkPage.php?id=$id?像finction一样保存“$.Ajax…”并每隔5秒循环调用一次。您还可以在Page.php的开头检查数据库中的$id。再次感谢您的回复。我整天都在工作!我已经让checkpage.php正常工作了,但是我在ajax代码中遇到了php变量。我已经提交了一个新的问题,以便在您觉得还有更多可以添加的内容时显示编辑后的代码。非常感谢。谢谢你的帮助和好意。我正在研究SunnyTar建议的代码,但遇到了一个需要将php变量传递到Ajax的问题
<html>
<head></head>
<body>
<script type="text/javascript">
$.ajax({
  url: 'checkPage.php?id=someId',
  success: function(data) {
    $(location).attr('href','http://sub.testxxx.com/someId/index.html');
  }
});
</script>
</body>
</html>