Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve complete URL from address bar using PHP? [duplicate]

Tags:

php

Possible Duplicate:
How to get full URL on the address bar using PHP

I use this function, but it does not work all the time. Can anyone give a hint?

function sofa_get_uri() {
    $host = $_SERVER['SERVER_NAME'];
    $self = $_SERVER["REQUEST_URI"];
    $query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
    $ref = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
    return $ref;
}

I want to retrieve the link in address bar (exactly) to use it to refer user back when he sign out. The urls are different:

http://domain.com/sample/address/?arg=bla

http://domain.com/?show=bla&act=bla&view=bla

http://domain.com/nice/permalinks/setup

But I can't get a function that works on all cases and give me the true referrer.

Hint please.

like image 507
Ahmed Fouad Avatar asked Jun 28 '12 13:06

Ahmed Fouad


People also ask

How do I get a full URL?

The necessary superglobal variables such as $_SERVER['HTTPS'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PORT'] are used to get full URL in PHP. The variable HTTPS can easily retrieve the protocol in the URL of a webpage.

How can I get the last part of a URL in PHP?

Get Last URL Segment If you want to get last URI segment, use array_pop() function in PHP.


1 Answers

How about this?

function getAddress() {
    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
    return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}

echo getAddress();
like image 198
Juha Untinen Avatar answered Sep 23 '22 20:09

Juha Untinen