corrupshun Posted January 8, 2010 Share Posted January 8, 2010 I finally started a login script, currently doing a register script first.. But everything works fine except preg_match isn't it supposed to return 1 if it finds a match? Well when i submit a username with a character that isn't allowed, it does the same thing as the correct username: invalid:#hello hello@hello.com 5d41402abc4b2a76b9719d911017c592 January 08, 2010 valid:hello hello@hello.com 5d41402abc4b2a76b9719d911017c592 January 08, 2010 preg_match isn't working, but everything else does. what did i do wrong? (Line 7 in functions.php) I suck at regex stuff :/ register.php <?php include('functions.php'); register(); ?> <html> <form name="register" action="register.php" method="POST"> Username<input type="text" name="username" maxlength="12" />max of 12 characters<br /> Password<input type="password" name="password" maxlength="20" />max of 20 characters<br /> Confirm Password<input type="password" name="passwordc" maxlength="20" /><br /> Email-Address<input type="text" name="email" />for password recovery<br /> <input type="submit" value="Register!" name="submit" /> </form> </html> functions.php <?php function register() { if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['passwordc']) && isset($_POST['email'])) { if(!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['passwordc']) && !empty($_POST['email'])) { if($_POST['password']==$_POST['passwordc']) { if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { if(preg_match('~^[a-z0-9._-]$~iD', $_POST['username']) == 0) { //if(strlen("$_POST['username']<12&&>3) $password = md5("$_POST[password]"); $date = date('F d, Y'); $username = $_POST['username']; $email = $_POST['email']; echo "$username $email $password $date"; } else { echo "username contains invalid characters or is too long"; } } else { echo "email not valid"; } } else { echo "passwords do not match"; } } else { echo "One or more fields are empty."; } } else { if(!isset($_POST['submit'])) { echo "Signup Here"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/187767-preg_match-not-doing-its-thing/ Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 if(preg_match('~^[a-z0-9._-]$~iD', $_POST['username']) == 0) { This preg_match will only ever return true if your password is a single character long that consists of a-z, A-Z, 0-9, fullstop, underscore or dash. I'm guessing every value you have tested it with is longer than 1 character as such the preg_match will always fail returning FALSE, so when you compare it to 0 it will always be true and hence enter that if block rather than the else block. Try... if(preg_match('~^[a-z0-9._-]+$~iD', $_POST['username']) == 0) { Quote Link to comment https://forums.phpfreaks.com/topic/187767-preg_match-not-doing-its-thing/#findComment-991376 Share on other sites More sharing options...
corrupshun Posted January 9, 2010 Author Share Posted January 9, 2010 thank you! i added the plus sign, thank you for explaining it as well. Now i'm a step closer to understanding it I also had to make it if(!preg_match('~^[a-z0-9._-]+$~iD', $_POST['username']) == 0) { *notice the exclamation point* Quote Link to comment https://forums.phpfreaks.com/topic/187767-preg_match-not-doing-its-thing/#findComment-991491 Share on other sites More sharing options...
cags Posted January 9, 2010 Share Posted January 9, 2010 Rather than inverting the result of the preg_match and comparing it to 0 why not simply have... if(preg_match('~^[a-z0-9._-]+$~iD', $_POST['username'])) { Quote Link to comment https://forums.phpfreaks.com/topic/187767-preg_match-not-doing-its-thing/#findComment-991627 Share on other sites More sharing options...
corrupshun Posted January 10, 2010 Author Share Posted January 10, 2010 Rather than inverting the result of the preg_match and comparing it to 0 why not simply have... if(preg_match('~^[a-z0-9._-]+$~iD', $_POST['username'])) { thank you Quote Link to comment https://forums.phpfreaks.com/topic/187767-preg_match-not-doing-its-thing/#findComment-992364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.