LOGIN-MODULE

Login Module With Session Management:

(1)First create databse & table & insert values for username & password:

Create database amrapali;

Create table user

(id int primary key auto_increment,

username varchar(20),

password varchar(20));

(1) Write html code for “Loginform.html” file , CODE IS GIVEN BELOW:

<html>

<head>

<title>

login example

</title>

</head>

<body>

<form action=”login.php” method=”post”>

username <input type=text name=”username”>

Password <input type=text name=”password”>

<input type=”submit” name=”submit” value=”submit”>

</body>

</html>

(2)write php code which will check username & password “login.php” file CODE IS GIVEN BELOW:

<?php

$con=mysql_connect(“Localhost”,”root”,””);

mysql_select_db(“amrapali”,$con);

$username=$_POST[‘username’];

$password=$_POST[‘password’];

$sql=”select username , password from user where username=’$username’ AND password=’$password'”;

$result=mysql_query($sql);

$num=mysql_num_rows($result);

if($num>0)

{

session_start();

$_SESSION[‘username’]=’$username’;

header(‘Location:welcome.php’);

}

else

{

echo “invalid login”;

}

?>

(3)AFTER LOGIN page will display that is “welcome.php”:

<?php

session_start();

if(!isset($_SESSION[‘username’]))

{

header(“Location:error.php”);

}

echo “<h1>welcome </h1>”;

echo ‘<a href=”logout.php”>Logout</a>’;

?>

(4)logout.php file this file destroy session:

<?php

session_start();

session_destroy();

header(“Location:loginform.html”);

?>

(5)error.php file:

<?php

echo “first make login “;

echo ‘<a href=”loginform.html”>Login here</a>’;

?>