Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Get URL with Parameter

Tags:

php

I want to get a URL and its parameters. Example:

www.someweb.com/somepage.php?id=10 

How to get only somepage.php?id=10?

I'm already tried REQUEST_URI and PHP_SELF but it only gives me www.someweb.com/somepage.php or somepage.php. What I want to is just the page name and its parameter(s).

like image 891
Cross Vander Avatar asked Nov 17 '12 02:11

Cross Vander


People also ask

How can I get parameters from a URL string?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.

What is $_ GET in PHP?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. Assume we have an HTML page that contains a hyperlink with parameters: <html> <body>

How do you find the variable in a URL?

To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.


2 Answers

basename($_SERVER['REQUEST_URI']); 

This will return all URLs with page name. (e.g.: index.php?id=1&name=rr&class=10).

like image 62
Cross Vander Avatar answered Sep 18 '22 14:09

Cross Vander


for complette URL with protocol, servername and parameters:

 $base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' .  $_SERVER['HTTP_HOST'];  $url = $base_url . $_SERVER["REQUEST_URI"]; 
like image 21
miralong Avatar answered Sep 20 '22 14:09

miralong