scriptygoddess

20 Jan, 2003

Getting the current URL with php

Posted by: Jennifer In: Lessons learned

There was a problem with one of the script I'd posted (I think we actually found a different way altogether of doing it) but it was an issue with $PHP_SELF when used in a file that gets included – it ends up showing the name of the INCLUDED file (not the current url).

Just for my future reference, I just tried this instead of PHP_SELF and it seemed to work ok with includes.

"http://".$_SERVER['HTTP_HOST'].$_SERVER['PATH_INFO']

Alternatively – if there's a "query string" at the end of the current url that you want to grab as well (ie http://www.domain.com/test.php?variable=value) you can add this onto the end of the line above:

.$_SERVER['QUERY_STRING']

(side note the . is just used to concatenate the different parts)

7 Responses to "Getting the current URL with php"

1 | lynda

January 21st, 2003 at 7:46 am

Avatar

That's really odd. I have no idea why it would do that. I have php_self in several included files and they all work just fine.

Odd! :)

2 | Jennifer

January 21st, 2003 at 9:02 am

Avatar

Yeah, when I just tested it now – PHP_SELF worked just fine when in an include. Maybe it was an isolated incident due to a weird setting on that server (?)… don't know…

3 | lynda

January 21st, 2003 at 5:35 pm

Avatar

That's okay, I didn't know about the query_string variable and now I do, so it's all good. :)

4 | Chris

January 28th, 2003 at 3:00 pm

Avatar

I'm not sure exactly what the difference is, but I'm using "$_SERVER['REQUEST_URI']" in some of my scripts and it also seems to work well!

Chris

5 | Jester

February 1st, 2003 at 10:51 am

Avatar

Yeah $_SERVER['REQUEST_URI'] gets everything after the actual host, this bit:

http://www.host.com/script.php?foo=bar

So any easy way to get the full, current URL is:

echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

Useful for when you want to send emails out and you need to link to the absolute URL.

6 | echo

December 10th, 2004 at 1:27 am

Avatar

This link explains exactly what you mention with some interesting examples. Stuff I hadn't seen before.

7 | echo

December 10th, 2004 at 1:29 am

Avatar

ooops…my bad…here it is…
link

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements