Last active
March 5, 2017 15:12
-
-
Save pipiscrew/82cf219bdf9223bb8605f0c8e8bee488 to your computer and use it in GitHub Desktop.
Login Form with Login Atempts and 1day expiration cookie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @link https://pipiscrew.com | |
* @copyright Copyright (c) 2016 PipisCrew | |
*/ | |
function connect_mysql() { | |
$mysql_hostname = "localhost"; | |
$mysql_user = ""; | |
$mysql_password = ""; | |
$mysql_database = "test"; | |
$dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_database", $mysql_user, $mysql_password, | |
array( | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, | |
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" | |
)); | |
return $dbh; | |
} | |
function connect_oracle() { | |
//enable ext - php_pdo_oci.dll | |
//src - http://stackoverflow.com/a/36639484 -- https://www.devside.net/wamp-server/connect-wamp-server-to-oracle-with-php-php_oci8_11g-dll | |
$server = "127.0.0.1"; | |
$db_username = "SYSTEM"; | |
$db_password = "Oracle_1"; | |
$sid = "ORCL"; | |
$port = 1521; | |
$dbtns = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = {$server})(PORT = {$port})))(CONNECT_DATA=(SID={$sid})))"; | |
$dbh = new PDO("oci:dbname=" . $dbtns . ";charset=utf8", $db_username, $db_password, array( | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_EMULATE_PREPARES => false, | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC)); | |
return $dbh; | |
} | |
function connect() { | |
//if doesnt exist, will created. | |
$dbh = new PDO('sqlite:dbase.db'); | |
//$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); | |
//check if table has records, if not create table | |
$d = getScalar($dbh, "select count(*) from users",null); | |
if ($d==0) | |
{ | |
executeSQL($dbh, "CREATE TABLE [users] (user_id INTEGER PRIMARY KEY, user_mail TEXT, user_password TEXT, user_level INTEGER)", null); | |
executeSQL($dbh, "your other tables here?",null); | |
//read&write only server (user cant download the dbase) | |
chmod("dbase.db", 0600); | |
} | |
//check if table has records, if not create table | |
return $dbh; | |
} | |
function getScalar($db, $sql, $params) { | |
if ($stmt = $db -> prepare($sql)) { | |
$stmt->execute($params); | |
return $stmt->fetchColumn(); | |
} else | |
return 0; | |
} | |
function getRow($db, $sql, $params) { | |
if ($stmt = $db -> prepare($sql)) { | |
$stmt->execute($params); | |
return $stmt->fetch(); | |
} else | |
return 0; | |
} | |
function getSet($db, $sql, $params) { | |
if ($stmt = $db -> prepare($sql)) { | |
$stmt->execute($params); | |
// return $stmt->fetchAll(PDO::FETCH_ASSOC); | |
return $stmt->fetchAll(); | |
} else | |
return 0; | |
} | |
function executeSQL($db, $sql, $params) { | |
if ($stmt = $db -> prepare($sql)) { | |
$stmt->execute($params); | |
return $stmt->rowCount(); | |
} else | |
return false; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
@session_start(); | |
//invalid login attempts - kick out! | |
if (isset($_SESSION["invalid_login"]) && $_SESSION["invalid_login"]>3) | |
exit; | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
$password_string = md5($_POST["upassword"]); //convert plain text to md5 | |
require_once('config.php'); | |
$db = connect(); | |
//get the dbase password for this mail | |
$r = getRow($db,"select user_id,user_level from users where user_mail=? and user_password=?",array($_POST['umail'], $password_string)); | |
//^if record exists | |
if ($r){ | |
$_SESSION['id'] = $r["user_id"]; | |
$_SESSION['level'] = $r["user_level"]; | |
$_SESSION['login_expiration'] = date("Y-m-d"); | |
header("Location: portal.php"); | |
} | |
else { | |
if (isset($_SESSION["invalid_login"])) | |
$_SESSION["invalid_login"]+=1; | |
else | |
$_SESSION["invalid_login"]=1; | |
//user doesnt exist - create new | |
$sql = "INSERT INTO users (user_mail, user_password, user_level) VALUES (:user_mail, :user_password, :user_level)"; | |
$stmt = $db->prepare($sql); | |
$stmt->bindValue(':user_mail' , $_POST['umail']); | |
$stmt->bindValue(':user_password' , $password_string); | |
$stmt->bindValue(':user_level' , 1); | |
$stmt->execute(); | |
$res = $stmt->rowCount(); | |
if($res == 1) | |
echo "User created successfully!"; | |
else | |
echo "error"; | |
} | |
} | |
//auto go to portal when loggedin | |
if (isset($_SESSION["id"])) { | |
date_default_timezone_set("UTC"); | |
if ($_SESSION["login_expiration"] == date("Y-m-d")) | |
{ | |
header("Location: portal.php"); | |
exit ; | |
} else { | |
session_destroy(); | |
} | |
} | |
?> | |
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> | |
<title>Login</title> | |
<script type="text/javascript" src="assets/jquery-3.0.0.min.js"></script> | |
<script src="assets/bootstrap.min.js"></script> | |
<link href="assets/bootstrap.min.css" rel="stylesheet"> | |
<style> | |
body { | |
padding-top: 40px; | |
padding-bottom: 40px; | |
background-color: #eee; | |
} | |
.form-signin { | |
max-width: 330px; | |
padding: 15px; | |
margin: 0 auto; | |
} | |
.form-signin .form-signin-heading, | |
.form-signin .checkbox { | |
margin-bottom: 10px; | |
} | |
.form-signin .checkbox { | |
font-weight: normal; | |
} | |
.form-signin .form-control { | |
position: relative; | |
height: auto; | |
-webkit-box-sizing: border-box; | |
-moz-box-sizing: border-box; | |
box-sizing: border-box; | |
padding: 10px; | |
font-size: 16px; | |
} | |
.form-signin .form-control:focus { | |
z-index: 2; | |
} | |
.form-signin input[type="email"] { | |
margin-bottom: -1px; | |
border-bottom-right-radius: 0; | |
border-bottom-left-radius: 0; | |
} | |
.form-signin input[type="password"] { | |
margin-bottom: 10px; | |
border-top-left-radius: 0; | |
border-top-right-radius: 0; | |
} | |
</style> | |
<script> | |
$(function() { | |
}); | |
</script> | |
</head> | |
<body> | |
<div class="container"> | |
<form class="form-signin" method="POST" action=""> | |
<h2 class="form-signin-heading">Please sign in</h2> | |
<label for="umail" class="sr-only">Email address</label> | |
<input type="email" name="umail" class="form-control" placeholder="Email address" required autofocus> | |
<label for="upassword" class="sr-only">Password</label> | |
<input type="password" name="upassword" id="upassword" class="form-control" placeholder="Password" required> | |
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> | |
</form> | |
</div> <!-- /container --> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
@session_start(); | |
session_destroy(); | |
header("Location: index.php"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
@session_start(); | |
if (!isset($_SESSION["id"])) { | |
header("Location: index.php"); | |
exit ; | |
} | |
else { | |
date_default_timezone_set("UTC"); | |
if ($_SESSION["login_expiration"] != date("Y-m-d")) | |
{ | |
session_destroy(); | |
header("Location: index.php"); | |
exit ; | |
} | |
} | |
?> | |
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> | |
<script type="text/javascript" src="assets/jquery-3.0.0.min.js"></script> | |
<link rel="stylesheet" type="text/css" href="assets/bootstrap.min.css" /> | |
<script type="text/javascript" src="assets/bootstrap.min.js"></script> | |
<script type="text/javascript"> | |
var selected_node = null; | |
var loading = $('<div class="modal-backdrop"></div><div class="progress progress-striped active loading"><div class="progress-bar" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%">'); | |
$(function() { | |
//edit record | |
$('#edit_node').on('click', function(e) { | |
e.preventDefault(); | |
if (selected_node == null){ | |
alert("must select a node!"); | |
return; | |
} | |
loading.appendTo(document.body); | |
$("#doc_container").load('doc_details.php?id=' + selected_node, function() { | |
loading.remove(); | |
}); | |
}); | |
}); //jQuery ends | |
</script> | |
<style> | |
.img-hover img { | |
-webkit-transition: all .3s ease; /* Safari and Chrome */ | |
-moz-transition: all .3s ease; /* Firefox */ | |
-o-transition: all .3s ease; /* IE 9 */ | |
-ms-transition: all .3s ease; /* Opera */ | |
transition: all .3s ease; | |
} | |
.img-hover img:hover { | |
-webkit-backface-visibility: hidden; | |
backface-visibility: hidden; | |
-webkit-transform:translateZ(0) scale(1.20); /* Safari and Chrome */ | |
-moz-transform:scale(1.20); /* Firefox */ | |
-ms-transform:scale(1.20); /* IE 9 */ | |
-o-transform:translatZ(0) scale(1.20); /* Opera */ | |
transform:translatZ(0) scale(1.20); | |
} | |
.grayscale { | |
-webkit-filter: brightness(1.10) grayscale(100%) contrast(90%); | |
-moz-filter: brightness(1.10) grayscale(100%) contrast(90%); | |
filter: brightness(1.10) grayscale(100%); | |
} | |
/*progress*/ | |
.modal-backdrop { opacity: 0.7; filter: alpha(opacity=70); background: #fff; z-index: 2;} | |
div.loading { position: fixed; margin: auto; top: 0; right: 0; bottom: 0; left: 0; width: 200px; height: 30px; z-index: 3; } | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="row img-hover"> | |
<div class="col-xs-1"> | |
<img src="assets/edit.png" id="edit_node" class="img-responsive img-rounded" > | |
</div> | |
<div class="col-xs-1"> | |
<a href="logout.php"><img src="assets/logout.png" id="logout" class="img-responsive img-rounded" ></a> | |
</div> | |
</div> | |
<div class="row"> | |
<!-- your content here --> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE [users] (user_id INTEGER PRIMARY KEY, user_mail TEXT, user_password TEXT, user_level INTEGER) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment