Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better $_GET security

Tags:

php

My PHP is very rusty. I have a md5 hash that's being passed via get to a script and then I'm grabbing it like this:

$id = $_GET['id'];

Obviously there's a security risk here...I was thinking of checking the string length to make sure it's 32 characters long but that doesn't seem very robust to me. What else could I do to make it more secure?

thanks

like image 658
Mike Rifgin Avatar asked Dec 01 '22 03:12

Mike Rifgin


1 Answers

You could validate with a regex to make sure it consists of only alphanumeric characters.

E.g. something like this (my PHP is rusty too):

if(preg_match("/^[A-Fa-f0-9]{32}$/", $id) > 0) {
    // All good
}
like image 51
mdm Avatar answered Dec 05 '22 12:12

mdm