require_once needs to pay attention to the problem and set the include path

黄舟
Release: 2023-03-11 12:20:01
Original
2242 people have browsed it

Set common files that need to be included: (in the project root directory)

include.php

<?php
    header("content-type:text/html;charset=utf-8");
    date_default_timezone_get("PRC");
    session_start();
    //_FILE_返回是当前代码所在文件(而不是url所在文件)完整(绝对)路径
    //dirname($file_name)返回$file_name所在层目录名
    define("ROOT",dirname(FILE));
    set_include_path(".".PATH_SEPARATOR.ROOT."/core".PATH_SEPARATOR.ROOT."/configs".PATH_SEPARATOR.get_include_path());
    require_once &#39;mysql.func.php&#39;;
    require_once &#39;image.func.php&#39;;
    require_once &#39;common.func.php&#39;;
    require_once &#39;string.func.php&#39;;
    require_once &#39;page.func.php&#39;;
    require_once &#39;configs.php&#39;;
    require_once &#39;admin.inc.php&#39;;
    require_once &#39;cate.inc.php&#39;;
    require_once &#39;pro.inc.php&#39;;
    require_once &#39;album.inc.php&#39;;
    require_once &#39;user.inc.php&#39;;
    require_once &#39;upload.func.php&#39;;
    connect();
?>
Copy after login


Then, other pages directly require_once(' ./include.php') //relative to the path of include.php

But problems may occur when files are nested and included

The following is a detailed explanation

Click to open the link

Therefore, to be safe, it is best to add dirname(FILE) at the beginning of

require_once (dirname(FILE).&#39;/&#39;.&#39;../include.php&#39;);
Copy after login

.'/'

The above is the detailed content of require_once needs to pay attention to the problem and set the include path. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template