You’re denying access to even normal characters

Asked

Viewed 57 times

0

This command line is not letting my form access the database even with all normal characters someone who understands more than preg_match can help me?

if(!preg_match("/^[a-zA-Z]*$",$name)||!preg_match("/^[a-zA-Z]*$",$title)||!preg_match("/^[a-zA-Z]*$",$text)){ #verificando se o usuario está usando algum caractere invaido
            header("location: ../contato.php?contato=invalidcharacters");
            exit(); 
  • What string is not passing? Do not allow white spaces?

  • was not allowing anything

1 answer

0


Its expression contains the wildcard character *. The * quantifier corresponds to the previous element ZERO or more times. Thus, even if the string does not contain any unauthorized character, the matcher will simply say: zero combines.

It’s right to use + matching of preceded characters (NOT ALLOWED) once or more.

Was also missing / limits at the end

Your corrected expression:

if(!preg_match("/^[a-zA-Z]+$/",$name)||!preg_match("/^[a-zA-Z]+$/",$title)||!preg_match("/^[a-zA-Z]+$/",$text)){

A suggestion

$string=$name.$title.$text;

if(!preg_match("/^[a-z]+?$/i", $string)){

 header("location: ../contato.php?contato=invalidcharacters");
 exit();

}

From the beginning ( ^ ) at the end ( $ ) of the string ONLY the amount can exist ( +? ) of letters from a to z ( [a-z] ) uppercase or minuscule ( i ).

( ! ) is the negative of the expression condition, that is, if the string does not consist only of uppercase or minuscule letters.

  • our complicated this method I started programming a month ago and I’m having a lot of trouble with these security methods thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.