Website URL:
http://www.example.com/?id=2
Code:
$id = intval($_GET['id']);
Answer from Steven on Stack OverflowWebsite URL:
http://www.example.com/?id=2
Code:
$id = intval($_GET['id']);
There are two ways to get variable from URL in PHP:
When your URL is: http://www.example.com/index.php?id=7 you can get this id via $_GET['id'] or $_REQUEST['id'] command and store in $id variable.
Lest's take a look:
// url is www.example.com?id=7
//get id from url via $_GET['id'] command:
_GET['id']
same will be:
//get id from url via $_REQUEST['id'] command:
_REQUEST['id']
the difference is that variables can be passed to file via URL or via POST method.
if variable is passed through url, then you can get it with $_GET['variable_name'] or $_REQUEST['variable_name'] but if variable is posted, then you need to you $_POST['variable_name'] or $_REQUEST['variable_name']
So as you see $_REQUEST['variable_name'] can be used in both ways.
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
All the parameters after ? can be accessed using $_GET array. So,
echo $_GET['email'];
will extract the emails from urls.
$_GET is not a function or language construct—it's just a variable (an array). Try:
<?php
echo $_GET['link'];
In particular, it's a superglobal: a built-in variable that's populated by PHP and is available in all scopes (you can use it from inside a function without the global keyword).
Since the variable might not exist, you could (and should) ensure your code does not trigger notices with:
<?php
if (isset($_GET['link'])) {
echo $_GET['link'];
} else {
// Fallback behaviour goes here
}
Alternatively, if you want to skip manual index checks and maybe add further validations you can use the filter extension:
<?php
echo filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL);
Last but not least, you can use the null coalescing operator (available since PHP/7.0) to handle missing parameters:
echo $_GET['link'] ?? 'Fallback value';
<?php
echo $_GET['link'];
?>
or
<?php
echo $_REQUEST['link'];
?>
do work...
$_SERVER['QUERY_STRING'] contains the data that you are looking for.
DOCUMENTATION
- php.net: $_SERVER - Manual
The PHP way to do it is using the function parse_url, which parses a URL and return its components. Including the query string.
Example:
$url = 'www.mysite.com/category/subcategory?myqueryhash';
echo parse_url($url, PHP_URL_QUERY); # output "myqueryhash"
Full documentation here
You can use explode.
$url_exp = explode("/", $url);
$lastValu = end($url_exp);
You can use regular expression
$url='https://localhost/booktheparty/index.php?/hyderabad/corporate-party-planner/view-corporate-entertainment-activities/ENT105012/50/12';
preg_match("/\/(\d+)$/",$url,$matches);
$lastParams = $matches[1];
You can pass this variable in another page using query string for example
header('Location: http://www.ccm.net/forum/?var='.$lastParams);
and use $_GET['var'] on that page to get the value
you can fetch it like:
$cat = $_GET['category'];
and you don't need any foreach
$_GET is an associative array having all the query string keys values.
You can get value of any query string parameter from this array by passing the key of query string parameter.
To get category you can use $_GET["category"] in your case.
These are called query parameters.
<?php
$param1 = $_GET['q']; // something for http://localhost/script.php?q=something
http://php.net/manual/en/reserved.variables.get.php
catch the value in a variable using
$variable = $_GET['value']
and then add $variable to your database.