How do you know the correct path to use in a PHP require_once() statement

后端 未结 9 2131
别跟我提以往
别跟我提以往 2021-01-31 22:02

As many do I have a config.php file in the root of a web app that I want to include in almost every other php file. So most of them have a line like:

require_on         


        
9条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 22:59

    I include this code at the top of every page:

    //get basic page variables
    $self=$_SERVER['PHP_SELF']; 
    $thispath=dirname($_SERVER['PHP_SELF']);
    $sitebasepath=$_SERVER['DOCUMENT_ROOT'];
    
    //include the global settings, variables and includes
    
    include_once("$sitebasepath/globals/global.include.php");
    

    Include and require both take either a relative path or the full rooted path. I prefer working with the full path and make all my references like the inlcude statement above. This allows me to enter a general variable $sitebasepath that handles account specific information that may change from machine to machine and then simply type the path from the webroot, ie. /globals/whatever_file.php

    I also use the $self variable in forms that may call themselves to handle data input.

    Hope that helps.

提交回复
热议问题