tags:

views:

204

answers:

3

I am using XAMPP on windows vista.

In my development, I have http://127.0.0.1/test_website/

Now I would like get this http://127.0.0.1/test_website/ with php.

I tried something like these, but none of them worked.

echo dirname(__FILE__)
or
echo basename(__FILE__);
etc.

I will appreciate any help.

Thanks in advance.

+4  A: 

Try this:

<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>

Learn more about the $_SERVER Predefined Variable

If you plan on using https, you can use this:

function url(){
  $protocol = $_SERVER['HTTPS'] ? "https" : "http";
  return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

echo url();
#=> http://127.0.0.1/foo
macek
Should check `$_SERVER['HTTPS']` and swap in `https://` instead of `http://` in those cases.
ceejayoz
@ceejayoz, updated answer to include this.
macek
Be careful, in protocol you must add "://"
Brice Favre
@Brice Favre, thanks for catching that.
macek
Thanks to you, i needed this function.
Brice Favre
+1  A: 

Take a look at $_SERVER variable. It has all the info you need.

Marko
A: 

I think the $_SERVER superglobal has the information you're looking for. It might be something like this:

echo $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']

You can see the relevant PHP documentation here.

Jeremy DeGroot