login.php
<div id="login-wrapper">
// здесь echo
<form method="post" action="">
<ul>
<li>
<label for="usn">Username : </label>
<input type="text" maxlength="30" required autofocus name="username"/>
</li>
<li>
<label for="passwd">Password : </label>
<input type="password" maxlength="30" required name="password"/>
</li>
<li class="buttons">
<input type="submit" name="login" value="Log me in"/>
<input type="button" name="register" value="Register" onclick="location.href='register.php'"/>
</li>
</ul>
</form>
</div>
register.php
<div id="register-wrapper">
// здесь echo
<form method="post">
<ul>
<li>
<label for="usn">Username : </label>
<input type="text" id="usn" maxlength="30" required autofocus name="username"/>
</li>
<li>
<label for="passwd">Password : </label>
<input type="password" id="passwd" maxlength="30" required name="password"/>
</li>
<li>
<label for="conpasswd">Confirm Password : </label>
<input type="password" id="conpasswd" maxlength="30" required name="conpassword"/>
</li>
<li class="buttons">
<input type="submit" name="register" value="Register"/>
<input type="button" name="cancel" value="Cancel" onclick="location.href='index.php'"/>
</li>
</ul>
</form>
</div>
index.php
<?php
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<header id="head">
<p><a href="register.php"><span id="register">Register</span></a></p>
</header>
<?php if (!(isset($_POST['login']))) { ?>
<div id="main-wrapper">
<?php include "login.php"; ?>
<?php } else {
$usr = new Users;
$usr->storeFormValues($_POST);
if ($usr->userLogin()) {
session_start();
$_SESSION['username'] = $usr->username;
header("Location:profile.php");
} else {
echo "Incorrect username or password";
include "login.php";
}
} ?>
<?php if (!(isset($_POST['register']))) {
include 'register.php';
} else {
$usr = new Users;
$usr->storeFormValues($_POST);
if ($_POST['password'] == $_POST['conpassword']) {
echo $usr->register($_POST);
include 'register.php';
} else {
include 'register.php';
echo "Password and Confirm password not match";
}
}
?>
</div>
</body>
</html>
<?php
class Users
{
public $username = null;
public $password = null;
public $salt = "Zo4rU5Z1YyKJAASY0PT6EUg7BBYdlEhPaNLuxAwU8lqu1ElzHv0Ri7EM6irpx5w";
public function __construct($data = array())
{
if (isset($data['username'])) $this->username = stripslashes(strip_tags($data['username']));
if (isset($data['password'])) $this->password = stripslashes(strip_tags($data['password']));
}
public function storeFormValues($params)
{
//store the parameters
$this->__construct($params);
}
public function userLogin()
{
$success = false;
try {
$con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1";
$stmt = $con->prepare($sql);
$stmt->bindValue("username", $this->username, PDO::PARAM_STR);
$stmt->bindValue("password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR);
$stmt->execute();
$valid = $stmt->fetchColumn();
if ($valid) {
$success = true;
}
$con = null;
return $success;
} catch (PDOException $e) {
echo $e->getMessage();
return $success;
}
}
public function register()
{
$correct = false;
$con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$sql = $con->prepare("SELECT `username` FROM `users` WHERE `username` = ?");
$sql->bindValue(1, $this->username);
$sql->execute();
if ($sql->rowCount() > 0) { # If rows are found for query
$error = "Email found!";
return $error;
} else {
try {
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO users(username, password) VALUES(:username, :password)";
$stmt = $con->prepare($sql);
$stmt->bindValue("username", $this->username, PDO::PARAM_STR);
$stmt->bindValue("password", hash("sha256", $this->password . $this->salt), PDO::PARAM_STR);
$stmt->execute();
return "Registration Successful <br/> <a href='index.php'>Login Now</a>";
} catch (PDOException $e) {
return $e->getMessage();
}
}
}
}
?>