在正文

时间:2018-06-12 09:45:19

标签: php

我在标头()中遇到了问题。我知道它必须在任何HTML代码之前,但用它来替换它,以便它重定向到另一个URL。

这是代码(index.php):

<!DOCTYPE html>
<html lang="en">
<head>title</head>
<body>
<?php
$count = 1;
if ($count > 0) {
        header("Location:list.php?incluided");
    } else {
       header("Location:list.php?error");
    }
?>
<body>
<html>

我该如何更换?感谢。

解决方案,添加javascript并删除header():

<!DOCTYPE html>
<html lang="en">
<head>title</head>
<body>
<?php
$count = 1;
if ($count > 0) {
        print '
        <noscript>
          <meta http-equiv="refresh" content="0;>
        </noscript>
        <!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
        <script type="text/javascript">
          if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
          {
            document.write("redirecting...");
            var referLink = document.createElement("a");
            referLink.href = url;
            document.body.appendChild(referLink);
            referLink.click();
          }
          else { window.location.replace("list.php?incluided"); } // All other browsers
        </script>
        ';

    } else {

       print '
       <noscript>
         <meta http-equiv="refresh" content="0;>
       </noscript>
       <!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
       <script type="text/javascript">
         if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
         {
           document.write("redirecting...");
           var referLink = document.createElement("a");
           referLink.href = url;
           document.body.appendChild(referLink);
           referLink.click();
         }
         else { window.location.replace("list.php?error"); } // All other browsers
       </script>
       ';
    }
?>
<body>
<html>

1 个答案:

答案 0 :(得分:2)

<?php
$count = 1;
if ($count > 0)
    header("Location:list.php?incluided");
else
   header("Location:list.php?error");
die();
?>
<!DOCTYPE html>
<html lang="en">
<head>title</head>
<body>
<body>
<html>

只需将重定向代码放在最上面。