1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Get the url of page via PHP

Discussion in 'PHP' started by caroline, Sep 4, 2007.

  1. #1
    Hello,

    I want to save the url of a page in a variable so that I can use it later.
    There are many pages I want to do that to so writing it manually everytime like this: $variable="http://www.foo.com/foo1.html"; isn't an option.

    There has to be a php function that can get the url from the browser, right? I'd appreciate some help.
     
    caroline, Sep 4, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $uri = 
    	'http' . (!empty($_SERVER['HTTPS']) ? 's' : null) . '://' .
    	$_SERVER['HTTP_HOST'] .
    	($_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : null) .
    	$_SERVER['REQUEST_URI'];
    	
    echo $uri;
    
    
    PHP:
     
    nico_swd, Sep 4, 2007 IP
    caroline likes this.
  3. caroline

    caroline Peon

    Messages:
    376
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. Much appreciated.
     
    caroline, Sep 4, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Note that the code above takes care of https connections and other ports than the default. In most cases something like this is enough:

    
    $uri = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
    
    PHP:
     
    nico_swd, Sep 4, 2007 IP