dongyu9894 2013-06-12 20:51
浏览 8
已采纳

PHP'包含'问题 - 似乎无法包含正常工作

I'm having a spot of bother with php includes. I have the following file structure

htdocs

  • index.php
  • login.php

php_includes

  • db_conx.php
  • check_user_status.php

within the db_conx.php file i've creates a variable $number = 10; for testing.

The db_conx file is included in the check_user_status.php file with the following code:

include_once("db_conx.php");

and that is working fine - i.e. i can echo $number and get 10.

However, I'm including the check_user_status.php file at the top of login.php with this code:

include_once("php_includes/db_conx.php");

and on this page I'm unable to echo out $number on this page (check_user_status.php).

I'm going to need this script included in many pages (since it checks whether the user is logged in or not). Am I doing something strange with the paths?

  • 写回答

3条回答 默认 最新

  • duanjucong3124 2013-06-12 20:59
    关注

    For relative paths you need to do this.

    include_once("../php_includes/db_conx.php");
    

    To break this down.

    Your Current working directory is initially going to be htdocs/ if your hit that file in your browser.

    the .. back you up one directory level (so the directory that contains both htdocs and php_includes)

    then you want to follow down php_includes to get to db_conx.php.

    This will become a problem when you do a file in a subdirectory. Assuming you and a page2.php to a htdocs/subpages/

    Now if we follow those same steps we are not going to arrive at the same location.

    A better approach is to get the path relative to an absolute location. I like using the document root (htdocs in your case), so:

     include($_SERVER["DOCUMENT_ROOT"]."/../php_includes/db_conx.php");
    

    will refer to the same place on the file system regardless of where it is used.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能