Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    26
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Basepath variable

    Hello everyone!

    I'm creating a miniscript for user management but i have some issues:
    When i want to include a file, i should everytime add the fullpath to be sure that the file is included correctly and i was thinkging if i can do this via any variable like :
    PHP Code:
    $bpath  '/home/userpanel/public_html/anydir/'
    and when i need to use it anywhere, to do something like:
    PHP Code:
    include '.$bpath. anyotherdir/athordir/anyfile.php' 
    ?????

    Is this possible in any way?

    I have done it with domain:
    PHP Code:
    $domain 'http://domain.tld/anydir/otherdire/'
    and when i use it i do something like:
    PHP Code:
      header('Location: '.$domain.'index.php?error=1'); 
    Hope that i was understandable and someone can help me.

    Thank you in advance!

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Yes, this is quite common practice. I would define path constants in a configuration file like
    PHP Code:
    define('BPATH','/home/userpanel/public_html/anydir/'); 
    and include the configuration file where necessary. It would allow you to reconfigure the system quickly if you e.g. move to a new hosting.

    In the same way you could define paths to the most important folders of the system which you could need in the code.

    I prefer defining paths as constants because constants are available in any scope (global scope, function scope, class scope etc.) after they are defined.

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    26
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SKDevelopment View Post
    Yes, this is quite common practice. I would define path constants in a configuration file like
    PHP Code:
    define('BPATH','/home/userpanel/public_html/anydir/'); 
    and include the configuration file where necessary. It would allow you to reconfigure the system quickly if you e.g. move to a new hosting.

    In the same way you could define paths to the most important folders of the system which you could need in the code.

    I prefer defining paths as constants because constants are available in any scope (global scope, function scope, class scope etc.) after they are defined.
    Thank you.
    I added it at the config file, but, how to use that constant now?
    thanks again

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    I usually define all path constants in a configuration file e.g. similar to this:
    PHP Code:
    define('BPATH','/home/userpanel/public_html/anydir/');  
    define('BPATH_INCLUDE',BPATH 'include' DIRECTORY_SEPARATOR);  
    define('BPATH_JS',BPATH 'js' DIRECTORY_SEPARATOR); 
    Now the only file which I need to include by relative path is this configuration file. Other files are included or accessed by absolute paths defined by the constants. E.g.
    PHP Code:
    require_once(BPATH_INCLUDE 'myclass.inc.php'); 

  • #5
    New Coder
    Join Date
    Oct 2008
    Posts
    26
    Thanks
    7
    Thanked 0 Times in 0 Posts
    PHP Code:
    Warning: require_once(BPATH_INCLUDEadmini/include/dbconfig.php) [function.require-once]: failed to open streamNo such file or directory in E:Program FilesVertrigoServwwwliveadminimoduletndeshjeshfaqndeshje.php on line 11

    Fatal error
    : require_once() [function.require]: Failed opening required 'BPATH_INCLUDEadmini/include/dbconfig.php' (include_path='.;E:\Program Files\VertrigoServ\Smarty'in E:Program FilesVertrigoServwwwliveadminimoduletndeshjeshfaqndeshje.php on line 11 
    Didn't worked for me

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    No, you would need to include the configuration file with constants by relative or absolute path. E.g. by relative path it could be like this:
    PHP Code:
    require_once('include/configure.php'
    Only after this you would be able to use the constants.

  • Users who have thanked SKDevelopment for this post:

    kleidi (02-21-2010)

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    kleidi, keep in mind that this is a CONSTANT, not a STRING. You use it just like you would a variable, meaning don't wrap it in quotes. Constants are also available globally, which is handy for dynamic paths. See the manual page for more information.

  • Users who have thanked Inigoesdr for this post:

    kleidi (02-21-2010)

  • #8
    New Coder
    Join Date
    Oct 2008
    Posts
    26
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Resolved - Basepath variable

    Got the idea ... thank you very much.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •