Unable to extract token from url in php

344

I am unable to extract query value from the url dynamically. My url is of the formwww.domain.com/script.php?token=4343434 . I want to extract the token value from this url. The token value is generated dynamically. I tried this but not working.

Thanks in advance.

$token = $_GET['token'];

When I print the token it gets printed.

print_r($_GET['token']); //works
475

Answer

Solution:

You can use a ternary operator,

URL :www.domain.com/script.php?token=4343434

$token = isset($_GET['token']) && !empty($_GET['token']) ? $_GET['token'] : "";
707

Answer

Solution:

Request URL : www.domain.com/script.php?token=4343434

By url parameter using Superglobals $_GET with ternary operator and check whether value is not empty.

$token = isset($_GET["token"]) && !empty($_GET["token"]) ? $_GET["token"] : " ";

If you are using PHP 7+ then ternary operator become more simple like below-

$token = $_GET["token"] ?? " ";

161

Answer

Solution:

You can use$_REQUEST global variable for it. It doesn't matter that request method is post or get. It will work on both.

Like:

$tokenVal= $_REQUEST['token'];

To checkecho $tokenVal;

or see all the value usingprint_r($_REQUEST);

People are also looking for solutions to the problem: php - Protect your option value

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.