Login page in php web application is created using sessions. Session array is used for storing user data on server side.Here is session array
$_SESSION.
Here if form for php login page.login_view.php
 <h1>Login Now</h1>

<form method=”post” id=”moduleDetail”
action=”signin_view.php<?php echo ($isEditing ? “?id=$userId” : “”); ?>”>
<div>
<span id=”validation_msg” style=”color:red”></span>
<label for=”user_name”>User Name</label>
<input id=”user_name” name=”user_name” type=”text” value=”<?php echo $userDetail[‘user_name’]; ?>”/>
<span id=”user_nameInfo”></span>
</div>
<div>
<label for=”password”>Password</label>
<input type=”password” id=”password” name=”password” value=””/>
<span id=”passwordInfo”></span>
</div>
<input id=”add” name=”login” type=”button” value=”Login” onclick=”validateLogin();”/>
</form>
From is validated and submited to server for validation of user.
Here is code for server side validation for user
if(isset($_POST[‘user_name’])){
//form submitted
//$user_name = “user_name ='”.$_POST[‘user_name’].”‘ AND “;
//$password = “password = ‘”.$_POST[‘password’].”‘”;

$arrUser = array(
“user_name”=>$_POST[‘user_name’],
“password”=>$_POST[‘password’],
);
//insert employee
$result = $signUpDal->validateLogin($_POST[‘user_name’],$_POST[‘password’]);
if($result==1)
{
$_SESSION[‘user_name’]=$_POST[‘user_name’];
}   

echo $result;
}
ValidateLogin function code is here
 public function validateLogin($user_name, $password)
{
$qry = “SELECT u.user_id,u.user_name,u.password, u.email, u.role FROM users u WHERE user_name= ‘”.$user_name.”‘ AND password = ‘”.$password.”‘”; //u.user_id=$userId”;
//foreach($param as $key=>$val){
//    $qry = $qry.$val;
//}
$res = $this->conn->executeQuery($qry);

$users = array();
if (($row = mysql_fetch_assoc($res))) {
$users = $row;
}
mysql_free_result($res);
if($users[‘user_name’] == $user_name && $users[‘password’] == $password)
{
$result= 1;
}
else {
$result = 0;
}
return $result;
}

If the result is 1 then this user is stored to session
Above code that is red colored is to store validated user in session.
$_SESSION[‘user_name’]=$_POST[‘user_name’]; // storing user to session.

Leave a Reply

Your email address will not be published. Required fields are marked *