移动到其他页面时,require 不起作用

问题描述 投票:0回答:1

导航栏.php

<?php
require 'config.php';

$admin = new Admin();

if (isset($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];
    $res = $admin->ret("SELECT * FROM `user` WHERE `user_id` = '$user_id'");
    $user = $res->fetch(PDO::FETCH_ASSOC);
    }
?>

当我在index.php页面时它工作正常并且需要config.php 但是当我转到其他页面时,它显示错误

警告 : require(config.php): 无法打开流: 没有这样的文件或目录 C:\xampp\htdocs\Web\includes avbar.php 在线的 2

致命错误 :未捕获错误:无法在 C:\xampp\htdocs\Web\includes 中打开所需的“config.php”(include_path='C:\xampp\php\PEAR') avbar.php:2 堆栈跟踪: #0 C:\xampp\htdocs\Web iews uth uth.php(20): include() #1 {main} 抛出 C:\xampp\htdocs\Web\includes avbar.php 在线的 2

directory

php
1个回答
0
投票

这是路径问题。

根据您提供的图像,您具有以下结构:

  • index.php
    config.php
    位于同一目录中 (
    root
    )
  • navbar.php
    嵌套在子目录中 (
    includes
    )

现在,脚本中的

require 'config.php'
行根本不引用任何文件夹,这意味着 PHP 将在与调用脚本相同的目录中查找。

  • index.php
    将在
    root
    目录中查找,并会在那里找到
    config.php
  • navbar.php
    将在
    includes
    子目录中查找...而
    config.php
    不会在那里!

解决方案是指定

config.php
文件的路径。例如:

require '/config.php'

© www.soinside.com 2019 - 2024. All rights reserved.