Continuing with our last topic, we’ll now configure the Mysql tables and the Apache2 files needed to get authentication working with mod_auth_form and mod_dbd.
Step 1) Create mysql table
CREATE TABLE apache_user ( username CHAR(100) NOT NULL, password CHAR(100) NOT NULL, PRIMARY KEY (username) );
Step 2) Create the webpages to register and login the user.
a) Create the registration page to collect user info
<html> <body> <form action="createUser.php" method="post"> Email: <input type="text" name="email"> Password: <input type="password" name="pass"> <input type="submit" name="register" value="register" /> </form> </body> </html>
b) Create the page that generates a login for the user
<?php $login = $_POST['email'];; $pass = $_POST['pass'];; $hash = '{SHA}' . base64_encode(sha1($pass, true)); $username="root"; $password="XXX"; $database="myDB"; $host="localhost"; $query="INSERT INTO apache_user VALUES('".$login."','".$hash."')"; mysql_connect($host,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); mysql_query($query); mysql_close(); echo "created " . $login; ?>
Apache2 will accept other password formats. If you care to know or try other ones, read this.
c) Create the login page for users with accounts. Users will be directed to this page when they try to access a secured area such as http://localhost/private as defined in the <Location> element of httpd.conf
<html> <head> <title>Login page</title> </head> <body> <form method="POST" action=""> User: <input type="text" name="httpd_username" value="" /> Pass: <input type="password" name="httpd_password" value="" /> <input type="submit" name="login" value="Login" /> </form> </body> </html>
[…] time ago, I detailed in horrific detail how to build and set up Apache 2.4 in Ubuntu 12: part I and part II. It worked but it was a […]