simple php login system code example
Example 1: how to create a logfile in php?
<?php
//Something to write to txt log
$log = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
"Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
"User: ".$username.PHP_EOL.
"-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
Example 2: how to make a working login system using code
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<script>
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
function signUp(){
var email = document.getElementById("email");
var password = document.getElementById("password");
const promise = auth.createUserWithEmailAndPassword(email.value, password.value);
promise.catch(e => alert(e.message));
alert("Signed Up");
}
function signIn(){
var email = document.getElementById("email");
var password = document.getElementById("password");
const promise = auth.signInWithEmailAndPassword(email.value, password.value);
promise.catch(e => alert(e.message));
}
function signOut(){
auth.signOut();
alert("Signed Out");
}
auth.onAuthStateChanged(function(user){
if(user){
var email = user.email;
alert("Signed in as " + email);
}else{
alert("No Active User");
}
});g
</script>
<style type="text/css">
body{
background-color: #55d6aa;
}
h1{
background-color: #ff4d4d;
margin: 10px auto;
text-align: center;
color: white;
}
#formContainer{
background-color: white;
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
width: 25%;
height: 45;
margin: 10px auto;
}
#header{
width: 100%;
height: 10px;
background: black;
}
#email{
width: 70%;
height: 40px;
display:block;
margin: 25px auto;
border: none;
outline: none;
border-bottom: 2px solid black;
}
#password{
width: 70%;
height: 40px;
display: block;
margin: 10px auto;
border: none;
outline: none;
border-bottom: 2px solid black;
}
#signUp{
background-color: #ff4d4d;
color: white;
border: none;
font-weight: bold;
padding: 15px 32px;
border-radius: 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 13px;
margin-top: 20px;
margin-left: 50px;
}
#signIn{
background-color: #32ff7e;
color: white;
font-weight: bold;
border: none;
padding: 15px 35px;
border-radius: 10px;
text-align: center;
text-decoration: none;
font-size: 13px
}
#signOut{
background-color: #FFA500;
color: white;
border: none;
padding: 12px 32px;
border-radius: 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 13px;
margin-top: 9px;
margin-left: 74px;
font-weight: bold;
}
button: hover{
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 7px 50px 0 rgba(0,0,0,0,.19);
}
</style>
</head>
<body>
<h1>Login Here</h1>
<div id="formContainer">
<div id="header"> </div>
<input type="email" placeholder="Email" id="email">
<input type="password" placeholder="Password" id="password">
<button onclick="signUp()" id="signUp"> Sign Up </button>
<button onclick="signIn()" id="signIn"> Sign In </button>
<button onclick="signOut()" id="signOut"> Sign Out </button>
Continue</a>
</body>
</html>
Example 3: login php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<form action="" method="POST">
<input type="password" name="password">
<button type="submit" name="submit">Login</button>
</form>
<?php
if(isset($_POST['submit'])){
if(password_verify($_POST['password'], '$2y$10$sejeRNYZGaoPh1EwfcuO1.hxl/uepQOh9SITWWgeej86vnMt26KIa')){
$_SESSION['login'] = true;
header("Location: http://localhost");
}
}
?>
</body>
</html>
Example 4: php login system with admin features
include('class/User.php');
$user = new User();
$message = $user->login();
Example 5: login.php
<?php
ob_start();
session_start();
?>
<?
// error_reporting(E_ALL);
// ini_set("display_errors", 1);
?>
<html lang = "en">
<head>
<title>Tutorialspoint.com</title>
<link href = "css/bootstrap.min.css" rel = "stylesheet">
<style>
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #ADABAB;
}
.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
color: #017572;
}
.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;
border-color:#017572;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
border-color:#017572;
}
h2{
text-align: center;
color: #017572;
}
</style>
</head>
<body>
<h2>Enter Username and Password</h2>
<div class = "container form-signin">
<?php
$msg = '';
if (isset($_POST['login']) && !empty($_POST['username'])
&& !empty($_POST['password'])) {
if ($_POST['username'] == 'tutorialspoint' &&
$_POST['password'] == '1234') {
$_SESSION['valid'] = true;
$_SESSION['timeout'] = time();
$_SESSION['username'] = 'tutorialspoint';
echo 'You have entered valid use name and password';
}else {
$msg = 'Wrong username or password';
}
}
?>
</div>
<div class = "container">
<form class = "form-signin" role = "form"
action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']);
?>" method = "post">
<h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
<input type = "text" class = "form-control"
name = "username" placeholder = "username = tutorialspoint"
required autofocus></br>
<input type = "password" class = "form-control"
name = "password" placeholder = "password = 1234" required>
<button class = "btn btn-lg btn-primary btn-block" type = "submit"
name = "login">Login</button>
</form>
Click here to clean <a href = "logout.php" tite = "Logout">Session.
</div>
</body>
</html>