Getting vars from URL

URL:

url.com/index.php?id=1000

How to get 1000from idand add it in <h1></h1>on the page?

+1
source share
6 answers

Use $_GET:

$id = isset($_GET['id']) ? (int) $_GET['id'] : FALSE;
echo '<h1>', $id, '</h1>';

If the URL is inside a variable, use parse_url Docs and parse_str Docs :

$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $vars);
$id = isset($vars['id']) ? (int) $vars['id'] : FALSE;
echo '<h1>', $id, '</h1>';

Edit:

If you turned on register globals (which is very discouraged, so just for completeness), you can do this:

$id = isset($id) ? (int) $id : FALSE;
echo '<h1>', $id, '</h1>';

Usually in an application you want to de-pair out $_GETand wrap it in a request object:

class Request
{
    public function getParameter($name, $default = NULL)
    {
        return isset($_GET[$name]) ? $_GET[$name] : $default;
    }
    public function getParameterInt($name, $default = NULL)
    {            
        $value = $this->getParameter($name, NULL);
        return NULL === $value ? $default : (int) $value;
    }
}

$request = new Request();
$id = $request->getParameterInt('id');
echo '<h1>', $id, '</h1>';

, , , http. .

+1

$_ GET htmlspecialchars XSS:

echo '<h1>', htmlspecialchars($_GET['id']), '</h1>';
+5

$_ GET superglobal, .

: <h1><?php echo $_GET['id']; ?></h1>

+2

$_REQUEST[], $_GET:

<h1><?php echo $_GET['id']; ?></h1>

XSS, htmlspecialchars:

<h1><?php echo htmlspecialchars($_GET['id']); ?></h1>
+2
<h1><?php echo $_REQUEST["id"]; ?></h1>
+2
$id = $_GET["id"];
//Perform checks on $id
echo '<h1>'.$id.'<h1/>';

If you want to insert it in h1, you can repeat it back and use javascript to set the innerhtml tag.

+2
source

Source: https://habr.com/ru/post/1778716/


All Articles