How to check for white spaces and special characters
up vote
0
down vote
favorite
I am trying to code a form for a login using php to check it. But I can't get it to check the if I have any whitespaces and special characters for the username. I tried using the [W]+ but that did not work.
<?php
$usernerr = "";
$passwerr = "";
$usern = "";
$passw = "";
$pattern = '/['/~`!@#$%^&*()_-+=[]|;:"<>,.?\]/';
if($_SERVER['REQUEST_METHOD'] == 'POST')
if(empty($_POST['uname']))
$usernerr = "*Please add a username please!";
else
!preg_match($pattern, $usern))
$usernerr = "*Username have only letters and numbers!";
$usernerr = "";
if(empty($_POST['psw']))
$passwerr = "*Please add a password please!";
else
$passw = clearInput($_POST['psw']);
$passwerr = "";
function clearInput($input)
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
?>
php authentication
add a comment |
up vote
0
down vote
favorite
I am trying to code a form for a login using php to check it. But I can't get it to check the if I have any whitespaces and special characters for the username. I tried using the [W]+ but that did not work.
<?php
$usernerr = "";
$passwerr = "";
$usern = "";
$passw = "";
$pattern = '/['/~`!@#$%^&*()_-+=[]|;:"<>,.?\]/';
if($_SERVER['REQUEST_METHOD'] == 'POST')
if(empty($_POST['uname']))
$usernerr = "*Please add a username please!";
else
!preg_match($pattern, $usern))
$usernerr = "*Username have only letters and numbers!";
$usernerr = "";
if(empty($_POST['psw']))
$passwerr = "*Please add a password please!";
else
$passw = clearInput($_POST['psw']);
$passwerr = "";
function clearInput($input)
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
?>
php authentication
1
Instead of checking for specific forbidden characters, check if the string contains anything other than the allowed characters.
– Magnus Eriksson
Nov 10 at 16:21
I'd use php.net/manual/en/function.ctype-alnum.php IfUsername have only letters and numbersare the rules. I also would remove thatclearInputfunction, is that to prevent SQL injections or XSS?
– user3783243
Nov 10 at 16:22
3
Also, everytime I see functions like yourcheckInput(), I cringe. That usually means that you're doing something wrong. If it is there for protecting yourself against SQL injections, it's not good enough. Also, escaping and changing the password is a bad idea. Let's keep the password as is, since you're only suppose to store the password hash anyway.
– Magnus Eriksson
Nov 10 at 16:23
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to code a form for a login using php to check it. But I can't get it to check the if I have any whitespaces and special characters for the username. I tried using the [W]+ but that did not work.
<?php
$usernerr = "";
$passwerr = "";
$usern = "";
$passw = "";
$pattern = '/['/~`!@#$%^&*()_-+=[]|;:"<>,.?\]/';
if($_SERVER['REQUEST_METHOD'] == 'POST')
if(empty($_POST['uname']))
$usernerr = "*Please add a username please!";
else
!preg_match($pattern, $usern))
$usernerr = "*Username have only letters and numbers!";
$usernerr = "";
if(empty($_POST['psw']))
$passwerr = "*Please add a password please!";
else
$passw = clearInput($_POST['psw']);
$passwerr = "";
function clearInput($input)
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
?>
php authentication
I am trying to code a form for a login using php to check it. But I can't get it to check the if I have any whitespaces and special characters for the username. I tried using the [W]+ but that did not work.
<?php
$usernerr = "";
$passwerr = "";
$usern = "";
$passw = "";
$pattern = '/['/~`!@#$%^&*()_-+=[]|;:"<>,.?\]/';
if($_SERVER['REQUEST_METHOD'] == 'POST')
if(empty($_POST['uname']))
$usernerr = "*Please add a username please!";
else
!preg_match($pattern, $usern))
$usernerr = "*Username have only letters and numbers!";
$usernerr = "";
if(empty($_POST['psw']))
$passwerr = "*Please add a password please!";
else
$passw = clearInput($_POST['psw']);
$passwerr = "";
function clearInput($input)
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
?>
php authentication
php authentication
asked Nov 10 at 16:13
Darker Minecraft
154
154
1
Instead of checking for specific forbidden characters, check if the string contains anything other than the allowed characters.
– Magnus Eriksson
Nov 10 at 16:21
I'd use php.net/manual/en/function.ctype-alnum.php IfUsername have only letters and numbersare the rules. I also would remove thatclearInputfunction, is that to prevent SQL injections or XSS?
– user3783243
Nov 10 at 16:22
3
Also, everytime I see functions like yourcheckInput(), I cringe. That usually means that you're doing something wrong. If it is there for protecting yourself against SQL injections, it's not good enough. Also, escaping and changing the password is a bad idea. Let's keep the password as is, since you're only suppose to store the password hash anyway.
– Magnus Eriksson
Nov 10 at 16:23
add a comment |
1
Instead of checking for specific forbidden characters, check if the string contains anything other than the allowed characters.
– Magnus Eriksson
Nov 10 at 16:21
I'd use php.net/manual/en/function.ctype-alnum.php IfUsername have only letters and numbersare the rules. I also would remove thatclearInputfunction, is that to prevent SQL injections or XSS?
– user3783243
Nov 10 at 16:22
3
Also, everytime I see functions like yourcheckInput(), I cringe. That usually means that you're doing something wrong. If it is there for protecting yourself against SQL injections, it's not good enough. Also, escaping and changing the password is a bad idea. Let's keep the password as is, since you're only suppose to store the password hash anyway.
– Magnus Eriksson
Nov 10 at 16:23
1
1
Instead of checking for specific forbidden characters, check if the string contains anything other than the allowed characters.
– Magnus Eriksson
Nov 10 at 16:21
Instead of checking for specific forbidden characters, check if the string contains anything other than the allowed characters.
– Magnus Eriksson
Nov 10 at 16:21
I'd use php.net/manual/en/function.ctype-alnum.php If
Username have only letters and numbers are the rules. I also would remove that clearInput function, is that to prevent SQL injections or XSS?– user3783243
Nov 10 at 16:22
I'd use php.net/manual/en/function.ctype-alnum.php If
Username have only letters and numbers are the rules. I also would remove that clearInput function, is that to prevent SQL injections or XSS?– user3783243
Nov 10 at 16:22
3
3
Also, everytime I see functions like your
checkInput(), I cringe. That usually means that you're doing something wrong. If it is there for protecting yourself against SQL injections, it's not good enough. Also, escaping and changing the password is a bad idea. Let's keep the password as is, since you're only suppose to store the password hash anyway.– Magnus Eriksson
Nov 10 at 16:23
Also, everytime I see functions like your
checkInput(), I cringe. That usually means that you're doing something wrong. If it is there for protecting yourself against SQL injections, it's not good enough. Also, escaping and changing the password is a bad idea. Let's keep the password as is, since you're only suppose to store the password hash anyway.– Magnus Eriksson
Nov 10 at 16:23
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Considering the error message you wrote, you are complicating yourself.
Instead of searching for the list of characters that aren't alpha num, search for alpha num only. Try using this pattern, and don't negate the condition.
$pattern = "/^[a-zA-Z0-9]+$/";
// Some code...
if(preg_match($pattern, $usern))
// ^-------------------------------Notice the changes
//Username is valid
Description of the pattern :
^ from the begining
[a-zA-Z0-9] search an alpha num
+ 1 or more time
$ to the end
/^[a-zA-Z0-9]+$/can be replaced by /^[[:alnum:]]+$/ or /^[a-zd]+$/i which produce the same effect.
1
There also is posix character class,[[:alnum:]]. I find[a-zd]withimodifier, or what your wrote easier though
– user3783243
Nov 10 at 16:28
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Considering the error message you wrote, you are complicating yourself.
Instead of searching for the list of characters that aren't alpha num, search for alpha num only. Try using this pattern, and don't negate the condition.
$pattern = "/^[a-zA-Z0-9]+$/";
// Some code...
if(preg_match($pattern, $usern))
// ^-------------------------------Notice the changes
//Username is valid
Description of the pattern :
^ from the begining
[a-zA-Z0-9] search an alpha num
+ 1 or more time
$ to the end
/^[a-zA-Z0-9]+$/can be replaced by /^[[:alnum:]]+$/ or /^[a-zd]+$/i which produce the same effect.
1
There also is posix character class,[[:alnum:]]. I find[a-zd]withimodifier, or what your wrote easier though
– user3783243
Nov 10 at 16:28
add a comment |
up vote
2
down vote
accepted
Considering the error message you wrote, you are complicating yourself.
Instead of searching for the list of characters that aren't alpha num, search for alpha num only. Try using this pattern, and don't negate the condition.
$pattern = "/^[a-zA-Z0-9]+$/";
// Some code...
if(preg_match($pattern, $usern))
// ^-------------------------------Notice the changes
//Username is valid
Description of the pattern :
^ from the begining
[a-zA-Z0-9] search an alpha num
+ 1 or more time
$ to the end
/^[a-zA-Z0-9]+$/can be replaced by /^[[:alnum:]]+$/ or /^[a-zd]+$/i which produce the same effect.
1
There also is posix character class,[[:alnum:]]. I find[a-zd]withimodifier, or what your wrote easier though
– user3783243
Nov 10 at 16:28
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Considering the error message you wrote, you are complicating yourself.
Instead of searching for the list of characters that aren't alpha num, search for alpha num only. Try using this pattern, and don't negate the condition.
$pattern = "/^[a-zA-Z0-9]+$/";
// Some code...
if(preg_match($pattern, $usern))
// ^-------------------------------Notice the changes
//Username is valid
Description of the pattern :
^ from the begining
[a-zA-Z0-9] search an alpha num
+ 1 or more time
$ to the end
/^[a-zA-Z0-9]+$/can be replaced by /^[[:alnum:]]+$/ or /^[a-zd]+$/i which produce the same effect.
Considering the error message you wrote, you are complicating yourself.
Instead of searching for the list of characters that aren't alpha num, search for alpha num only. Try using this pattern, and don't negate the condition.
$pattern = "/^[a-zA-Z0-9]+$/";
// Some code...
if(preg_match($pattern, $usern))
// ^-------------------------------Notice the changes
//Username is valid
Description of the pattern :
^ from the begining
[a-zA-Z0-9] search an alpha num
+ 1 or more time
$ to the end
/^[a-zA-Z0-9]+$/can be replaced by /^[[:alnum:]]+$/ or /^[a-zd]+$/i which produce the same effect.
edited Nov 10 at 16:30
answered Nov 10 at 16:21
Cid
3,00421025
3,00421025
1
There also is posix character class,[[:alnum:]]. I find[a-zd]withimodifier, or what your wrote easier though
– user3783243
Nov 10 at 16:28
add a comment |
1
There also is posix character class,[[:alnum:]]. I find[a-zd]withimodifier, or what your wrote easier though
– user3783243
Nov 10 at 16:28
1
1
There also is posix character class,
[[:alnum:]]. I find [a-zd] with i modifier, or what your wrote easier though– user3783243
Nov 10 at 16:28
There also is posix character class,
[[:alnum:]]. I find [a-zd] with i modifier, or what your wrote easier though– user3783243
Nov 10 at 16:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240850%2fhow-to-check-for-white-spaces-and-special-characters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Instead of checking for specific forbidden characters, check if the string contains anything other than the allowed characters.
– Magnus Eriksson
Nov 10 at 16:21
I'd use php.net/manual/en/function.ctype-alnum.php If
Username have only letters and numbersare the rules. I also would remove thatclearInputfunction, is that to prevent SQL injections or XSS?– user3783243
Nov 10 at 16:22
3
Also, everytime I see functions like your
checkInput(), I cringe. That usually means that you're doing something wrong. If it is there for protecting yourself against SQL injections, it's not good enough. Also, escaping and changing the password is a bad idea. Let's keep the password as is, since you're only suppose to store the password hash anyway.– Magnus Eriksson
Nov 10 at 16:23