I have try different ways checking if usernames are valid in my site, but its still not detected by preg_match.
After I clicked sign up button its showed "invalidusername" on browser: [1]: https://i.stack.imgur.com/fznXA.png
signup.inc.php :
<?php
if(isset($_POST["submit"])){
$firstname =$_POST["firstname"];
$lastname =$_POST["lastname"];
$username =$_POST["username"];
$email =$_POST["email"];
$gender =$_POST["gender"];
$password =$_POST["password"];
$repeatPassword =$_POST["repeatpassword"];
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
if (emptyInputSignup($firstname, $lastname, $username, $email, $gender, $password, $repeatPassword ) !==false){
header("location:../signup.php?error=emptyinput");
exit();
}
if (invalidUsername($username) !==false){
header("location:../signup.php?error=invalidusername");
exit();
}
if (invalidUsername($email) !==false){
header("location:../signup.php?error=invalidemail");
exit();
}
if (passwordMatch($password, $repeatPassword) !==false){
header("location:../signup.php?error=passworddontmatch");
exit();
}
if (usernameExists($con, $username) !==false){
header("location:../signup.php?error=passworddontmatch");
exit();
}
createUser($con,$name,$email,$username,$pwd);
}
else {
header("location:../signup.php");
exit();
}
function.inc.php :
function invalidUsername($username){
$result;
if (!preg_match("/[^A-Za-z0-9_\-]/", $username)) {
$result= true;
}
else{
$result= false;
}
return $result;
}
CodePudding user response:
The error you're getting "NOT FOUND, The requested URL was not found on this server" isn't exactly a PHP error. This is an apache error. You are redirecting the form to a path that you never created a route for. This is the default "404" page, so to speak.
Regarding preg_match, it might be your pattern. I'm personally not too savvy with filter patterns, but that's okay because they are all readily available online.
https://www.w3schools.com/PHP/php_form_url_email.asp
Also, I'm not sure if this error is actually in your code or if you mistyped it in your question here, but notice you are using invalidUsername to check the email in your third if statement on the signup.inc.php page.
Use the following filter to check email: "/^[A-Za-z0-9._%-] @[A-Za-z0-9.-] \.[A-Za-z]{2,4}$/"
Rewrite the code to this:
if (!preg_match("/^[a-zA-Z0-9] $/", $username)) {
$result=true;
}
else{
$result=false;
}
CodePudding user response:
First get some redundant code out of the way, if
you use !
the result is already a boolean and if
branches on true. Therefore:
function invalidUsername(string $username): bool {
return (bool) !preg_match("/[^A-Za-z0-9_\-]/", $username);
}
already suffices.
To gain more understanding of a regular expression, to test or to debug it, you can make use of https://regex101.com , it has support for PHP regular expressions and also explains the pattern (only a compatible textual representation see regex101 for a more readable variant):
/[^A-Za-z0-9_\-]/
Match a single character not present in the list below
[^A-Za-z0-9_\-]
* A-Z matches a single character in the range between A (index 65)
and Z (index 90) (case sensitive)
* a-z matches a single character in the range between a (index 97)
and z (index 122) (case sensitive)
* 0-9 matches a single character in the range between 0 (index 48)
and 9 (index 57) (case sensitive)
* _ matches the character _ with index 95 (x5F or 0x137)
literally (case sensitive)
* \- matches the character - with index 45 (x2D or 0x55)
literally (case sensitive)
That normally helps to get things starting well.