07-01-2009, 07:10 PM
[eluser]Pinkiee[/eluser]
The code below is for demonstration only.
I am new to CI, and I have not read up on the conventions
I also have not tested the code below
I am aware that there are still better ways to do this.
There is a large lack of comments
etc.
But I think it displays what I was saying.
One for loop, no arrays, no if statements that are not needed.
I really do think that this is a great idea, and I would love to see it turned into a library where you can "tweak" the values etc. I might even get around to it myself one day.
The code below is for demonstration only.
I am new to CI, and I have not read up on the conventions
I also have not tested the code below
I am aware that there are still better ways to do this.
There is a large lack of comments
etc.
But I think it displays what I was saying.
One for loop, no arrays, no if statements that are not needed.
I really do think that this is a great idea, and I would love to see it turned into a library where you can "tweak" the values etc. I might even get around to it myself one day.
Code:
function check_strength($password)
{
$password_length = strlen($password);
if ($password_length > 0)
{
// Length
if ($password_length > 0)
{
$_points += 5;
if($password_length >= 5)
{
$_points += 5;
if($password_length >= 7)
{
$_points += 15;
}
}
}
// gather the information
foreach ($password as $character)
{
if (ctype_lower($character))
{
$lower_case_count++;
}
else if (ctype_upper($character))
{
$upper_case_count++;
}
else if (ctype_digit($character))
{
$numeric_count++
}
else if (ctype_punct($character))
{
$special_count++;
}
}
if($lower_case_count > 0)
{
$_points += 10;
}
if($upper_case_count > 0)
{
$_points += 10;
}
// numeric
if($numeric_count > 0)
{
if ($numeric_count <= 2)
{
$_points += 10;
}
else
{
$_points += 20;
}
}
// special characters
if ($special_count > 0)
{
if ($special_count == 1)
{
$_points += 10;
}
else
{
$_points += 25;
}
}
// bonus - note you were "double checking" values you had already checked
// this does not give a bonus for jsut having both lower and upper
// nor for lower AND upper AND numeric
// I have not changed it though as this may be by design
if($numeric_count > 0 AND ($lower_case_count > 0 OR $upper_case_count > 0))
{
$_points += 2;
if($special_count > 0)
{
$_points += 1;
if($lower_case_count > 0 AND $upper_case_count > 0)
{
$_points += 2;
}
}
}
}
return $_points;
}