Search
Left Quote    Black holes are where God divided by zero.
- Steven Wright    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: String check for invalid characters, Can this code be more e
  This user is offline  Xecu
  Subject: "String check for invalid characters..." Posted: @ 9:15 pm on Feb 27 2008   
Member #: 599
Rank: User - (6)
Since: 02/27/08
Posts: 6
From: New York City

Hi guys, I am very new to php, and mySQL, so please bare with me.

I have created a code from some scrap codes I found on various sites/ forums. This is the code, and it's meant to check passed strings aka Username and Password, if there are invalid characters being passed from a form after a Submit button is pressed.

Code:

$str = $_POST['uname'] ;
$str2 = $_POST['pword'] ;
$invalid_chars = ".,/\`;[]- *&^%$#@!~+( )|{}<>?: ='";

for($a = 0; $a < strlen($str); $a++) {
$char = substr($str, $a, 1);

if(strpos($inva lid_chars, $char) === false) {
error('Usernam e error: Username contains invalid characters');
}
}

for($a = 0; $a < strlen($str2); $a++) {
$char2 = substr($str2, $a, 1);

if(strpos($inva lid_chars, $char2) === false) {
error('Passwor d error: Password contains invalid characters');
}
}



Now the code should work, but it always passes error. Is there a better way to code an invalid char check, I've also tried with an array, but it had many flaws, so I decided to do it like this.

Edited at 09:35:43 pm on 02/27/08


You see heaven, before you see hell. - Xecu
    Viewed: 7,014 Times | Reply to This | To top
  This user is offline  Xecu
  Subject: "re: String check for invalid charac..." Posted: @ 10:15 pm on Feb 27 2008    
Member #: 599
Rank: User - (6)
Since: 02/27/08
Posts: 6
From: New York City

Sorry to double post, but I solved the non working part, and I think this is pretty efficient. Unless someone can show me a much better way.

Code:

$str = $_POST['uname '];
$str2 = $_POST['pword '];
$valid_chars = "12345678 90_abcdefghijkl mnopqrstuvwxyzA BCDEFGHIJKLMNOP QRSTUVWXYZ&quo t;;

for($a = 0; $a < strlen($str); $a++) {
$char = substr($str, $a, 1);

if(strpos($vali d_chars, $char) === false) {
error('Userna me error: Username contains invalid characters');< br /> }
}

for($a = 0; $a < strlen($str2); $a++) {
$char2 = substr($str2, $a, 1);

if(strpos($vali d_chars, $char2) === false) {
error('Passwo rd error: Password contains invalid characters');< br /> }
}



Hopefully this helps new people like myself, with a username and password invalid character check in their sign up forms.




You see heaven, before you see hell. - Xecu

    Viewed: 7,002 Times | Reply to This | To top
  This user is offline  misterhaan
  Subject: "re: String check for invalid charac..." Posted: @ 3:17 pm on Feb 28 2008    

Member #: 5
Rank: User - (213)
Since: 02/11/05
Posts: 148
From: chair

i




please note that the above post is likely made up in its entirety.

    Viewed: 6,984 Times | Reply to This | To top
  This user is offline  Xecu
  Subject: "re: String check for invalid charac..." Posted: @ 5:49 pm on Feb 28 2008    
Member #: 599
Rank: User - (6)
Since: 02/27/08
Posts: 6
From: New York City



Wow, see I are noob to this stuff, that looks much more efficient, and hopefully after I test it faster as well. Thanks again, I'll let you know how it goes.



Edited at 05:52:26 pm on 02/28/08


You see heaven, before you see hell. - Xecu

    Viewed: 6,980 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

You must be logged in to post on the forums. Login or Register








"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank