How to correctly get get variables

I am still somewhat new to php and wondered how best to extract the $_GET variable from the url.

for example, how would I capture it from something like this:

 http://www.blahblahblah.com/reset_password.php?token=3072420e7e32cbcf304da791537d757342cf5957 

I just want to get everything from " token=etc ..."

early

+4
source share
4 answers

A simple way:

 $token = isset($_GET['token']) ? $_GET['token'] : null; 

If token set, it is assigned $token , otherwise $token is null.

+15
source

$_GET - an array:

 $token = $_GET['token']; 

So, if you print, you should see the marker part of the query string:

 echo "'Token: $token'"; // should display 'Token: 3072420e7e32cbcf304da791537d757342cf5957'; 

Note

If you are trying to use $ token to search the mysql database, you need to hide the slashes first to prevent security issues:

 $token = mysql_real_escape_string($_GET['token']); 

In addition, you first need to connect to mysql before calling mysql_real_escape_string() .

NOTE V.2

In your query string, your token will consist of ?token= until PHP encounters a query key / pair (usually & ; ). To wit:

 http://www.blahblahblah.com/reset_password.php?token=3072420e7e32cbcf304da791537d757342cf5957&token2=otherstuff 

&token2=otherstuff will be another key available for $_GET['token2'] , so this is not a problem with $_GET['token'] .

+4
source

So, you really have a URL string , and you want to extract values ​​from it:

 $url = "http://www.example.com/reset_pw.php?token=3072420e7e32cbc..."; $p = parse_url($url); // splits up the url parts parse_str($p["query"], $get); // breaks up individual ?var= &vars= print $get["token"]; 
+1
source

Solution 1:

 $token = isset($_GET['token']) ? $_GET['token'] : null; 

Here, if the variable is not set. She returns zero

Solution 2:

 $token = $_GET['token']; 
0
source

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


All Articles