The following code is incomplete and probably doesn't work as is, but it should get where you want to go? Maybe? ---->
<?php
/* Using PDO to connect to database */
$db_options = array(
PDO::ATTR_EMULATE_PREPARES => false // important! use actual prepared statements (default: emulate prepared statements)
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // throw exceptions on errors (default: stay silent)
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // fetch associative arrays (default: mixed arrays)
);
$pdo = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'root', '*****', $db_options);
/* This can be your init.php - just a suggestion */
require ('core/init.php');
// A nice password hashing library:
// Find it here: https://github.com/ircmaxell/password_compat/blob/master/lib/password.php
// Read the Documentation for further help:
// Another suggestion...
include('includes/password.php');
if (isset($_POST['action']) && $_POST['action'] == 'register') {
$username = $_POST['username'];
$password = $_POST['password'];
// Using Regex to check username:
if (preg_match("/^[0-9a-zA-Z_]{5,}$/", $username) === 0) {
$errMsg = '<p>Username must be bigger that 5 chars and contain only digits, letters and underscore<p>';
}
/*
* You can sanitized your user's input here, but remember always validate the data first,
* before you santize your data.
*/
// Using Regex to check password ( I use this on the registration portion, it might be overkill for login page???:
if (preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $password) === 0) {
$errMsg .= '<p>Password must be at least 8 characters, and must contain at least one lower case letter, one upper case letter and one digit.</p>';
}
// Login here, if form data is validated:
if(!$errMsg) {
// Login if there are no errors code goes here:
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Login To Systems</title>
<link rel="stylesheet" href="css/login.css" type="text/css" />
</head>
<body>
<?php echo (isset($errMsg)) ? $errMsg : '<h1>User Login Page:</h1>'; ?>
<div id="login_form">
<table id="login">
<form action="user_login" method="post">
<input type="hidden" name="action" value="login" />
<tr><td colspan="3" align="center"><div id="login_title">Login to Systems</div></td></tr>
<tr><td align="center">Username</td><td align="center">:</td><td align="right"><input type="text" name="username" id="username" /></td></tr>
<tr><td align="center">Password</td><td align="center">:</td><td align="right"><input type="password" name="password" id="password" /></td></tr>
<tr><td> </td><td> </td><td align="right"><input type="submit" id="submit" value="Login" /></td></tr>
</form>
</table>
</div>
</body>
</html>