Implement login registration PHP to look like that <?php session_start(); includ
ID: 3916335 • Letter: I
Question
Implement login registration PHP to look like that
<?php
session_start();
include ("connect.php");
if($_SESSION['counter']==null)
{
$_SESSION['counter']=1;
}
$msg = "";
if(isset($_POST["check"]))
{
if($_POST["username1"]=='')
{
$msg = "Enter User Name...";
//return false;
}
else if($_POST["ddlsecurity"]=="0")
{
$msg = "Select Security Question...";
//return false;
}
else if(empty($_POST["answer"]))
{
$msg = "Enter Answer...";
//return false;
}
else{
$username1 = $_POST["username1"];
$ddlsecurity = $_POST["ddlsecurity"];
$answer = $_POST["answer"];
$username1 = mysqli_real_escape_string($con, $username1);
$ddlsecurity = mysqli_real_escape_string($con, $ddlsecurity);
$answer = mysqli_real_escape_string($con, $answer);
$sql="SELECT Password FROM `tbl_user_registration` WHERE User_Name='$username1' and Security_Question_Id='$ddlsecurity' and Security_Answer='$answer'";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result) == 1)
{
//$_SESSION['User_Id'] = $row['Password'];
$msg = "Your Password is:".$row['Password']; /* Redirect browser */
//exit();
}
else
{
$msg = "Sorry..! Invalid Answer";
}
}
}
if(isset($_POST["submit"]))
{
if($_POST["username"]=='')
{
$msg = "Enter User Name...";
//return false;
}
else if($_POST["password"]=='')
{
$msg = "Enter Password...";
//return false;
}
else
{
$username = $_POST["username"];
$password = $_POST["password"];
$username = mysqli_real_escape_string($con, $username);
$password = mysqli_real_escape_string($con, $password);
//$password = md5($password);
$sql="SELECT User_Id FROM `tbl_user_registration` WHERE User_Name='$username' and Password='$password'";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result) == 1)
{
$_SESSION['User_Id'] = $row['User_Id'];
$msg = "Successfully Log in"; /* Redirect browser */
//exit();
}
else
{
$msg = "Sorry..! Invalid UserName and Password".$_SESSION['counter'];
$_SESSION['counter']=$_SESSION['counter']+1;
if($_SESSION['counter']==3)
{
?>
<script> var x = document.getElementById('divlogin'); x.style.display = 'none'; var y = document.getElementById('divsecurity'); y.style.display = 'block';</script>
<?php
$_SESSION['counter']=1;
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>login</title>
<style>
.txtcontrol{
height: 40px;
font-size: 22px;
border: single;
border-color:black;
width: 100%;
margin-bottom:10px;
text-align:center;
}
.mcontainer {
max-width: 600px;
margin: 0 auto;
padding: 80px 0;
height: 400px;
text-align: center;
}
.mcontainer {
width: 100%;
padding-right: 10px;
padding-left: 10px;
margin-right: auto;
margin-left: auto;
}
.sbutton
{
width: 42%;
height: 42px;
background-color: #75d4b6;
border: none;
color: #f9f5ee;
font-size: 17px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="mcontainer">
<h1>Welcome</h1>
<form method="post" action="">
<?php
if($msg!='')
{
echo " <div>
<h4>Message</h4>
$msg</div>";
}
?>
<div id="divlogin">
<table align="center" width="60%">
<tr>
<td>
<input type="text" placeholder="Email Id" class="txtcontrol" name="username"/>
</td>
</tr>
<tr>
<td>
<input type="password" placeholder="Password" class="txtcontrol" name="password"/>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Log In" class="sbutton"/>
<br>New User? <a href="Register.php">Register Here</a>
</td>
</tr>
</table>
</div>
<div id="divsecurity">
<table align="center" width="60%">
<tr>
<td>
<input type="text" placeholder="Email Id" class="txtcontrol" name="username1"/>
</td>
</tr>
<tr>
<td>
<select name="ddlsecurity" class="txtcontrol">
<option value="0">Select Security Question</option>
<?php
$sql="SELECT * FROM `tbl_Security_Question`";
$qury=mysqli_query($con,$sql);
if(!$qury){
echo "No Records Found";
}
else
{
while($row=mysqli_fetch_array($qury))
{
echo "<option value=".$row["Security_Question_Id"].">".$row["Security_Question"]."</option>";
}
}
?>
</select>
</td>
</tr>
<tr>
<td>
<input type="text" placeholder="Security Answer" class="txtcontrol" name="answer"/>
</td>
</tr>
<tr>
<td>
<input type="submit" name="check" value="Log In" class="sbutton"/>
</td>
</tr>
</table>
</div>
</form>
</div>
</div>
</body>
</html>
Registration
<?php
include ("connect.php");
$msg = "";
if(isset($_POST["submit"]))
{
if($_POST["User_Name"]=='')
{
$msg = "Enter User Name...";
//return false;
}
else if($_POST["password"]=='')
{
$msg = "Enter Password...";
//return false;
}
else if($_POST["ddlsecurity"]=="0")
{
$msg = "Select Security Question...";
//return false;
}
else if($_POST["answer"]=='')
{
$msg = "Enter Answer...";
//return false;
}
else
{
$User_Name = $_POST["User_Name"];
$password = $_POST["password"];
$ddlsecurity = $_POST["ddlsecurity"];
$answer = $_POST["answer"];
$User_Name = mysqli_real_escape_string($con, $User_Name);
$ddlsecurity = mysqli_real_escape_string($con, $ddlsecurity);
$answer = mysqli_real_escape_string($con, $answer);
$password = mysqli_real_escape_string($con, $password);
//$password = md5($password);
$sql="SELECT * FROM `tbl_user_registration` WHERE User_Name='".$User_Name."'";
$result=mysqli_query($con,$sql);
if(!$result)
{
$msg = "Sorry...This email already exist...";
}
else
{
$query = mysqli_query($con, "INSERT INTO `tbl_user_registration`(`User_Name`, `Password`, `Security_Question_Id`, `Security_Answer`) VALUES ('".$User_Name."','".$password."','".$ddlsecurity."','".$answer."')");
if($query)
{
$msg = "Thank You! you are now registered.";
header("Location: login.php"); /* Redirect browser */
exit();
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Register</title>
<style>
.txtcontrol{
height: 40px;
font-size: 22px;
border: single;
border-color:black;
width: 100%;
margin-bottom:10px;
text-align:center;
}
.mcontainer {
max-width: 600px;
margin: 0 auto;
padding: 80px 0;
height: 400px;
text-align: center;
}
.mcontainer {
width: 100%;
padding-right: 10px;
padding-left: 10px;
margin-right: auto;
margin-left: auto;
}
.sbutton
{
width: 42%;
height: 42px;
background-color: #75d4b6;
border: none;
color: #f9f5ee;
font-size: 17px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="mcontainer">
<h1>Welcome</h1>
<form method="post" action="">
<?php
if($msg!='')
{
echo " <div>
<h4>Message</h4>
$msg</div>";
}
?>
<table align="center" width="60%">
<tr>
<td>
<input type="text" placeholder="User Name" class="txtcontrol" name="User_Name"/>
</td>
</tr>
<tr>
<td>
<input type="password" placeholder="Password" class="txtcontrol" name="password"/>
</td>
</tr>
<tr>
<td>
<select name="ddlsecurity" class="txtcontrol">
<option value="0">Select Security Question</option>
<?php
$sql="SELECT * FROM `tbl_Security_Question`";
$qury=mysqli_query($con,$sql);
if(!$qury){
echo "No Records Found";
}
else
{
while($row=mysqli_fetch_array($qury))
{
echo "<option value=".$row["Security_Question_Id"].">".$row["Security_Question"]."</option>";
}
}
?>
</select>
</td>
</tr>
<tr>
<td>
<input type="text" placeholder="Security Answer" class="txtcontrol" name="answer"/>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Register" class="sbutton"/>
</td>
</tr>
<tr>
<td>
Already a User? <a href="login.php">Login Here</a>
</td>
</tr>
</table>
<br>
</form>
</div>
</div>
</body>
</html>
Explanation / Answer
regiser.php:
<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Registration system PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Register</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo $username; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo $email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<button type="submit" class="btn" name="reg_user">Register</button>
</div>
<p>
Already a member? <a href="login.php">Sign in</a>
</p>
</form>
</body>
</html>
style.css file
* {
margin: 0px;
padding: 0px;
}
body {
font-size: 120%;
background: #F8F8FF;
}
.header {
width: 30%;
margin: 50px auto 0px;
color: white;
background: #5F9EA0;
text-align: center;
border: 1px solid #B0C4DE;
border-bottom: none;
border-radius: 10px 10px 0px 0px;
padding: 20px;
}
form, .content {
width: 30%;
margin: 0px auto;
padding: 20px;
border: 1px solid #B0C4DE;
background: white;
border-radius: 0px 0px 10px 10px;
}
.input-group {
margin: 10px 0px 10px 0px;
}
.input-group label {
display: block;
text-align: left;
margin: 3px;
}
.input-group input {
height: 30px;
width: 93%;
padding: 5px 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid gray;
}
.btn {
padding: 10px;
font-size: 15px;
color: white;
background: #5F9EA0;
border: none;
border-radius: 5px;
}
.error {
width: 92%;
margin: 0px auto;
padding: 10px;
border: 1px solid #a94442;
color: #a94442;
background: #f2dede;
border-radius: 5px;
text-align: left;
}
.success {
color: #3c763d;
background: #dff0d8;
border: 1px solid #3c763d;
margin-bottom: 20px;
}
server.php
<?php
session_start();
// initializing variables
$username = "";
$email = "";
$errors = array();
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'registration');
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "email already exists");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}
// ...
Login user
<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Registration system PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Login</h2>
</div>
<form method="post" action="login.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" >
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password">
</div>
<div class="input-group">
<button type="submit" class="btn" name="login_user">Login</button>
</div>
<p>
Not yet a member? <a href="register.php">Sign up</a>
</p>
</form>
</body>
</html>
login.php
// ...
// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
?>
index.php
<?php
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Home Page</h2>
</div>
<div class="content">
<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
<div class="error success" >
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>
<!-- logged in user information -->
<?php if (isset($_SESSION['username'])) : ?>
<p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>
<p> <a href="index.php?logout='1'">logout</a> </p>
<?php endif ?>
</div>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.