Web Development Thread, PHP: Display current URL in Coding and Web Development; Hi guys, looking for some help with PHP. I'd like to display the current URL that you're looking at.
29th June 2009, 02:35 PM #1
- Rep Power
PHP: Display current URL
Hi guys, looking for some help with PHP. I'd like to display the current URL that you're looking at.
At the moment I have:
But this echoes the /index.php at the end. Any ideas how I could get rid of the /index.php, and just display the path that we're looking at.
<?php echo parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);?>
Thanks in advance
IDG Tech News
29th June 2009, 02:52 PM #2
i don't know of any built in way to do it, but you could explode/strip (i forget what php calls it) the variable (parse_url) then so its cuts off anything after and including the "/" and then echo it.
30th June 2009, 10:15 AM #3
PHP: $_SERVER - Manual
I'm sure one of those could help, I'm not sure what kind of URL you're trying to get though.
It might be that you just need to drop the parse_url() function and just use $_SERVER['REQUEST_URI'] on its own.
Last edited by DrPerceptron; 30th June 2009 at 10:19 AM.
30th June 2009, 10:32 AM #4
How much do you care about urls that end in some other file, like mypage.php? (better question: what exactly are you trying to achieve? there's probably an easier way)
30th June 2009, 10:52 AM #5
- Rep Power
Unfortunately just the '$_SERVER['REQUEST_URI']' on its own still displays /index.php on the end, I just needed to hide the actual filename on the end (to make it nice and tidy!)
If anyone's interested, I managed to bodge it together:
Now we get something like /directory1/otherthing/potato/ rather than /directory1/otherthing/potato/index.php
$this_dir = $_SERVER['REQUEST_URI'];
if (strpos($this_dir, basename($_SERVER['REQUEST_URI'])) !== false) $this_dir = reset(explode(basename($_SERVER['REQUEST_URI']), $this_dir));
Thanks for nudging me in the right direction
30th June 2009, 11:29 PM #6
- Rep Power
Glad you got it.
If you wanted to see what was available from the $_SERVER superglobal then you could have printed out the array by doing this
Thanks to leeneilson from:
tomdawson (21st August 2009)
Last Post: 24th February 2009, 10:36 AM
By dsk in forum Wireless Networks
Last Post: 20th February 2009, 02:32 PM
By Hightower in forum Web Development
Last Post: 11th February 2009, 10:53 AM
By kerlj001 in forum MIS Systems
Last Post: 15th December 2008, 04:41 PM
By sLiDeR in forum How do you do....it?
Last Post: 5th October 2008, 02:14 PM
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread