Page 1 of 1
Document Root and HTML links
Posted: Wed Aug 20, 2008 10:45 am
by SyntheticShield
Ive seen it done before, but Im not sure how to use it and could not find any examples searching with the keywords I was using. What I would like to know is how to use the document root code
To create a link in the website. For instance if I have:
Code: Select all
<a href="inc/contact_us.php">Contact Us</a>
How would I use the document root code to create a link so that no matter what page I put that link on its always pointing to the same resource. Sorry for such a simple question, I did try searching here and with google but was not coming up with much. I know you would have to escape the quotes and I think you would have to use the php echo function but Im not 100% and thought I would get some input on it.
Re: Document Root and HTML links
Posted: Wed Aug 20, 2008 11:22 am
by lukewilkins
$_SERVER['DOCUMENT_ROOT'] is going to give you the location on the server ... which is not what you want in a link.
$_SERVER['HTTP_HOST'] will give you more what you are looking for.
Refer to this page:
http://us.php.net/manual/en/reserved.va ... server.php
It tells you everything you can get from the server regarding this issue.
And test for yourself:
Code: Select all
<?php
echo $_SERVER['DOCUMENT_ROOT'];
echo '<br/>';
echo $_SERVER['HTTP_HOST'];
?>
Hope that helps!
Re: Document Root and HTML links
Posted: Wed Aug 20, 2008 12:24 pm
by SyntheticShield
lukewilkins wrote:$_SERVER['DOCUMENT_ROOT'] is going to give you the location on the server ... which is not what you want in a link.
$_SERVER['HTTP_HOST'] will give you more what you are looking for.
Refer to this page:
http://us.php.net/manual/en/reserved.va ... server.php
It tells you everything you can get from the server regarding this issue.
And test for yourself:
Code: Select all
<?php
echo $_SERVER['DOCUMENT_ROOT'];
echo '<br/>';
echo $_SERVER['HTTP_HOST'];
?>
Hope that helps!
Okay, I tried those out and I can see now that the HTTP_HOST is a better option as it echoes
http://www.mydomain.com. Im assuming that its safe to use Document Root in includes, just not in the links, correct? So I would use HTTP_HOST when creating links? If that is so, then how do I format everything. I took a couple practice runs at it but I keep getting parse errors. I know I need to escape the quotes, but Im not sure what to do with the '<>' and '=' signs.
Anyway, not meaning to be a pest, just trying to learn. I pulled up that link as well and I tried a few of those in a test page to see what they output and Im going to have to play with some of those so I can get comfortable with their usage and purposes.
Re: Document Root and HTML links
Posted: Wed Aug 20, 2008 12:35 pm
by lukewilkins
Hey SyntheticShield. You're not being a pest, we're all learning here and passing on knowledge.
Yes, 'DOCUMENT_ROOT' is a good option for includes. But for a browser link it would be useless because it shows public directory PLUS all the sub-directories as well and a browser wouldn't know what to do.
As for 'HTTP_HOST', post what you do have and I can point out why you are getting parse errors. For now, here is an example of using 'HTTP_HOST':
Code: Select all
echo '<a href="http://'.$_SERVER['HTTP_HOST'].'/link/to/my/website.php">My link</a>';
If you still have issues with it, let's see your code.
Re: Document Root and HTML links
Posted: Wed Aug 20, 2008 12:43 pm
by ghurtado
The thing is, if you are linking within your own site, you don't even need the help of PHP. Start all your links with a slash, like this:
Code: Select all
<a href="/contact_us.php">Contact</a>
And they will work regardless of the location where you place the link.
Re: Document Root and HTML links
Posted: Wed Aug 20, 2008 1:15 pm
by SyntheticShield
lukewilkins wrote:Hey SyntheticShield. You're not being a pest, we're all learning here and passing on knowledge.
Yes, 'DOCUMENT_ROOT' is a good option for includes. But for a browser link it would be useless because it shows public directory PLUS all the sub-directories as well and a browser wouldn't know what to do.
As for 'HTTP_HOST', post what you do have and I can point out why you are getting parse errors. For now, here is an example of using 'HTTP_HOST':
Code: Select all
echo '<a href="http://'.$_SERVER['HTTP_HOST'].'/link/to/my/website.php">My link</a>';
If you still have issues with it, let's see your code.
After seeing your version, I was making it WAY too complicated. For which I apologize. I was trying to figure it out on my own and I went off he deep end with the escape slashes. So thank you for that example, I very much appreciate it.
ghurtado wrote:The thing is, if you are linking within your own site, you don't even need the help of PHP. Start all your links with a slash, like this:
Code: Select all
<a href="/contact_us.php">Contact</a>
And they will work regardless of the location where you place the link.
I had not tried this and when it comes to simplistic solutions, I wish I had known about this a long time ago. I tried your version as well and it too worked like a champ. So now I have two ways to code links. Im having a great day I think with all that Ive learned here in the last few minutes.
Thank you both for taking the time to help out. Im trying to move away from pure HTML/XHTML and CSS and do things a bit more dynamic and I knew that was going to require I learn some php. I just wish I had started a long time ago. I was going to try and get my head around Ruby, but I think I'll start with php and work on that a while first.
Re: Document Root and HTML links
Posted: Wed Aug 20, 2008 3:17 pm
by SyntheticShield
I have spent some time looking over these two methods of creating the links. Would I be correct in stating that the HTTP_HOST method would be better for SEO?
Re: Document Root and HTML links
Posted: Wed Aug 20, 2008 3:25 pm
by ghurtado
While much of SEO is black magic and lore, myths and legends, I doubt any decent search engine pays much attention to whether you use the full server name in your internal links.