Sunday, September 13, 2015

Code PHP Login

1. create database name user

CREATE TABLE `tbluser` (
  `LogID` varchar(20) NOT NULL,
  `Name` varchar(50) NOT NULL,
  `Password` varchar(30) NOT NULL,
  `Email` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `tbluser` (`LogID`, `Name`, `Password`, `Email`) VALUES
('admin', 'Administrator', '123456', 'admin@yahoo.com'),
('bob', 'sourn sophois', '123456', 'sournsophois@yahoo.com'),
('dara', 'sok dara', '123', 'dara@yahoo.com'),
('sok', 'sok han', '123', 'abac@yshoo.com');

2. login.php

<?php
session_start();
if(isset($_POST['btn'])){
echo $_SESSION['isLogin'];
include('config.php');

$user=$_POST['txtUserid'];
$pwd=$_POST['txtUserpwd'];

if(strlen($user)==0 || strlen($pwd)==0){
echo"Login ID and Password are required!!";

}
else{
$query="select logID, password from tbluser where logID='$user'";
$result=mysql_query($query,$conn);

if(mysql_num_rows($result)==0){
echo"Login ID doesn't exist in system! Please register before login again, thanks!";
}
else{
$row=mysql_fetch_array($result);
if($row['password']!=$pwd){
echo"Incorrect password!! Try again";

}

else{
$_SESSION['isLogin']=true;
header('Location:registration.php');
}

}
}

}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>&nbsp;</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="frmCampaign" id="frmCampaign">
 <table align="center" width="500" border="0" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
  <tr>
   <td width="150" bgcolor="#336699"><font color="#FFFFFF"><strong>Login ID</strong></font></td>
   <td bgcolor="#FFFFFF"><input name="txtUserid" type="text" id="txtUserid"></td>
  </tr>
  <tr>
   <td width="150" bgcolor="#336699"><font color="#FFFFFF"><strong>Password</strong></font></td>
   <td bgcolor="#FFFFFF"><input name="txtUserpwd" type="password" id="txtUserpwd"></td>
  </tr>
  <tr>
   <td colspan="2" align="center" bgcolor="#FFFFFF"> <input type="submit" name="btn" value="Login">
   </td>
  </tr>
  <tr>
  <td colspan="2">Registration is required for new user:<a href="newuser.php">Register</a></td>

  </tr>
 </table>
</form>

</body>
</html>
3. user.php
<a href="logout.php">Logout</a>
<?php
session_start();
if(!isset($_SESSION['isLogin'])){
header('Location:login.php');
exit;
}

?>
<html>
<body>
Welcome to admin zoon
</body>
</html>

4. logout.php
<?php
session_start();
unset($_SESSION['isLogin']);
//session_destroy();

header("Location:Login.php");
exit;

?>

5. We can new user log by using user.php the code as below ;
<a href="logout.php">Logout</a>
<?php
session_start();
if(!isset($_SESSION['isLogin'])){
 header('Location:login.php');
 exit;
}
if(isset($_POST['add']))
{
include('config.php');
$logid=$_POST['txtlogid'];
$user=$_POST['txtname'];
$email=$_POST['txtmail'];
$pws=$_POST['psw'];
$sql = "INSERT INTO tbluser(LogID,Name,Password,Email) VALUES('$logid','$user','$pws','$email')";
mysql_select_db('registration1');
$retval = mysql_query($sql,$conn);
if(! $retval )
{
  die('Could not add data: ' . mysql_error());
}
echo "<br/>add data successfully\n<a href='select.php'>view list</a>";
mysql_close($conn);
}
else
{
?>
<html>
<body>
Welcome to admin zoon
<form method="post" action="<?php $_PHP_SELF ?>">
<table>
<tr><td>LogID</td>
<td><input type="text" name="txtlogid"></td>
</tr>
<tr><td>UserName</td>
<td><input type="text" name="txtname"></td>
</tr>
<tr><td>Email</td>
<td><input type="text" name="txtmail"></td>
</tr>
<tr><td>Password</td>
<td><input type="text" name="psw"></td>
</tr>
<tr><td><input type="submit" name="add" value="Add New User"></td>
</tr>

</table>
</form>
<?php
}
?>
</body>
</html>

No comments:

Post a Comment