PHP - Get Current URL

Discussion in 'Coding Help' started by conor, Jan 14, 2009.

Tags:
  1. conor

    conor New Member

    Hi,

    I am trying to find out the current URL using PHP. This is what I have thus far:

    $http = 'http';
    if($_SERVER["HTTPS"] == "on"){
    $http .= "s";
    }
    $http .= "://";
    $url = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    $_SESSION['siteurl'] = $http.$url;

    I can't figure out how to take the filename from that. So for example it would currently return:
    http://somesite.com/blah/file.php
    Where as I want it to return:
    http://somesite.com/blah/

    Does anyone know how I could achieve this?
     
  2. conor

    conor New Member

    I just figured it out!

    $http = 'http';
    if($_SERVER["HTTPS"] == "on"){
    $http .= "s";
    }
    $url = $http."://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    $filename = explode("/", $url);
    for( $i = 0; $i < (count($filename) - 1); ++$i ) {
    $_SESSION['siteurl'] .= $filename[$i].'/';
    }
     
  3. damoth

    damoth New Member

    $_SERVER['PHP_SELF'] should give you the filename...
     
  4. conor

    conor New Member

    yeah but I didn't want the filename. I wanted the URL minus the filename. The code above gives me that
     
  5. damoth

    damoth New Member

    Just to finish off this thread...

    I've since come across the function Conor was looking for so...

    --------------------------------------------------

    $_SERVER['PHP_SELF'] returns the current file including directory

    dirname($_SERVER['PHP_SELF']) will return the directory only and not include the filename
     

Share This Page