Jump to content

preg_match() not doing it's thing


corrupshun

Recommended Posts

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 [email protected] 5d41402abc4b2a76b9719d911017c592 January 08, 2010

valid:hello [email protected] 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";
}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/187767-preg_match-not-doing-its-thing/
Share on other sites

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) {

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.