3
\$\begingroup\$

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?

<?php
// Password to be encrypted for a .htpasswd file
$user1 = 'admin';
$user2 = 'g';
$user3 = 'homer';
$user4 = 'marge';
$user5 = 'bart';
$user6 = 'lisa';
$user7 = 'maggie';
$user8 = 'dog';


//Admin
$admin1 = 'admin';
$admin2 = '23!bseEsF@';
$admin3 = 'homeradmin';
$admin4 = 'margeadmin';
$admin5 = 'bartadmin';
$admin6 = 'lisaadmin';
$admin7 = 'maggieadmin';
$admin8 = 'dogadmin';

// Encrypt password
$pass1 = crypt($user1, base64_encode($user1));
$pass2 = crypt($user2, base64_encode($user2));
$pass3 = crypt($user3, base64_encode($user3));
$pass4 = crypt($user4, base64_encode($user4));
$pass5 = crypt($user5, base64_encode($user5));
$pass6 = crypt($user6, base64_encode($user6));
$pass7 = crypt($user7, base64_encode($user7));
$pass8 = crypt($user8, base64_encode($user8));


$adminpass1 = crypt($admin1, base64_encode($admin1));
$adminpass2 = crypt($admin2, base64_encode($admin2));
$adminpass3 = crypt($admin3, base64_encode($admin3));
$adminpass4 = crypt($admin4, base64_encode($admin4));
$adminpass5 = crypt($admin5, base64_encode($admin5));
$adminpass6 = crypt($admin6, base64_encode($admin6));
$adminpass7 = crypt($admin7, base64_encode($admin7));
$adminpass8 = crypt($admin8, base64_encode($admin8));
?>

<!doctype html>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Encrypt</title>
  <meta name="viewport" content="width=device-width">
  <head>
</head>
<body>

<p>
<?php
// Print encrypted password
echo $user1 . ":" . $pass1 . "<br/>";
echo $user2 . ":" . $pass2 . "<br/>";
echo $user3 . ":" . $pass3 . "<br/>";
echo $user4 . ":" . $pass4 . "<br/>";
echo $user5 . ":" . $pass5 . "<br/>";
echo $user6 . ":" . $pass6 . "<br/>";
echo $user7 . ":" . $pass7 . "<br/>";
echo $user8 . ":" . $pass8 . "<br/>";
?>
</p>

<p>
<?php
echo $admin1 . ":" . $adminpass1 . "<br/>";
echo $admin2 . ":" . $adminpass2 . "<br/>";
echo $admin3 . ":" . $adminpass3 . "<br/>";
echo $admin4 . ":" . $adminpass4 . "<br/>";
echo $admin5 . ":" . $adminpass5 . "<br/>";
echo $admin6 . ":" . $adminpass6 . "<br/>";
echo $admin7 . ":" . $adminpass7 . "<br/>";
echo $admin8 . ":" . $adminpass8 . "<br/>";
?>
</p>

</body>
</html>
\$\endgroup\$
7
  • 1
    \$\begingroup\$ this is off-topic for this site in that it is asking for code to be written, we don't do that here. \$\endgroup\$
    – Malachi
    Dec 26, 2013 at 18:30
  • \$\begingroup\$ @Malachi This question seems to just be written differently. I believe the existing code here works and that it is desperately in need for a review. I interpret the question as "Is there a way I can make this better, with less code?" \$\endgroup\$ Dec 26, 2013 at 19:18
  • \$\begingroup\$ I don't see any traces of the .htaccess stuff though, and we won't help you write that, @Conor. I hope someone will help you write the existing code in a better way. \$\endgroup\$ Dec 26, 2013 at 19:19
  • 1
    \$\begingroup\$ @SimonAndréForsberg: If so, it could be edited to reflect that. As it's worded, it sounds like a request for code. \$\endgroup\$
    – Jamal
    Dec 26, 2013 at 19:22
  • 1
    \$\begingroup\$ @SimonAndréForsberg I have the .htaccess files already pointed to these .htpasswd files on the server. Everything works fine, just need to be pointed in the right direction on how to write this code more efficiently. \$\endgroup\$
    – Conor
    Dec 26, 2013 at 19:33

1 Answer 1

2
\$\begingroup\$

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 . "<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/>";
}

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

\$\endgroup\$
1
  • \$\begingroup\$ @Conor, your welcome. having them in an array really helps. but you have to make sure they are in the right order \$\endgroup\$
    – Malachi
    Dec 27, 2013 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.