tags:

views:

19

answers:

1

i have a problem with this registration form, im checking the password and username, this is the process im doing it in?

php:

$username = mysql_real_escape_string($_POST['username']);
 $password = md5(mysql_real_escape_string($_POST['pass']));


    //check if username is valid
        if (ereg("^[a-zA-Z0-9_\-]+$", $username)){

             die(msg(0,"oOps, This username is not valid, only numbers, letters and underscores allowed"));

        }

        // check if password is more than 6

    if(strlen($password) < 6){

       die(msg(0,"oOps, The password has to be more than 6 characters, be tricky!"));
    }

but i type the user, password correctly, but its still giving me the errors shown, is thier something wrong with the checking :)) thanks

+2  A: 

It doesn't make sense to check the validity of $username after passing it through mysql_real_escape_string() since that function will escape certain characters, and the resultant 'username' will end up containing '\' characters, and therefore always fail your regexp.

(Also, you appear to be checking for a username, and die()ing if it's valid. Are you sure that code is exactly how you're written it?)

Likewise, it doesn't make any sense to check the length of $password after passing it through md5(). md5() will convert anything into a 32-character string, so that condition will always fail.

P.S.

// check if password is more than 6

if(strlen($password) < 6){

If you're going to over-comment code, at least make sure the comment is accurate! ;-)

Bobby Jack
so basically i should check before taking them through mysql escape and md5!!
getaway
yup - exactly right
Bobby Jack
for the username checking i just want to allow _ 0-9 a-z only, how would i change it
getaway
P.S. Note that 'if (ereg("^[a-zA-Z0-9_\-]+$", $username)) { die(); }' effectively means "if the username only contains basic characters, die because it's invalid" - I *don't* think that's what you intended
Bobby Jack
just reverse the logic: if (ereg("^[a-zA-Z0-9_-]+$", $username) === FALSE) { die(); }
Bobby Jack
can you change for me please, im so stupid at this stuff @bobby, thanks
getaway
N.B. I've removed the escaping backslash from that character class on purpose - from memory, you don't need to escape hyphen (-) if it's the last character in the square brackets, since it's unambiguous
Bobby Jack
thanks @bobby the username checking is not working, i just typed ..... in the username box and its letting me?
getaway
Hmmm... is it possible for you to supply the entire code? (if it's too long, you can use something like pastebin)
Bobby Jack
its wokring sorry its my mistake, cheers your a star
getaway
no worries, getaway
Bobby Jack