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 :)
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.
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.
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)
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 :)
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