0

I want to take the url generated from a dynamic web in to a php variable Suppose there is one adress in url

www.example.com/new/id=2

I want to take the parts new/id=2 in to a php variable.

same time that variable is able to take new/id=2&title=ARTICLE from

www.example.com/new/id=2&title=ARTICLE

Is there any tricks which can do that job?.

Neenu
  • 47
  • 1
  • 14
  • Please be more specific when asking questions and research on google – Khan Shahrukh Sep 06 '14 at 17:16
  • As a general comment: Depending on the usage this can be dangerous, because attackers could inject arbitrary code (HTML/JavaScript). E.g. by appending ```?"><"``` or ```/"><"``` (directly after the .php ending). – MrTux Sep 06 '14 at 17:38
  • @KhanShahrukh pls do one thing Avoid the comment " Please be more specific when asking questions and research on google " Cause lot are telling the same comment And try somthing diffrent!. – Neenu Sep 07 '14 at 10:48
  • @Neenu Allrite I will make sure of that. – Khan Shahrukh Sep 07 '14 at 11:19

5 Answers5

2

Try this

$full_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
MH2K9
  • 11,951
  • 7
  • 32
  • 49
  • 1
    This does not work for https or different ports, also it does not contain the query string – MrTux Sep 06 '14 at 17:15
0

look at $_SERVER array

print_r($_SERVER);
Daniel Kucal
  • 8,684
  • 6
  • 39
  • 64
0

You can use the $_SERVER globals to get this info. $_SERVER["REQUEST_URI"] should give you new/id=2 and the full (GET) request URI.

ETA: My bad. MrTux is right... you need to concatenate $_SERVER["QUERY_STRING"] to get the URI and the query. So try this:

$uri_query = $_SERVER["REQUEST_URI"].$_SERVER["QUERY_STRING"];
Joseph8th
  • 636
  • 5
  • 10
  • For a more complete implementation that solves MrTux's valid comment on user3659034's answer, see [this cheatsheet](http://webcheatsheet.com/php/get_current_page_url.php). – Joseph8th Sep 06 '14 at 17:21
  • You still might need to add a "?" before ```.$_SERVER["QUERY_STRING"]```. – MrTux Sep 06 '14 at 17:31
0

Use the parse_url function of php.

$url = '//www.example.com/path?googleguy=googley';

$query=parse_url($url));

var_dump($query);

The above example will output

array(3) {
  ["host"]=>
  string(15) "www.example.com"
  ["path"]=>
  string(5) "/path"
  ["query"]=>
  string(17) "googleguy=googley"
}

That means you will get your required value at $query["query"]

zakaria
  • 426
  • 2
  • 15
0

When you use print_r($_SERVER);, you get a peak into all the server details, including the page referrer, in $_SERVER['HTTP_REFERER'].

apaderno
  • 28,547
  • 16
  • 75
  • 90