moosey_man1988 Posted November 12, 2015 Share Posted November 12, 2015 Hi Everyone i know this is a bit basic, but I have been working on a project and to tidy things up i have been placing them in directories. my problem is I need to know how to plan this so i dont need to call an "include" or "require" in every page. as it gets difficult when you are going back a directory and then into another directory for example: in the html folder I have index.php sessions/login.php functions/functions.php index.php can all the functions via "include ('functions/functions.php'); but if we need the login.php to require functions it needs to be ('../functions/functions.php'); this can sometimes having something like a header.php in every page as it would call the script wrong directory. would some be able to help so that i either don't need to include a script on every page, or maybe i can define functions that are a constant? please let me now if this makes no sense! as ts a little difficult to explain. Thanks in advance for any help given <3 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 12, 2015 Share Posted November 12, 2015 Why not use constants for the path names and include a module that declares all these constants. Then your include simply reference the correct constant the the simple filename. You move things around - update the constants. Make them absolute too! Quote Link to comment Share on other sites More sharing options...
moosey_man1988 Posted November 12, 2015 Author Share Posted November 12, 2015 okay thats great! you have confirmed my little lightbulb i had in the pub this evening lol. could you give me an example for the scenario i displayed above? some things i hav a bit of a mental block on :| Quote Link to comment Share on other sites More sharing options...
Solution ginerjm Posted November 12, 2015 Solution Share Posted November 12, 2015 So you have a little module that defines paths /* paths.php $php_path = "/home/domain/php/"; $lib_path = "/home/domain/public_html/libs/"; ... ... Then in your code you include the above file and in your other includes you use the appropriate path var: include($php_path . "connect.php"); include($lib_path . "functions.php"); ... ... Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted November 13, 2015 Share Posted November 13, 2015 Also useful is $_SERVER['DOCUMENT_ROOT'] or dirname(__FILE__) . DIRECTORY_SEPARATOR Quote Link to comment Share on other sites More sharing options...
moosey_man1988 Posted November 13, 2015 Author Share Posted November 13, 2015 Hi QuickOldCar thanks for the response, I am using this function function url(){ return sprintf( "%s://%s/", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_ADDR'] //$_SERVER['SERVER_ADDR'] ); } But i have to call the functions php before i can use it lol Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted November 13, 2015 Share Posted November 13, 2015 (edited) For server includes is no need for the protocol, you can link to local files. Additionally can link to css,images and such within the html using absolute url without a protocol <img src="//mysite.com/image.png" /> Check out some server variables can use A while ago I babbled on a bit about the differences in a post, take a look. http://forums.phpfreaks.com/topic/292789-paths-when-rewriting-urls/?p=1498045 Edited November 13, 2015 by QuickOldCar Quote Link to comment 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.