doubledee Posted May 30, 2012 Share Posted May 30, 2012 Up until now, I usually start off each script like this... <?php // Initialize a session. session_start(); // Access Constants. require_once('config/config.inc.php'); // Access Functions. require_once(WEB_ROOT . 'utilities/functions.php'); // Update Last Activity. require_once(WEB_ROOT . 'account/last_activity.php'); // Connect to the database. require_once(WEB_ROOT . 'private/mysqli_connect.php'); // Initialize Variables. $sessMemberID = (isset($_SESSION['sessMemberID']) ? $_SESSION['sessMemberID'] : ''); ?> I just added a new link in my Page Header which show how many Private Messages are coming in, and it calls the function... $newMessageCount = getNewMessageCount($dbc, $sessMemberID); Since I added that, I am getting random errors on certain pages, and so it has me wondering if I should try to put the Includes I first mentioned above in my "header.inc.php" file so I know I always have what I need to run every script. What do you think? Then again, maybe that would compound my problems, since the Header Script is included in most scripts, but they are located in all different levels of directories, which makes the paths change. Thoughts? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263362-should-i-relocate-my-includes/ Share on other sites More sharing options...
kicken Posted May 30, 2012 Share Posted May 30, 2012 I put all my startup stuff into a common.inc.php file and just include that on individual pages. My system composes mostly of classes so I use autoloading to include things on demand but you could just list out your includes. Including a file even if you don't need it is generally not a big deal. I will include checks for some constants too to help control some features such as doing: define('NO_DATABASE', true); define('NO_SESSION', true); require 'common.inc.php'; Which would cause my startup code to skip connecting to the database and starting a session. Quote Link to comment https://forums.phpfreaks.com/topic/263362-should-i-relocate-my-includes/#findComment-1349720 Share on other sites More sharing options...
doubledee Posted May 30, 2012 Author Share Posted May 30, 2012 I put all my startup stuff into a common.inc.php file and just include that on individual pages. My system composes mostly of classes so I use autoloading to include things on demand but you could just list out your includes. Including a file even if you don't need it is generally not a big deal. So, if I put this in my "header.inc.php" file... <?php // Access Functions. require_once(WEB_ROOT . 'utilities/functions.php'); // Connect to the database. require_once(WEB_ROOT . 'private/mysqli_connect.php'); // Determine Script Name. $scriptName = $_SERVER['SCRIPT_NAME']; ?> And then I had one of those Includes in another Including file (e.g. "index.php") it wouldn't be the end of the world?? I will include checks for some constants too to help control some features such as doing: define('NO_DATABASE', true); define('NO_SESSION', true); require 'common.inc.php'; Which would cause my startup code to skip connecting to the database and starting a session. You lost me. Why would you want to skip connecting to the database and starting a session? ----- Also, since my Includes typically need this... // Access Constants. require_once('../config/config.inc.php'); ...wouldn't I have to put that in each file? It seems to me, that if I included that reference to my Constants in my "header.inc.php" I would still have problems since the files that Include "header.inc.php" are already expecting to have the Constants defined by the time they would Include the Header file, if you follow me?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263362-should-i-relocate-my-includes/#findComment-1349805 Share on other sites More sharing options...
KevinM1 Posted May 30, 2012 Share Posted May 30, 2012 Why can't you include the constants first in header.inc.php? // Access Constants. require_once('config/config.inc.php'); // Access Functions. require_once(WEB_ROOT . 'utilities/functions.php'); // Connect to the database. require_once(WEB_ROOT . 'private/mysqli_connect.php'); // Determine Script Name. $scriptName = $_SERVER['SCRIPT_NAME']; ? Quote Link to comment https://forums.phpfreaks.com/topic/263362-should-i-relocate-my-includes/#findComment-1349811 Share on other sites More sharing options...
doubledee Posted May 30, 2012 Author Share Posted May 30, 2012 Why can't you include the constants first in header.inc.php? // Access Constants. require_once('config/config.inc.php'); // Access Functions. require_once(WEB_ROOT . 'utilities/functions.php'); // Connect to the database. require_once(WEB_ROOT . 'private/mysqli_connect.php'); // Determine Script Name. $scriptName = $_SERVER['SCRIPT_NAME']; ? When you Include a file, PHP treats it like it is part of the Parent file, right? If that is correct - and I believe it is - then that creates a problem with my reference to my Config file, because when we Include "header.inc.php" into "index.php" then the above code works, but if "header.inc.php" is Included into "view_pm.php" which is located in the "account" directory, then the above code would fail, because it would need to read... // Access Constants. require_once('../config/config.inc.php'); Right? --- The way I have things now, not only am I being redundant and including all of my Includes at the tops of each script like this... <?php // Initialize Session. session_start(); // Access Constants. require_once('../config/config.inc.php'); // Access Functions. require_once('../utilities/functions.php'); // Update Last Activity. require_once('../account/last_activity.php'); // Connect to the database. require_once(WEB_ROOT . 'private/mysqli_connect.php'); ...but I am also hard-coding the reference to my "config.inc.php" file like this... // Access Constants. require_once('config/config.inc.php'); // Access Constants. require_once('../config/config.inc.php'); // Access Constants. require_once('../../config/config.inc.php'); and so on... I like your suggestion, and if there is some way to make it so my "header.inc.php" file is *aware* of its location, and can change the path to back to my "config.inc.php" file, then your idea would be an improvement. Sincerely, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263362-should-i-relocate-my-includes/#findComment-1349819 Share on other sites More sharing options...
KevinM1 Posted May 30, 2012 Share Posted May 30, 2012 Have you tried setting your include paths? If you don't want to do that, then why can't you use your already predefined WEB_ROOT constant to help with your path issue? Quote Link to comment https://forums.phpfreaks.com/topic/263362-should-i-relocate-my-includes/#findComment-1349860 Share on other sites More sharing options...
doubledee Posted May 30, 2012 Author Share Posted May 30, 2012 Have you tried setting your include paths? If you don't want to do that, then why can't you use your already predefined WEB_ROOT constant to help with your path issue? Can you please respond to my long response to your comments above first? (I thought we were making some progress there.) Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/263362-should-i-relocate-my-includes/#findComment-1349862 Share on other sites More sharing options...
Adam Posted May 30, 2012 Share Posted May 30, 2012 Why not use an absolute file path to include it? Or you could use $_SERVER['DOCUMENT_ROOT'] to create a relative path? Quote Link to comment https://forums.phpfreaks.com/topic/263362-should-i-relocate-my-includes/#findComment-1349865 Share on other sites More sharing options...
KevinM1 Posted May 30, 2012 Share Posted May 30, 2012 When you Include a file, PHP treats it like it is part of the Parent file, right? If that is correct - and I believe it is - then that creates a problem with my reference to my Config file, because when we Include "header.inc.php" into "index.php" then the above code works, but if "header.inc.php" is Included into "view_pm.php" which is located in the "account" directory, then the above code would fail, because it would need to read... // Access Constants. require_once('../config/config.inc.php'); Right? Correct. Easy enough to test, too, if you make a couple of nested folders. I made the following test just to double-check: test.php /inc inc.php /nested nested.php test.php: echo "Hello from test.php"; inc.php: require_once('../test.php'); nested.php: require_once('../inc.php'); nested.php could not grab a hold of test.php because it is a level deeper than inc.php. The way I have things now, not only am I being redundant and including all of my Includes at the tops of each script like this... <?php // Initialize Session. session_start(); // Access Constants. require_once('../config/config.inc.php'); // Access Functions. require_once('../utilities/functions.php'); // Update Last Activity. require_once('../account/last_activity.php'); // Connect to the database. require_once(WEB_ROOT . 'private/mysqli_connect.php'); ...but I am also hard-coding the reference to my "config.inc.php" file like this... // Access Constants. require_once('config/config.inc.php'); // Access Constants. require_once('../config/config.inc.php'); // Access Constants. require_once('../../config/config.inc.php'); and so on... I like your suggestion, and if there is some way to make it so my "header.inc.php" file is *aware* of its location, and can change the path to back to my "config.inc.php" file, then your idea would be an improvement. Well, that's what include paths are for. They're set in your php.ini, and tell PHP to look in those paths for the files you try to include/require. So, instead of having to keep track of what your current script's relation to the files you want to include manually, you simply tell it "Look in these locations whenever I try to include something." If it finds it, it will automatically be included. Look at the link I provided in my previous response for more info. Quote Link to comment https://forums.phpfreaks.com/topic/263362-should-i-relocate-my-includes/#findComment-1349907 Share on other sites More sharing options...
RobertP Posted June 5, 2012 Share Posted June 5, 2012 with my application, i have nothing valuable in my initial index.php file ( security reasons of course ); from there i call my engine file ( located out of my web root ) which initiates everything. something like this: /index.php (check php version, make sure /sources & /commons exit, include engine.php) /sources (outside of my web root, also contains engine.php) /commons (hold my js/css/image files, anything that needs to be accessed public) i try to code things once. if i plan to use it more then once, then i modify the existing code ( create a function or class if needed ) so if any error arise later on, i have 1 code block to blame, not 10+. Quote Link to comment https://forums.phpfreaks.com/topic/263362-should-i-relocate-my-includes/#findComment-1351301 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.