shah Posted March 30, 2009 Share Posted March 30, 2009 Hi, I have a little problem with php require("header.php"). i have two php file 1) config.php and 2) header.php in includes folder. I have another Stylesheet file stylesheet.css in a folder called CSS. in the header.php file, i have used require'config.php' and linked to the style sheet through href="../CSS/stylesheet.css". Now when i have a file index.php in the parent directory and used require("./includes/header.php"), then the CSS file format of stylesheet.css and the variable of config.php are not displayed although i have included the header file in the index.php. When i investigated the issue, i have found out that when i change the include path of the header file as if it was also in the parent directory, then it works. that is when i change require'config.php' to require("./includes/config.php") and href="../CSS/stylesheet.css" to href="./CSS/stylesheet.css" then it works. as you can see, logically, the path of require should be accroding to the header.php file which is in the includes directory , but actually it is accepting the path according to the index.php file which is in the parent directory. this is actually a pain for me, because i am using the header and footer files for reusablity purpose, but it is asking me to change the path according to the caller file. I will appreciate if some one could help me. i have searched a lot on this but could not find anything. i can re explain with example if you can not understand what i mentioned above BR Syed Arif Ullah Shah Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 30, 2009 Share Posted March 30, 2009 Code? Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 I think the simplest solution would be to always use an absolute path when including files i.e. include "/CSS/stylesheet.css"; instead of include "../CSS/stylesheet.css"; that way you dont have to worry about the folder location of the page that is referencing this include file if that doesn't work then as Ken said, post your code and include a simple diagram of what your current folder structure looks like Quote Link to comment Share on other sites More sharing options...
shah Posted March 31, 2009 Author Share Posted March 31, 2009 Okay, here is a snapshot of my code......... before posting the code, let me give you the folder structure again.... Parent Directory (index.php) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Includes CSS (config.php,header.php,footer.php) (stylesheet.css) The stylesheet.css simple contains the CSS layout which could be used in other files. The code of config.php is given below (according to the folder strcuture given above) <?php $dbhost = "localhost"; $dbuser = "root"; $dbpassword = ""; $dbdatabase = "blogtastic"; $config_blogname = "MC's Home"; $config_author = "Syed Arif Ullah Shah"; $config_basedir = "http://127.0.0.1/sites/Myblog/"; ?> the code of the header.php is given below (according to the folder structure) <?php require_once'config.php'; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title><?php echo $config_blogname; ?></title> <link rel="stylesheet" href="../CSS/stylesheet.css" type="text/css" /> </head> <body> <div id="header"> <h1><?php echo $config_blogname; ?></h1> [<a href="index.php">home</a>] </div> <div id="main"> Since config.php is in the same folder, therefore i use require 'config.php'. Since stylesheet.css is in the CSS folder which is in the parent folder, i have to go one step above and then go to the CSS folder so i used "../CSS/stylesheet.css". If you know any other method to do so, kindly mention. When i run the header.php, it works absolutely fine. It displays the variables of config.php and uses the theme of stylesheet.css. Now comes the problem. in the parent directory, i have the index.php. its code is given below <?php require_once("./includes/header.php"); ?> It uses the code of header.php, but theme style of stylesheet.css and variables of config.php which were used in header.php using the "require" is not used here anymore. for this purpose, i have to change the paths of files used in the header.php. change require'config.php' to require("./includes/config.php") and href="../CSS/stylesheet.css" to href="./CSS/stylesheet.css". as i have already mentioned, although the header.php is in the includes folder, but for it to be used in the index.php, we have to change the paths according to the index.php. I hope i have explained my problem. Kindly help me now. Kind Regards Syed Arif Ullah Shah Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 31, 2009 Share Posted March 31, 2009 TIP: Use bbcode 'CODE' to preserve formatting of code - makes it easier for those helping to read it. Quote Link to comment Share on other sites More sharing options...
shah Posted March 31, 2009 Author Share Posted March 31, 2009 Okay, here is a snapshot of my code......... before posting the code, let me give you the folder structure again.... Parent Directory (index.php) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Includes CSS (config.php,header.php,footer.php) (stylesheet.css) The stylesheet.css simple contains the CSS layout which could be used in other files. The code of config.php is given below (according to the folder strcuture given above) <?php $dbhost = "localhost"; $dbuser = "root"; $dbpassword = ""; $dbdatabase = "blogtastic"; $config_blogname = "MC's Home"; $config_author = "Syed Arif Ullah Shah"; $config_basedir = "http://127.0.0.1/sites/Myblog/"; ?> the code of the header.php is given below (according to the folder structure) <?php require_once'config.php'; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title><?php echo $config_blogname; ?></title> <link rel="stylesheet" href="../CSS/stylesheet.css" type="text/css" /> </head> <body> <div id="header"> <h1><?php echo $config_blogname; ?></h1> [<a href="index.php">home</a>] </div> <div id="main"> Since config.php is in the same folder, therefore i use require 'config.php'. Since stylesheet.css is in the CSS folder which is in the parent folder, i have to go one step above and then go to the CSS folder so i used "../CSS/stylesheet.css". If you know any other method to do so, kindly mention. When i run the header.php, it works absolutely fine. It displays the variables of config.php and uses the theme of stylesheet.css. Now comes the problem. in the parent directory, i have the index.php. its code is given below <?php require_once("./includes/header.php"); ?> It uses the code of header.php, but theme style of stylesheet.css and variables of config.php which were used in header.php using the "require" is not used here anymore. for this purpose, i have to change the paths of files used in the header.php. change require'config.php' to require("./includes/config.php") and href="../CSS/stylesheet.css" to href="./CSS/stylesheet.css". as i have already mentioned, although the header.php is in the includes folder, but for it to be used in the index.php, we have to change the paths according to the index.php. I hope i have explained my problem. Kindly help me now. Kind Regards Syed Arif Ullah Shah Quote Link to comment Share on other sites More sharing options...
shah Posted April 7, 2009 Author Share Posted April 7, 2009 Guys, i am still waiting for a solid solution. please reply asap as my deadline is drawing near. every thing is complete. i am only having this issue Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 7, 2009 Share Posted April 7, 2009 Guys, i am still waiting for a solid solution. please reply asap as my deadline is drawing near. every thing is complete. i am only having this issue sorry, didn't know we were on a deadline with you .. you can only expect so much when it's free. i've never seen such over-complication of such a simple task. at least the run-on paragraphs make it seem over-complicated. personally, i use an absolute path back to the document root on the server, that way, no matter where the script is being processed it can read any included file no problem. to find your document root open your phpinfo file on the server/browser and under Environment you'll see it. give that a go. Quote Link to comment Share on other sites More sharing options...
shah Posted April 7, 2009 Author Share Posted April 7, 2009 yeah i know you are not obliged to offer me a solution. Actually the person for whom i am doing it, wants relative paths so that it is usable anywhere, without changing the paths. It will also increase my knowledge to know how to use relative path in re-usable code. We are not using absolute path here. Once again i apologize if any of my statement offended you. It was not intentional. Quote Link to comment Share on other sites More sharing options...
schilly Posted April 7, 2009 Share Posted April 7, 2009 you can't use "../CSS/stylesheet.css" as header.php will load into index.php and then when the browser sees the link tag and calls the stylesheet, the path will be wrong as the browser is in the parent directory and not includes. I tested all your code and just changed "../CSS/stylesheet.css" to "./CSS/stylesheet.css" and works fine. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 7, 2009 Share Posted April 7, 2009 yeah i know you are not obliged to offer me a solution. Actually the person for whom i am doing it, wants relative paths so that it is usable anywhere, without changing the paths. It will also increase my knowledge to know how to use relative path in re-usable code. We are not using absolute path here. Once again i apologize if any of my statement offended you. It was not intentional. i take nothing personally on the internet. no worries. the only way to guarantee re-usability is to point to your root directory, so like stated above, "./directory/file.file" will work no matter where on the site you are. "../directory/file.file" will not as that tells the browser to find the file in question one directory below the current directory. so, if you were browsing www.your-site.com/directory/1/2/3/4/index.php, calling for "../directory/file.file" will tell the browser to look in folder '3'. hope that helps if you didn't already know that. Quote Link to comment Share on other sites More sharing options...
shah Posted April 8, 2009 Author Share Posted April 8, 2009 so what i can get from this whole discussion is that the only way i can do it to use absolute path. correct me if i am wrong. BR Syed Quote Link to comment Share on other sites More sharing options...
shah Posted April 8, 2009 Author Share Posted April 8, 2009 So will "./css/stylesheet.css" work if i am calling it from any subdirectories which is not in the parent directory?? you can't use "../CSS/stylesheet.css" as header.php will load into index.php and then when the browser sees the link tag and calls the stylesheet, the path will be wrong as the browser is in the parent directory and not includes. I tested all your code and just changed "../CSS/stylesheet.css" to "./CSS/stylesheet.css" and works fine. Quote Link to comment Share on other sites More sharing options...
schilly Posted April 8, 2009 Share Posted April 8, 2009 the problem here is that php function include and the html in header.php are treated differently. If you look at your header file <?php require_once'config.php'; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title><?php echo $config_blogname; ?></title> <link rel="stylesheet" href="../CSS/stylesheet.css" type="text/css" /> </head> <body> <div id="header"> <h1><?php echo $config_blogname; ?></h1> [<a href="index.php">home</a>] </div> <div id="main"> The php engine is only evaluating the php tags which is the include which operates from the directory in which the file calls if from (ie. includes directory b/c that's where header.php is). Nothing is done with the link tag to the css file. Now when you include the header file in the index that html in the header file is just passed into the index file and not evaluation. The browser will evaluate that html and see the link tag and then make a call for the css file. Browser is operating from the index dir so that is your relative dir, not includes. So to answer your question So will "./css/stylesheet.css" work if i am calling it from any subdirectories which is not in the parent directory?? That will work if you include header.php in any file that is in your index dir. I'm not sure if all of the above is true with how the php engine and browser operates but the underlying idea is. Hope this makes sense. 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.