Haddman;10931732 wrote:Hadn't thought of that.....
Thanks for the advise, Neville1985 and laserlight, but seeing as i barely know what you're doing i came up with something different, and more simple.
User:
if($user!=""){
$whitespacecheck=str_replace(" ","",$user);
if($whitespacecheck==""){
// Error
}
}
That works fine, but I really wouldn't suggest it for almost every security reason under the sun, as laser and I have said, regular expressions are the best way to handle things like this. You can use something like the above to edit the string after the fact.
Password one; Let me see if i got this right.
- Counts the $pass strings length
- Trims the white-space from either end of the $pass string.
- Recounts the string length then checks to see if its equal to the original string length.
I see your logic and it works, although if you use trim() you will get yourself into trouble with white-space(s) within your $pass string.
$pass1 = " pass 1 ";
$pass2 = " pass 2 ";
//result of password after str_replace() and trim() respectively
$pass1 = "pass1";
$pass2 = "pass 2";
You won't remove the internal white-space. If you really want to continue along this path do this:
//Assuming $user and $pass have been filled by a $_POST, $_GET or $_REQUEST
//It would look like this:
//$user = (string) $_REQUEST['username'];
//$pass = (string) $_REQUEST['password'];
$user = (string) " user 1 2 3 ";
$pass = (string) " pass 1 2 3 ";
$pass_cnt_1 = (int) countLen($pass);
$user = (string) removeWS($user);
$pass = (string) removeWS($pass);
$pass_cnt_2 = (int) countLen($pass);
if ( $user && $pass != "" && $pass_cnt_1 === $pass_cnt_2) {
//Pass Scripts Here
} else {
//Failed Scripts Here
unset($user,$pass,$pass_cnt_1,$pass_cnt_2); //Release the variables
}
//function that removes white-spaces
function removeWS($input) {
$value = str_replace(" ", "",$input);
return $value;
}
//function that counts string length
function countLen($input) {
$total = strlen($input);
return $total;
}
Now, before you go off I understand this is a little more programming (even though not by much.) Also I am understanding that your not as experienced as some on this board. Although programming this way shows you good practices, some 'security' (lol), refined if...else statement, reusable functions and last but not least, its more readable.
All you need to do is replace the "test 1 2 3" strings in the $user and $pass vars with data of your choice and off you go.
Remember it will blank page if you don't tell it to do something in the //Pass / Failed script areas
Hope this helps.
@: thanks for the expression fix.