PHP - parse current URL
You can use parse_url();
$url = 'http://www.mydomain.com/abc/';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
which would give you
Array
(
[scheme] => http
[host] => www.mydomain.com
[path] => /abc/
)
/abc/
Update: to get current page url and then parse it:
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
print_r(parse_url(curPageURL()));
echo parse_url($url, PHP_URL_PATH);
source for curPageURL function
How do I parse a URL in PHP?
You can use parse_url to get down to the hostname.
e.g.:
$url = "http://localhost/path/to/?here=there";
$data = parse_url($url);
var_dump($data);
/*
Array
(
[scheme] => http
[host] => localhost
[path] => /path/to/
[query] => here=there
)
*/
PHP: Best way to parse URL query without variable name?
If you are sure that nothing malicious would be passed (or you are just testing), then you could do something like:
$output = eval('return ' . parse_url($_SERVER['REQUEST_URI'])['query'] . ';');
echo $output;
Obligatory notice:
Caution
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.
However, I would advise you use the following method and parse the expression with parser in the linked question:
$exp = parse_url($_SERVER['REQUEST_URI'])['query']; // 1+1
Reading Material
How to evaluate formula passed as string in PHP?
parse_url
eval
How can I get parameters from a URL string?
You can use the parse_url()
and parse_str()
for that.
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['email'];
If you want to get the $url
dynamically with PHP, take a look at this question:
Get the full URL in PHP
Related Topics
How to Bind Multiple Parameters to MySQLi Query
How to Upload and Parse a CSV File in PHP
Setting PHP Tmp Dir - PHP Upload Not Working
Why Does Crypt/Blowfish Generate the Same Hash with Two Different Salts
Send PHP HTML Mail with Attachments
Can You Re-Populate File Inputs After Failed Form Submission with PHP or JavaScript
Php's JSON_Encode Does Not Escape All JSON Control Characters
Ajax Call with Contenttype: 'Application/JSON' Not Working
Password_Hash Returns Different Value Every Time
Trying to Get Laravel 5 Email to Work
Any Way to Specify Optional Parameter Values in PHP