-3

我正在尝试将刷新用作重定向功能,因为标头由于某种原因不起作用。这是我的一段代码,当我运行它时会刷新循环:

----------不工作 /----------------

$url = $_SERVER['REQUEST_URI'];
$url = substr($url, 10); //cuting url to "index.php?site=page"
echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$url.'">';

- - - - - 在职的 / - - - - - - - -

$url = 'index.php?site=page';
echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$url.'">';

----------全功能 /----------------

elseif($action=="nfvi") {
        safe_query("UPDATE ".PREFIX."nfv SET nfv=nfv+1");
    $url = $_SERVER['REQUEST_URI'];
    $url = substr($url, 10);
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$url.'">';
}
4

2 回答 2

1

为什么不使用 HTTP 标头?

<?php
$url = "http://www.google.com";

header("Location: index.php?site=" . urlencode($url)); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

请参阅手册

于 2013-02-22T01:15:58.003 回答
1

您可以使用CURL作为客户端启动:

它将平衡 CURL_SET TIMEOUT:curl_setopt($ch, CURLOPT_TIMEOUT, 3);

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ping_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_POST, true ); curl_setopt($ch, CURLOPT_POSTFIELDS, trim($request)); $result = curl_exec($ch); curl_close($ch);

如果标题位置不起作用,您是否检查标题中的刷新页面?

 header("Refresh: 2;");

如果标头不起作用,请使用 fsockets 读取数据:

$host = 'www.example.com';
$service_uri = '/cgi-bin/processACT';
$vars ='code=22&act=TEST';

$header = "Host: $host\r\n";
$header .= "User-Agent: PHP Script\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($vars)."\r\n";
$header .= "Connection: close\r\n\r\n";

$fp = pfsockopen("ssl://".$host, 443, $errno, $errstr);
if (!$fp) {
   echo "$errstr ($errno)<br/>\n";
   echo $fp;
} else {
    fputs($fp, "POST $service_uri  HTTP/1.1\r\n");
    fputs($fp, $header.$vars);
    fwrite($fp, $out);
    while (!feof($fp)) {
    echo fgets($fp, 128);
    }
    fclose($fp);
} 

sleep(3)而不是用你想要暂停的第二个循环。

为什么在 PHP 中使用元标记而不是标头?

header('Location: '.$url);

或完整的标头重定向示例:

$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
于 2013-02-22T01:16:01.567 回答