Hello, I am creating a site that has a navigation bar at the top that never changes. I get the navigation bar into the html code by using <?php $file = file_get_contents ("navigation_code.php"); Echo $file;?>. I recently created some folders to organize my files, and that caused all the links to be broken. So to fix this, i just used the full url for every link in my whole site. Rather than href = "home.php" it now says href = "http://mysite.com/home.php." This fixes the problem.
My first question is, is there anything wrong with doing it this way (href = "home.php" changed to href = "http://mysite.com/home.php.")? I read something about how this affects the caching process, I didn't uderstand it.
My second question is, is this way (php - get_file_contents()) of creating sitewide content good practice? It works great, and editing my site's navigation/header/footer is quick; I change one file and the whole site is adjusted perfectly. Does this make the page load faster or slower? Is there anything else that I should know?
Thanks for all the help.
Is it okay to always link to the full URL?
Moderator: General Moderators
-
condoravenue1
- Forum Commoner
- Posts: 30
- Joined: Fri Dec 03, 2010 10:24 pm
Re: Is it okay to always link to the full URL?
1) The only thing potentially wrong with is what happens if your domain name changes? If that's not going to happen, there shouldn't be a problem. I can't imagine how it would negatively affect caching
2) Having common files for various parts of your site is a good idea - for the exact reason you state. However, I wouldn't do it the way you do it. With your method, you can't have PHP code execute in the file you're referencing. Just use a simple include() call instead.
Example:
title.php (file to be included)
file to display
resulting output
[syntax]//your method
Welcome <?PHP
echo $_SESSION['user']['title'];
?>
//include method
Welcome Mr. Anderson[/syntax]
2) Having common files for various parts of your site is a good idea - for the exact reason you state. However, I wouldn't do it the way you do it. With your method, you can't have PHP code execute in the file you're referencing. Just use a simple include() call instead.
Example:
title.php (file to be included)
Code: Select all
<?PHP
echo $_SESSION['user']['title'];
?>Code: Select all
//your method
Welcome <?php $file = file_get_contents('title.php'); echo $file; ?>
//using include()
Welcome <?php include 'title.php'; ?>[syntax]//your method
Welcome <?PHP
echo $_SESSION['user']['title'];
?>
//include method
Welcome Mr. Anderson[/syntax]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
condoravenue1
- Forum Commoner
- Posts: 30
- Joined: Fri Dec 03, 2010 10:24 pm
Re: Is it okay to always link to the full URL?
Will using this include() function cause the loading time to be slower than if i just use normal HTML with out the external file?
Re: Is it okay to always link to the full URL?
Technically yes. It's such a small amount though that you won't even be able to time the difference. This is a pretty standard practice - I wouldn't worry about any negative time effects.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.