+ Post New Thread
Results 1 to 6 of 6
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. At ...
  1. #1

    Join Date
    Jan 2008
    Location
    UK
    Posts
    76
    Thank Post
    5
    Thanked 5 Times in 5 Posts
    Rep Power
    13

    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:

    PHP Code:
    <?php echo parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);?>
    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.

    Thanks in advance
    Tom.

  2. #2
    p858snake's Avatar
    Join Date
    Dec 2008
    Location
    Queensland
    Posts
    1,488
    Thank Post
    37
    Thanked 175 Times in 151 Posts
    Blog Entries
    2
    Rep Power
    50
    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.

  3. #3
    DrPerceptron's Avatar
    Join Date
    Dec 2008
    Location
    In a house
    Posts
    850
    Thank Post
    32
    Thanked 121 Times in 103 Posts
    Rep Power
    37
    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 09:19 AM.

  4. #4

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,855
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    181
    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)

  5. #5

    Join Date
    Jan 2008
    Location
    UK
    Posts
    76
    Thank Post
    5
    Thanked 5 Times in 5 Posts
    Rep Power
    13
    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:


    PHP Code:
    <?php
    $this_dir 
    $_SERVER['REQUEST_URI'];
    if (
    strpos($this_dirbasename($_SERVER['REQUEST_URI'])) !== false$this_dir reset(explode(basename($_SERVER['REQUEST_URI']), $this_dir));
    echo 
    $this_dir;
    ?>
    Now we get something like /directory1/otherthing/potato/ rather than /directory1/otherthing/potato/index.php

    Thanks for nudging me in the right direction

  6. #6

    Join Date
    May 2009
    Posts
    11
    Thank Post
    0
    Thanked 5 Times in 5 Posts
    Rep Power
    10
    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

    PHP Code:
    print_r($_SERVER); 

  7. Thanks to leeneilson from:

    tomdawson (21st August 2009)

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 13
    Last Post: 24th February 2009, 09:36 AM
  2. Extending our current ip range
    By dsk in forum Wireless Networks
    Replies: 17
    Last Post: 20th February 2009, 01:32 PM
  3. Trim URL with PHP
    By Hightower in forum Web Development
    Replies: 19
    Last Post: 11th February 2009, 09:53 AM
  4. Replies: 8
    Last Post: 15th December 2008, 03:41 PM
  5. Current and emerging technologies.....
    By sLiDeR in forum How do you do....it?
    Replies: 7
    Last Post: 5th October 2008, 01:14 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •