HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Need Loops for PHP Username/Pass Encryption Script

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptencryptionneedpassphploopsusernamefor

Problem

I've created this PHP script to print a batch of usernames with encrypted passwords locally on my computer because the user/pass format is always the same.

username = username

password = username + "admin"

example = homer / homeradmin

Then paste them into two different .htpasswd file and upload to an public Apache server to password-protect a directory and a directory within it.

Is there a better way to write the code below?


  
  
  Encrypt
  
  

";
echo $user2 . ":" . $pass2 . "";
echo $user3 . ":" . $pass3 . "";
echo $user4 . ":" . $pass4 . "";
echo $user5 . ":" . $pass5 . "";
echo $user6 . ":" . $pass6 . "";
echo $user7 . ":" . $pass7 . "";
echo $user8 . ":" . $pass8 . "";
?>

";
echo $admin2 . ":" . $adminpass2 . "";
echo $admin3 . ":" . $adminpass3 . "";
echo $admin4 . ":" . $adminpass4 . "";
echo $admin5 . ":" . $adminpass5 . "";
echo $admin6 . ":" . $adminpass6 . "";
echo $admin7 . ":" . $adminpass7 . "";
echo $admin8 . ":" . $adminpass8 . "";
?>

Solution

the first thing that came to my mind to make this cleaner was an array or rather several arrays.

maybe something like this:

$user = array('admin','g','homer','marge','bart','lisa','maggie','dog');
$admin = array('admin','23!bseEsF@','homeradmin','margeadmin','bartadmin','maggieadmin','dogadmin');

$password = array(8); //my syntax may be wrong here
$adminPassword = array(8); //my syntax may be wrong here

for ($x=0; $x < count($user); x$++)
{
    $password[$x] = crypt($user[$x], base64_encode($user[$x]));
    $adminPass[$x] = crypt($admin[$x], base64_encode($admin[$x]));
}


And then your Echo's would look like this.

echo $user1 . ":" . $pass1 . "";
for ($y=0; $y ";
}
for ($z=0; $z ";
}


The only way that this works is if all the arrays are the same length and arrays in PHP are base 0. otherwise you have to change the for declarations a little bit.

Again I say that my Syntax may not be perfect or even well formed, but this is much cleaner than what you have.

Syntax not Guaranteed

Code Snippets

$user = array('admin','g','homer','marge','bart','lisa','maggie','dog');
$admin = array('admin','23!bseEsF@','homeradmin','margeadmin','bartadmin','maggieadmin','dogadmin');

$password = array(8); //my syntax may be wrong here
$adminPassword = array(8); //my syntax may be wrong here

for ($x=0; $x < count($user); x$++)
{
    $password[$x] = crypt($user[$x], base64_encode($user[$x]));
    $adminPass[$x] = crypt($admin[$x], base64_encode($admin[$x]));
}
echo $user1 . ":" . $pass1 . "<br/>";
for ($y=0; $y < count($user); $y++)
{
    echo $user[$y] . ":" . $pass[$y] . "<br/>";
}
for ($z=0; $z < count($admin); $z++)
{
    echo $admin[$z] . ":" . $adminPass[$z] . "<br/>";
}

Context

StackExchange Code Review Q#38124, answer score: 2

Revisions (0)

No revisions yet.