grasshopper31 Posted January 4, 2014 Share Posted January 4, 2014 I have a file outside of the site root folder containing the password to connect to mysql that is being saved in a function, which is being included in my custom cms_class.php. Now my cms_class.php file is going to be included into a few other files including the main index.php file. Now we know when using relatives path is a big no no when including a file that has already included a file. So whats the successful way to do this. my directory looks something like this:/x/server/www <--- siteroot/x/server/www/cms/cms_class.php/x/server/www/index.php <-- main index a.k.a Home file/x/server/pass.php pass.php ---->includes into -->cms_class.php---->includes into --->index.php ---->includes into ---> (random files in different folders) im also including the pass.php file into phpmyadmin and other config files so i can change passwords from one file that is secured. (at least i think it is since its outsite of the siteroot folder)/x/server/apps/phpmyadmin/config.incthe reason for me doing this is just to protect the password to connect to my databases. If someone knows another way, better or not please let me know since im always interested in different methods when it comes to passwords protections, but i still want to know what will be the right way to include these files since i'm going to be doing something similar later with different files.Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/285095-relativeabsolute-paths-including-files/ Share on other sites More sharing options...
kicken Posted January 4, 2014 Share Posted January 4, 2014 You can get a file's directory by using the __DIR__ magic constant. With this you can include a file with a path relative to that file regardless of if it is itself included else where. eg: require_once __DIR__.'/../blah.php'; If that line were located in a file saved at /var/www/htdocs/example.php then it would always include /var/www/blah.php regardless of where example.php was initially included from (or if it were run directly) Quote Link to comment https://forums.phpfreaks.com/topic/285095-relativeabsolute-paths-including-files/#findComment-1463885 Share on other sites More sharing options...
grasshopper31 Posted January 5, 2014 Author Share Posted January 5, 2014 Thank you! I actually tried dirname(__FILE__); but didn't work but it was just a missing "/" and some typos. It worked once i used the code in both pages where a file was being included. Quote Link to comment https://forums.phpfreaks.com/topic/285095-relativeabsolute-paths-including-files/#findComment-1463891 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.