...

View Full Version : Getting the base url, how?



kaisellgren
06-15-2006, 03:54 PM
Hi,

I'm having a script http://www.domain.com/install/install.php

I want to that install.php to show the website: http://www.domain.com <- without any end slashes or anything crap! The domain.com is a address that change often so I cannot just type my address, I need the php file to check it itself.

I can not solve this myself wiht $_SERVER["SERVER_ADDR"], is there any else?

Thanks!

Bill Posters
06-15-2006, 04:07 PM
$_SERVER['HTTP_HOST']


http://www.mydomain.com/page/to/my/index.php?par=1
$_SERVER['SERVER_PROTOCOL'].'://'. $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF'].'?'. $_SERVER['QUERY_STRING']

http://www.mydomain.com/page/to/my/index.php?par=1
$_SERVER['SERVER_PROTOCOL'].'://'. $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI']

kaisellgren
06-15-2006, 04:13 PM
Hmm,

My script located at http://www.domain.com/my script base dir/install/install.php

And with that script I can only get wheather
http://www.domain.com
or
http://www.domain.com/my script base dir/install/install.php

but I can not get http://www.domain.com/my script base dir

Bill Posters
06-15-2006, 04:24 PM
You only said the domain name changes, so I presume the rest (the sub-directory structure) doesn't.

The purpose of my post was to demonsrate which server variables you need to form the base of your url. As the rest will be static, you can hard-code them into the string.

e.g.

'http://'.$_SERVER['HTTP_HOST'].'/install/install.php'

kaisellgren
06-15-2006, 04:30 PM
Hmm, is there any way to change the
$address = "http://www.domain.com/dgdf/dfg/install/install.php";
to go to 2 directories back like
http://www.domain.com/dsgdfg/fdggf ?

it's like <a href="../../directory"></a> <--- 2 dir back

Help?

Bill Posters
06-15-2006, 04:54 PM
$localroot = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$localroot = str_ireplace('/install/install.php','',$localroot);
echo $localroot;

What this basically does is construct a string of the entire url:
www.domain.com/route/to/install/install.php

Locate the string /install/install.php within that $localroot url string and replace it with nothing, effectively removing it.

It then echos our the remaining part of the $localroot url string

e.g.
www.domain.com/route/to


(Fwiw, the code could be condensed into a single line*, but I figure you'd appreciate the clarity of having it more open.)

*

echo str_ireplace('/install/install.php','',$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

kaisellgren
06-15-2006, 05:07 PM
Yeah. Now it works! Thanks dude!

One strange thing,

It seems to not understand what is str_ireplace, but it worked well with str_replace

Did you type ireplace correct or why it doesn't work for me? The str_replace worked but not the ireplace one...

Bill Posters
06-15-2006, 05:31 PM
It seems to not understand what is str_ireplace, but it worked well with str_replace

Did you type ireplace correct or why it doesn't work for me? The str_replace worked but not the ireplace one...
Your hosting is probably running PHP4, rather than PHP5.
str_ireplace (a case insensitive version of str_replace) came in with PHP5, but str_replace will do you fine so long as you stick to using the same casing in your directory and file name and the str_replace function.

i.e.
str_replace('/install/install.php', will work with www.domain.com/route/to/install/install.php, but it won't work with www.domain.com/route/to/Install/Install.php

kaisellgren
06-15-2006, 05:35 PM
Ah okay. That's good. I'm only using lower case letters in all files and folder names.

Thanks for giving this good info!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum