hotcoder
hotcoder

Reputation: 3256

Current page url in PHP

I've this url: http://localhost/alignment/?page_id=52&q=2 I want to get the portion: ?page_id=52 , how can I do that?

Upvotes: 0

Views: 497

Answers (7)

Thomas Clayson
Thomas Clayson

Reputation: 29925

$_SERVER['QUERY_STRING']

or

$url = "http://localhost/alignment/?page_id=52&q=2";
$bits = explode("?", $url);
$querystring = $bits[1]; // this is what you want

but the first one will be much more reliable and is easier. :)

EDIT

if you meant that you just wanted that one variable use:

$_GET['page_id']

Upvotes: 7

gnome
gnome

Reputation: 1133

Assign the url to string and then explode() it, http://php.net/manual/en/function.explode.php, using the ? as a delimiter

Upvotes: 0

Ken Richards
Ken Richards

Reputation: 3013

This should do it:

echo $_SERVER['QUERY_STRING'];

Upvotes: 0

bcosca
bcosca

Reputation: 17555

echo parse_url($url,PHP_URL_QUERY);

Upvotes: 0

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

If you have it as a string, use parse_url. Documentation here. Get the query value of it. or if its current request use $_SERVER['QUERY_STRING']

Upvotes: 0

Evan Mulawski
Evan Mulawski

Reputation: 55334

This is called a query string. The main portion of the query string is separated by the rest of the URL with a ?. Each argument in the query string is separated by a &.

PHP:

$_SERVER['QUERY_STRING'];

If you want to get the individual pieces, use:

$_GET['page_id']; //etc...

Upvotes: 2

GWW
GWW

Reputation: 44093

You can get the whole query string with $_SERVER['QUERY_STRING'], but you would have to parse out page_id part. If you insist on doing things manually the function parse_str may come in handy.

A better choice would be to just use the predefined $_GET global variable. $_GET['page_id'] would give you value 52.

Upvotes: 1

Related Questions