As Drawmack stated, Regex would be useful for preventing dangerous characers. Below is an example of that.
<?php
function regex_checkusername($name)
{
if(preg_match('^[a-zA-Z0-9-_]^',$_POST['username']))
{
return true;
} else {
return false;
}
}
?>
Then it could be used like this.
<?php
if(regex_checkusername($_POST['username']) == false)
{
echo('Your username was not valid!');
}
?>