Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a problem with php include section. In order to fully explain the problem, I created a test page for you.

First I want to show the schema of the files to you. Also you can download the test files from this LINK and you can test it online TEST LINK

enter image description here

As you can see, there is a subfolder in the htdocs (root) file and all php files in there. Now I'll show you the php code within the file, respectively.

appname/index.php

<?php include_once 'includes/config.php';?>
<div class="callPage">Click Here</div>
<div id="testBox"></div>

<script type="text/javascript">
  $(document).ready(function(){
      var siteurl = '<?php echo $url;?>';
      $("body").on("click",".callPage", function(){
       $.ajax({
            url: siteurl+'content/test',
            beforeSend: function() {
                //Do something
            },
            complete:function(){
              //Do something 
            },
            success:function(response){
                $("#testBox").html(response);
            }
            });
    });
     function LoadPage(){
       $.get(siteurl+'content/test', function(data) {
          $('#testBox').html(data);
       });
    }
    LoadPage(); 
  });
</script>  

appname/content/test.php

<?php 
include_once 'includes/config.php';
echo $text.'</br>';
echo $worked.'</br>';
?>

appname/includes/config.php

<?php 
$url = 'http://localhost:8888/';
$text = 'Well Come! How are you today ?';
$worked = 'It is working :)';
?>

When you open the TEST LINK, LoadPage(); javascript function will call test.php in the content file and display it in #testBox. First you will not see anything in #testBox from index.php . Because config.php can not be included from test.php .

I know if I change this line include_once 'includes/config.php'; from test.php like this include_once '/appname/includes/config.php'; then problem will be fix.

But if the pages multiply and, I want to use the files in the root (htdocs or www) folder, I need to delete appname (subfolder name) => include_once 'appname/includes/config.php'; from all files. It will be a big problem when these files multiply.

Actually the question is exactly:

How can we include php files without specifying the full path to the include, when the application's path relative to the DOCUMENT_ROOT is variable or unknown and include_path cannot be reliably modified by all application users?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
112 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is sometimes a problem with includes when you're not using the absolute path on the system.

Explanation

Depending on how PHP is running could affect the way include&require work, if PHP is running from inside the appname directory it will work fine if php is told it's running inside the appname directory via a connector it's fine. however, if PHP is run for example www-data@/usr/bin/# php /var/www/somesite.com/htdocs/appname/index.php the path can be broken.

Fix

if you use define("FS_ROOT", realpath(dirname(__FILE__))); as the first thing other than if ther is a namespace inside index.php you can then use include FS_ROOT."/includes/config.php"; this means file paths are used from the root of the system so it gets evaluated to include "/var/www/somesite.com/htdocs/appname/index.php"

Why this differs

This differs from ROOT_PATH as ROOT_PATH is sometimes set by PHP configuration by web hosts and this could be the problem. as the PHP execution path could be wrong casing the PHP host application to look in the wrong place for includes and requries.

This also means no include or require should ever be using ../ as you should know the path from your codebase.

your appname/index.php

<?php define("FS_ROOT", realpath(dirname(__FILE__)));
include_once FS_ROOT.'includes/config.php';?>
<div class="callPage">Click Here</div>
<div id="testBox"></div>

<script type="text/javascript">
  $(document).ready(function(){
      var siteurl = '<?php echo $url;?>';
      $("body").on("click",".callPage", function(){
       $.ajax({
            url: siteurl+'content/test',
            beforeSend: function() {
                //Do something
            },
            complete:function(){
              //Do something 
            },
            success:function(response){
                $("#testBox").html(response);
            }
            });
    });
     function LoadPage(){
       $.get(siteurl+'content/test', function(data) {
          $('#testBox').html(data);
       });
    }
    LoadPage(); 
  });
</script>

your appname/content/test.php

<?php 
# as this file is loaded directly and is not in the root directory
# we apend the dirname result with ../ so realpath can resolve the root directory for this site
define("FS_ROOT", realpath(dirname(__FILE__)."../"));
include_once FS_ROOT.'includes/config.php';
echo $text.'</br>';
echo $worked.'</br>';
?>

Ideally, you should go through a bootstrap and .htaccess so you don't have to change redefine the FS_ROOT in every file loaded.

you can do this by making sure mod_rewrite is enabled in apache

create file .htaccess in appname folder

RewriteCond %{REQUEST_URI} .(php)$
RewriteRule .* bootstap.php [L]

create bootstrap.php

define("FS_ROOT", realpath(dirname(__FILE__)));
include_once FS_ROOT.'includes/config.php';
if(file_exists(FS_ROOT.$_SERVER['REQUEST_URI']){
    include(FS_ROOT.$_SERVER['REQUEST_URI']);
}else{
    // 404
}

this means you don't require the include for the config as it's automaticly included before the script for that request is wanted this is just a base outline and is not secure (and could be easily exploited to reveal system files contents) I would highly recommend reading up on how to use MVC's and how they work it will give you a better understanding of loading files on demand and requiring files.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...