Benchamoneh Posted January 26, 2008 Share Posted January 26, 2008 Hello everyone, this is my first post! I have recently decided to learn PHP. Overall things are going well for me and I have recently discovered the include() function which is amazing for headers and footers! However it is with this function that I have a question: I have created my header.html and footer.html and put them in an includes directory in the root of my site. I then use include() to wrap the 2 pages around my unique page content. For /index.php it looks like this: <?php include ('includes/header.html'); ?> <p>UNIQUE PAGE CONTENT HERE</p> <?php include ('includes/footer.html'); ?> This is great, but i find that links that are in the header.html file do not update to remain relative to where the particular php file is stored (eg, the above code makes links in the header work for /index.php but they would break for /information/about.php) To get around this I have created a '$page_path' variable that is unique to every page and shifts the directory focus back to the site root, from there I just add in the relevant path information. To link /information/about.php to itself the code would look like this: In the /includes/header.html file; <head> <link href="<?php echo $page_path; ?>includes/css/main.css" rel="stylesheet" type="text/css" /> </head> <body> <a href="<?php echo $page_path; ?>information/about.php"link</a> </body> and then in the /information/about.php file; <?php $page_path = '../'; include ($page_path . 'includes/header.html'); ?> This moves the path from the includes folder back to root and then to the information dir and selecting the about.php file. This all works correctly and I am pleased with it but my question is is this the best way?? I'm pretty sure there is a more efficient solution but I just can't work it out. Sadly my understanding of PHP is not brilliant yet so I have a hard time explaining what I have done and looking for a more efficient process. In case my explanation is so poor that you cannot understand me, I have attached a zip of my web directory to help explain. Any help/information/assurance/constructive criticism is welcome. Sorry about the length of this post! [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/87958-using-include-and-linking-to-files/ Share on other sites More sharing options...
PHP Monkeh Posted January 26, 2008 Share Posted January 26, 2008 I always find it a bit difficult as well to be quite honest, and it can be a tad tricky (especially if you're viewing files in different directories etc.) One way you could do it is create a $domain variable or something, which would contain http://localhost or http://www.mydomain.com and then you could do your includes from there, like: <?php include($domain."/includes/header.html"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/87958-using-include-and-linking-to-files/#findComment-450030 Share on other sites More sharing options...
amites Posted January 26, 2008 Share Posted January 26, 2008 setting up a configuration.php file or some other such name and having a universal site root works rather well ex: <?php $home = 'http://www.mysite.com/'; ?> and then using <?php include 'configuration.php'; $link = $home . 'myfile.html' ?> to link to http://www.mysite.com/myfile.html you can play with it from there, welcome to the world of PHP, opens up what you can do in building sites to a new level of business and other functions as opposed to making things that just look pretty Quote Link to comment https://forums.phpfreaks.com/topic/87958-using-include-and-linking-to-files/#findComment-450031 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.