为什么PHP论坛系统中的一个文件不能连接到其他文件?

为什么PHP论坛系统中的一个文件不能连接到其他文件?,php,sql,mysqli,forum,Php,Sql,Mysqli,Forum,我正在一个PHP论坛上工作,该论坛涉及多个相互关联的页面。我正试图将分类主题显示在单独的页面上,但由于某种原因,topics.php文件正在连接到其余文件。当我点击主页按钮或尝试注销时,页面不会改变。我通过单独的函数文件做所有事情 以下是topics.php文件: <?php session_start(); include ('../db.php'); $first_name = $_SESSION['first_name']; $last_name = $_SESSION['l

我正在一个PHP论坛上工作,该论坛涉及多个相互关联的页面。我正试图将分类主题显示在单独的页面上,但由于某种原因,topics.php文件正在连接到其余文件。当我点击主页按钮或尝试注销时,页面不会改变。我通过单独的函数文件做所有事情

以下是topics.php文件:

    <?php
session_start();
include ('../db.php');
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];

// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
  $_SESSION['message'] = "You must log in before viewing your profile page!";
  header("location: error.php");
}
else {
    // Makes it easier to read
    $first_name = $_SESSION['first_name'];
    $last_name = $_SESSION['last_name'];
    $email = $_SESSION['email'];
    $active = $_SESSION['active'];
}

include ('content_function.php');

 ?>
<!DOCTYPE html>
<html lang="en">

  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Home</title>

    <!-- Bootstrap core CSS -->
    <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom fonts for this template -->
    <link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>

    <!-- Custom styles for this template -->
    <link href="css/clean-blog.min.css" rel="stylesheet">
    <link href="main.css" type="text/css" rel="stylesheet" />
  </head>

  <body>

    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
      <div class="container">
        <a class="navbar-brand" href="../logout.php">Logout</a>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          Menu                                      <!-- Right above this ^ comment was this code: data-toggle="collapse" #NeverForget-->
          <i class="fa fa-bars"></i>
        </button>
        <div  class="nav-link" id="navbarResponsive">
          <ul  class="navbar-nav ml-auto">
            <li class="nav-link" class="nav-item">
              <a  href="index2.php">Home</a>
            </li>
            <li class="nav-link" class="nav-item">
              <a  href="about.html">About</a>
            </li>
            <li class="nav-link" class="nav-item">
              <a  href="msgBoard.php">Posts</a>
            </li>
            <li class="nav-link" class="nav-item">
              <a  href="contact.html">Contact</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>

    <!-- Page Header -->
    <header class="masthead" style="background-image: url('img/bible.jpg')">
      <div class="overlay"></div>
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-10 mx-auto">
            <div class="site-heading">
              <h1>Hi <?= $first_name ?>!</h1>
              <span class="subheading">Have Fun Exploring the Fellowship Wall</span>
            </div>
          </div>
        </div>
      </div>
    </header>

    <!-- Main Content -->
    <!-- <h1>Categories</h1>-->
    <style>
  body{
    background: tan;}
    </style>
    <!-- The above code is for the bacckground color on index2.php -->
          <hr>
          <!-- Pager -->
          <!--<div class="clearfix">
            <a class="btn btn-primary float-right" href="#">Older Posts &rarr;</a>
          </div>-->
        </div>
      </div>
    </div>
    <?php
  /*
   $select = mysqli_query($mysqli, "SELECT * FROM categories");
    while($row = mysqli_fetch_assoc($select)){
      echo  "<h1>", $row['category_title']."</h1>" ;
    }*/

    disptopics($_GET['cid'], $_GET['scid']);
    /*if (isset($_GET['cid'], $GET['scid'])){

      disptopics();
    }*/

     ?>
    <hr>

    <!-- Footer -->
    <footer>
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-10 mx-auto">
            <ul class="list-inline text-center">
              <li class="list-inline-item">
                <a href="#">
                  <span class="fa-stack fa-lg">
                    <i class="fa fa-circle fa-stack-2x"></i>
                    <i class="fa fa-twitter fa-stack-1x fa-inverse"></i>
                  </span>
                </a>
              </li>
              <li class="list-inline-item">
                <a href="#">
                  <span class="fa-stack fa-lg">
                    <i class="fa fa-circle fa-stack-2x"></i>
                    <i class="fa fa-facebook fa-stack-1x fa-inverse"></i>
                  </span>
                </a>
              </li>
            <!--  <li class="list-inline-item">
                <a href="#">
                  <span class="fa-stack fa-lg">
                    <i class="fa fa-circle fa-stack-2x"></i>
                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                  </span>-->
                </a>
              </li>
            </ul>
            <p class="copyright text-muted">Copyright &copy; Your Website 2018</p>
          </div>
        </div>
      </div>
    </footer>

    <!-- Bootstrap core JavaScript -->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Custom scripts for this template -->
    <script src="js/clean-blog.min.js"></script>

  </body>

</html>

家
菜单
你好 享受探索团契墙的乐趣 身体{ 背景:tan;}

下面是函数文件(content_function.php):


这里发生了很多坏事。请不要小看,我们都必须学习,这是一个过程。我不知道我是否能具体回答你的问题,但我可以指出一些事情

session_start();
include ('../db.php');
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];

// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
  $_SESSION['message'] = "You must log in before viewing your profile page!";
  header("location: error.php");
}
else {
    // Makes it easier to read
    $first_name = $_SESSION['first_name'];
    $last_name = $_SESSION['last_name'];
    $email = $_SESSION['email'];
    $active = $_SESSION['active'];
}
核弹上半部分:

session_start();
include ('../db.php');

// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
  $_SESSION['message'] = "You must log in before viewing your profile page!";
  header("location: error.php");
  exit; //<--- add this
}else {
    // Makes it easier to read
    $first_name = $_SESSION['first_name'];
    $last_name = $_SESSION['last_name'];
    $email = $_SESSION['email'];
    $active = $_SESSION['active'];
}
而不是在你的页面后面

<style>
  body{background: tan;}
</style>
您多次包含该文件,除了浪费处理能力和时间外,您还创建了对该文件的硬依赖。如果您选择稍后替换该文件,则有大量代码需要编辑。最好像您在页面顶部那样在
中包含/要求
,然后在调用函数时将连接传递到函数中。因此,与此相反:

      function getnumtopics($cat_id, $subcat_id){
         include ('../db.php');
         $select = mysqli_query($mysqli,  ...
做这样的事

     function getnumtopics($cat_id, $subcat_id, $mysqli){
         $select = mysqli_query($mysqli,  ...
当你调用它时,只要把它添加到调用中,只要它包含在调用它的文件中。如果您不确定并且需要在多个文件中使用它,那么您可以使用“require_once”(稍后将对此进行详细介绍)。这导致的问题的一个例子。比如说,你想创建一个新的DB文件(无论出于什么原因,我想不出一个,但它可能会发生)。将该文件名备份到所有函数中后,如果不修改页面中使用的所有函数(其中一些函数可能在其他页面上使用),就无法在一个页面上测试它。因此,您必须执行所有代码搜索和替换文件名。这可能是个坏例子,但这是我现在能想到的最好的例子。这只是一种不好的做法,有几个原因

需要Vs需要一次

文件1

  require_once('../db.php');
  require_once('../file2.php');
  ....
  getnumtopics($cat_id, $subcat_id, $mysqli);
文件2

  require_once('../db.php');
  ....
  dispsubcategories($cat_id, $subcat_id, $mysqli);
在本例中,File1需要DB,File2也需要。但是,您也可以在不需要的文件3中使用文件2:

文件3

require_once('../file2.php');
在这种情况下,如果您只需要它,那么您应该不需要包含它。然后在File1案例中获得2份副本,在File3案例中获得1份副本。您不能只将其包含在文件2中,因为文件1也需要它。因此,您可以使用
\u一次
,这可以确保如果它已经准备好了,您就不会再包含它,但如果它没有包含,它也会包含(这在我的头脑中更有意义)

包括与需求

至于包含vs需要。Include不会抛出任何错误如果文件丢失,则require will。因此,您必须问问自己,如果找不到文件x(要包含的文件),该脚本是否正常工作。在这种情况下,答案是否定的。所以你应该把它设为必选项。这将帮助您在文件丢失时调试错误。你可以把它移植到Linux上,在Linux中大小写(大写vs小写)很重要,它在Windows上运行得很好,但现在它被破坏了。这将告诉您缺少所需的文件,而不是因为无法连接到DB或调用空资源上的DB函数而导致一般错误

萨默里

至于为什么它会合并页面,我不确定这可能是因为没有调用exit,但我不能肯定,因为我没有详细研究过不调用exit的行为,我只知道你应该总是在重定向或奇怪的事情发生后调用它

我也不能说这一切都是错的,这只是我马上看到的,我会改变的


希望这能有所帮助。

这里发生了很多坏事。请不要小看,我们都必须学习,这是一个过程。我不知道我是否能具体回答你的问题,但我可以指出一些事情

session_start();
include ('../db.php');
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];

// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
  $_SESSION['message'] = "You must log in before viewing your profile page!";
  header("location: error.php");
}
else {
    // Makes it easier to read
    $first_name = $_SESSION['first_name'];
    $last_name = $_SESSION['last_name'];
    $email = $_SESSION['email'];
    $active = $_SESSION['active'];
}
核弹上半部分:

session_start();
include ('../db.php');

// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
  $_SESSION['message'] = "You must log in before viewing your profile page!";
  header("location: error.php");
  exit; //<--- add this
}else {
    // Makes it easier to read
    $first_name = $_SESSION['first_name'];
    $last_name = $_SESSION['last_name'];
    $email = $_SESSION['email'];
    $active = $_SESSION['active'];
}
而不是在你的页面后面

<style>
  body{background: tan;}
</style>
您多次包含该文件,除了浪费处理能力和时间外,您还创建了对该文件的硬依赖。如果您选择稍后替换该文件,则有大量代码需要编辑。最好像您在页面顶部那样在
中包含/要求
,然后在调用函数时将连接传递到函数中。因此,与此相反:

      function getnumtopics($cat_id, $subcat_id){
         include ('../db.php');
         $select = mysqli_query($mysqli,  ...
做这样的事

     function getnumtopics($cat_id, $subcat_id, $mysqli){
         $select = mysqli_query($mysqli,  ...
当你调用它时,只要把它添加到调用中,只要它包含在调用它的文件中。如果您不确定并且需要在多个文件中使用它,那么您可以使用“require_once”(稍后将对此进行详细介绍)。这导致的问题的一个例子。比如说,你想创建一个新的数据库文件(