PHP Allowing safe non-alphanumeric characters

I am still trying to make sense of PHP regular expressions.

My first half of the question is a bit of a security one.
My second is more syntactical.

I want to allow users to have some of the symbols such as (!, *, #, %) in there passwords for better security.  I wont be writing these passwords directly to the DB, they will be stored in alpha-numeric MD5 so I'm not to worried about the back-end queries.

Is it wise to allow some non-alphanumeric field input (PHP, MySQL environment)?
If so, what characters are safe?

Second question is about my attached code,
I chose some symbols to allow and am detecting invalid input.  I am not to confident in my abilities with regular expressions so; "how does it look?"
function validate_password($password) {
	$allowed_char_pattern = "[^-A-Za-z0-9!.#%&$*_]";
	//	END CONFIG
	// =============================================================================================
	
	$clean_string = ereg_replace( $allowed_char_pattern, "", $password );
	if(!strcmp($password, $clean_string))
		{return true;}
	return false;
}

Open in new window

LVL 5
gambit_642Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hernst42Commented:
> If so, what characters are safe?
all if you deal correctly with user input. If you add user-input to the database always use mysql_real_escape_string to quote the string correctly for the database.
If you redisplay the user-input make sure to also escape that output correctly with htmlenties.

I prefer preg-functions:
 
function validate_password($password)
{
        $allowed_char_pattern = "/[^-A-Za-z0-9!.#%&$*_]/";
        //      END CONFIG
        // =============================================================================================
        
        $clean_string = preg_replace( $allowed_char_pattern, "", $password );
        if ($password !== $clean_string) {
                return false;
        }
        return true;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gambit_642Author Commented:
Will that handle SQL keywords like "select", "insert", "update"?
0
hernst42Commented:
Yes mysql_real_escape_string also deas correctly with SQL-Tokens if the are declared within a string
0
gambit_642Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.