Here , we make simple login page with session. I hope you know about how to write php code in php format.if you don’t know then , write between <?php …… ?> all code of php script.
We need following files fo login page with database mysql.
Connection.php,Index.php,Home.php,Logout.php,
DATABASE Databank-name : login ,Table-name : users
First we need to connection file to MySql the server and select database. So following code …
connection.php
--------------------------------------------------------------------------------------------------------------
<?php
error_reporting(0);
session_start();
mysql_connect('localhost','root','') or die(mysql_error()); // Connection path of Sql server
mysql_select_db('login') or die(mysql_error()); // Select Database from the Sql Server
?>
--------------------------------------------------------------------------------------------------------------
Above , first session_start() function is used to start the session when this connection file call . second function mysql_connect() is to connect the MySql server , in this function the first parameter is ‘localhost’ is used sever name , second parameter is username that always in localhost sever ‘root’.And This parameter is password that not must required to give , if you give then write there.and another function mysql_select_db is used to the select database from the MySQL-sever.
index.php
Following is login page designing with style and fetching data from database.
--------------------------------------------------------------------------------------------------------------
<html>
<head>
<title>Login Page</title>
</head>
<style>
#form {
width:300px;
text-align:center;
height:auto;
font-family:"Times New Roman", Times, serif;
border:2px solid #666;
border-radius:5px;
margin-top:15%;
margin-left:40%;
}
</style>
<body>
<?php
include "connection.php";
if(isset($_REQUEST['log']))
{
$user=$_REQUEST['user'];
$pass=$_REQUEST['pass'];
if($user==null || $pass==null)
{
$msg="please enter username and password ";
}
else
{
$logg="select * from `users` where username='$user' && password='$pass'"; // Query for fetch data from user table
$exe=mysql_query($logg); // Execution of Query
$row=mysql_fetch_array($exe);
$total=mysql_num_rows($exe);
if($total==1)
{
$_SESSION['user'] = $row['username'];
header("location:home.php");
}
else
{
$message = "record not exits";
}
}
}
?>
<div id="form">
<form action="" method="post" >
<center>
<table>
<font color="red">
<?php echo $msg;
echo $message;
?>
</font>
<th colspan="2">Login</th>
<tr>
<td>Username</td>
<td><input type="text" name="user" maxlength="30" placeholder="Enter Username"></td>
</tr>
<tr>
<td> Password </td>
<td><input type="password" name="pass" maxlength="50" placeholder="Enter Password"></td>
</tr>
<tr>
<th colspan="2"><input type="submit" name="log" value="Login"></th>
</tr>
</table>
</center>
</form>
</div>
</body>
</html>
--------------------------------------------------------------------------------------------------------------
Home.php
This page display after login successfully with session users .
--------------------------------------------------------------------------------------------------------------
<html>
<head>
<title>Home</title>
</head>
<body>
<?php
include "connection.php";
echo "Welcome ".$_SESSION['user'];
?>
</br>
<a href="logout.php"> Logout </a>
</body>
</html>
--------------------------------------------------------------------------------------------------------------
Logout.php
This used when user logout from the page.
--------------------------------------------------------------------------------------------------------------
<?php
session_start();
session_destroy();
unset($_SESSION["name"]);
header("location:login.php");
?>
--------------------------------------------------------------------------------------------------------------
you can download this code here..
DOWNLOAD.zip
If you like this, then share with your friends. And if you have query, contact-me and give comment.
0 comments:
Post a Comment