访问其他页面时重定向到index.php

时间:2013-01-24 07:23:15

标签: php

我有这个php页面

的index.php

<?php
    include 'viewannouncement.php';
?>

viewannouncement.php

<?php     
          $result = mysql_query("SELECT announcement_date,announcement_title,
          announcement_text FROM sp_announcement") or die(mysql_error());  

        echo "<table id=\"newspaper-b\">";
        echo "<thead>";
        echo "<tr>";
        echo "<th scope=\"col\">Date</th>";
        echo "<th scope=\"col\">Title</th>";
        echo "<th scope=\"col\">Announcement</th>";
        echo "</tr>";
        echo "</thead>";

        while($row = mysql_fetch_array( $result )) {
                echo "<tr>";
                echo '<td>' . $row['announcement_date'] . '</td>';
                echo '<td>' . $row['announcement_title'] . '</td>';
                echo '<td>' . $row['announcement_text'] . '</td>';
                echo "</tr>"; 
        } 
        echo "</table>";
        echo "<br>";
        echo "<br>";
?>

在index.php中它有一个包含和查看viewannouncement.php的选项卡,问题是当我手动访问子页面时会留下白屏,我怎么能让它重定向到index.php并仍然可以通过index.php中的标签

我希望你们能帮助我,非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

通常人们会设置一个变量来检查文件是否包含在内, 为什么不做这样的事情? 在索引文件中

<?php
$fromIndex = true;
include 'viewannouncement.php';
?>

然后在你的文件viewannouncement.php中,在顶部试试。

if(!isset($fromIndex)) {
  header('Location: /index.php');
  die;
}