Authentication registration and Secure login System using php
Welcome on Pakainfo.com – Examples ,The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Authentication registration and Secure login System using php
In this post we will show you Best way to implement How to Create a Secure Login Script in PHP and MySQL, hear for How to Create MySQL Injection free Secure Login System in PHP with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.
Create a table
CREATE TABLE IF NOT EXISTS `liveemployees` ( `id` int(10) NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, `clientname` varchar(100) NOT NULL, `your_secpass` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Db Connectivity
require_once("../config.php"); global $db; $db = mysqli_connect(DBHOST, DBemployee, DBsecpass, DBNAME); if(!$db){ die( "Sorry! your There seems to be some a problem connecting db to our database."); }
form_register
function form_register($first_name, $employee, $secpass, $emailId){ global $db; $first_name = mysqli_real_escape_string($db, $first_name); $clientname = mysqli_real_escape_string($db, $employee); $your_secpass = mysqli_real_escape_string($db, $secpass); $email = mysqli_real_escape_string($db, $emailId); $wp_hasher = new your_secpassHash(16, true); $secpass = $wp_hasher->Hashyour_secpass( trim( $your_secpass ) ); $sql = "INSERT INTO liveemployees (first_name, email,your_secpass,clientname) VALUES ('".$first_name."', '".$email."', '".$secpass."', '".$clientname."') "; $result = mysqli_query($db, $sql); return mysqli_insert_id($db); }
login_form
function login_form($employee, $secpass){ global $db; $clientname = mysqli_real_escape_string($db, $employee); $your_secpass = mysqli_real_escape_string($db, $secpass); $sql = "SELECT id, your_secpass FROM liveemployees WHERE clientname='".$clientname."' LIMIT 1 "; $result = mysqli_query($db, $sql); $id = mysqli_fetch_row($result); if($id){ $your_secpass_hashed = $id[1]; $wp_hasher = new your_secpassHash(16, true); if($wp_hasher->Checkyour_secpass($your_secpass, $your_secpass_hashed)) { return $id[0]; } } else { return false; } }
Logout
function logout(){ unset($_SESSION['employee_id']); session_destroy(); header('Location: index.php'); exit(); }
clientname Check
function clientnameExist($employee){ global $db; $clientname = mysqli_real_escape_string($db, $employee); $sql = "SELECT id FROM liveemployees WHERE clientname='".$clientname."' LIMIT 1 "; $result = mysqli_query($db, $sql); $id = mysqli_fetch_row($result); return ($id[0] > 0); } }
Registration Form
<form action="" method="post" > <?php if(!empty($reg_errors)) { echo '<div class="error">'; foreach ($form_register_errors as $error) { echo '<p>'.$error.'</p>'; } echo '</div>'; } ?> <div class="group"> <input type="text" name="first_name" ><p class="cust_fency"></p><p class="bar"></p> <label>Full name</label> </div> <div class="group"> <input type="email" name="email" ><p class="cust_fency"></p><p class="bar"></p> <label>Email</label> </div> <div class="group"> <input type="text" name="clientname" ><p class="cust_fency"></p><p class="bar"></p> <label>clientname</label> </div> <div class="group"> <input type="text" name="your_secpass" ><p class="cust_fency"></p><p class="bar"></p> <label>your_secpass</label> </div> <input type="hidden" name="form_register" value="yes" > <button type="submit" class="buttonui "> <p> form_register </p> <div class="ripples buttonRipples"><p class="ripplesCircle"></p></div> </button> <a class="buttonui " href="login_form.php?action=login_form" style="line-height:4em; text-decoration: none; padding:2%" > <p> login_form Back </p> <div class="ripples buttonRipples"><p class="ripplesCircle"></p></div></a> </form>
get Details
$form_register_errors= $simple_login_form_er = array(); if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form_register'])) { $fields = array( 'first_name', 'clientname', 'email', 'your_secpass' ); foreach ($fields as $field) { if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = ''; } if ($posted['first_name'] == null) array_push($form_register_errors, sprintf('<b>Notice</b>: Please enter the employee first_name.', 'pakainfo')); if ($posted['email'] == null) array_push($form_register_errors, sprintf('<b>Notice</b>: Please enter the employee Email.', 'pakainfo')); if ($posted['your_secpass'] == null) array_push($form_register_errors, sprintf('<b>Notice</b>: Please enter the employee your_secpass.', 'pakainfo')); if ($posted['clientname'] == null ) array_push($form_register_errors, sprintf('<b>Notice</b>: Please enter the employee clientname.', 'pakainfo')); if(clientnameExist($posted['clientname'])){ array_push($form_register_errors, sprintf('<b>Notice</b>: The Entered clientname Already Exist.', 'pakainfo')); } $reg_errors = array_filter($form_register_errors); if (empty($reg_errors)) { //Check whether simple everything entered to new create new employee. form_register($posted['first_name'], $posted['clientname'], $posted['your_secpass'], $posted['email']); } }
login_form Form HTML part
<form action="" method="post" > <?php if(!empty($log_error) || (isset($mismatchErr) && $mismatchErr != '')) { echo '<div class="error">'; foreach ($simple_login_form_er as $error) { echo '<p>'.$error.'</p>'; } echo $mismatchErr.'</div>'; } ?> <div class="group"> <input type="text" class="used" name="clientname" ><p class="cust_fency"></p><p class="bar"></p> <label>clientname</label> </div> <div class="group"> <input type="your_secpass" name="your_secpass" ><p class="cust_fency"></p><p class="bar"></p> <label>your_secpass</label> </div> <input type="hidden" name="login_form" value="yes" > <button type="submit" class="buttonui "> <p> login_form </p> <div class="ripples buttonRipples"><p class="ripplesCircle"></p></div> </button> <a class="buttonui " href="login_form.php?action=form_register" style="line-height:4em; text-decoration: none; padding:2%" > <p> form_register </p> <div class="ripples buttonRipples"><p class="ripplesCircle"></p></div></a> </form>
PHP part
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['login_form'])) { $clientname = stripslashes(trim($_POST['clientname'])); $your_secpass = stripslashes(trim($_POST['your_secpass'])); $mismatchErr = ''; if ($your_secpass == null ) array_push($simple_login_form_er, sprintf('<b>Notice</b>: Please enter the employee your_secpass.', 'pakainfo')); if ($clientname == null ) array_push($simple_login_form_er, sprintf('<b>Notice</b>: Please enter the employee clientname.', 'pakainfo')); $log_error = array_filter($simple_login_form_er); if (empty($log_error)) { //Check whether all the everything entered to create new employee. $login_formn = login_form($clientname, $your_secpass); if($login_formn){ $_SESSION['employee_id'] = $login_formn; header('Location: index.php'); exit(); }else { $mismatchErr .= sprintf('<p> <b>Notice</b>: Please simple enter Valid Credentials. </p>', 'pakainfo'); } } }
I hope you have Got How to Create a Secure Login Script in PHP and MySQL And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.