how to make secure login in php with sessions – PHP script code to create your own admin login form and pages with sessions
As a web Developer you are desired to develop high secure as well as good looking site. When ever we talk about designing, it is not a big issue. It can be easily done just by little bit hard work. the main thing is its security and efficiency. In this article we will learn how we to make secure login in PHP with sessions. if you are making any project and you are required to make admin panel or any type of login panel for users. Then you can’t play with user security as well as personal details.
So, here we will learn how we can create Login form in php, HTML with sessions. you can also able to download or copy the entire code and use it into your own projects :) .
Soon i will make a tutorial on how to make blogging CMS like, blogspot or wordpress in PHP. i hope you will wait for it keenly. feel free to subscribe us in order to get touched with us. you might also like to read how to Create Dynamic Website in PHP MySQL with admin Panel.
Make Secure Login in PHP, HTML (Full Script) :
Before sharing code first lets me elaborate what are sessions and why we need to use them? Sessions are the time period data storage which are use to save data for a specific time or till any event. You may notice many time whenever you are login in facebook or gmail. If your window or PC is shut down accidentally or by electricity breakdown. when yo try to access that page again then it will ask you to login again in order to proceed. that’s are sessions and on the back-end there are code to create and delete as well as destroy sessions. Learn How to create a simple website in PHP tutorial class 1.
Pages we will need to make for Secure Login panel in PHP :
- Index.php (login form is code here).
- login.php (having session details as well as establishing server and DB connection).
- profile.php (page where user will redirect after successfully login).
- Log out (page to destroy sessions as well as log out user and stop accessed to profile.php).
Login form with sessions and admin panel script in PHP :
- Login.php
<?php
session_start(); // Punction for Starting Session
$error=”; // will Store Error Message
if (isset($_POST[‘submit’])) {
if (empty($_POST[‘username’]) || empty($_POST[‘password’])) {
$error = “Username or Password is invalid”;
}
else
{
// Define $username and $password
$username=$_POST[‘username’];
$password=$_POST[‘password’];
// Establishing Connection with server as well as detabase
$connection = mysql_connect(“localhost”, “root”, “”);
// To protect from MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// Selecting Database
$db = mysql_select_db(“bcms”, $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query(“select * from userss where password=’$password’ AND username=’$username'”, $connection);
if (mysql_num_rows($query)==1) {
$_SESSION[‘login_user’]=$username; // Initializing Session (storing username)
header(“location: profile.php”); // Redirecting To Other Page
} else {
$error = “Username or Password is invalid”;
}
mysql_close($connection); // Closing Connection
}
}
?>
Don’t missed to read how to create menu in php Dynamically – php navigation.
Don’t forget to change database and server info with yours.
- Index.php
<?php
include(‘login.php’); // Includes Login Scriptif(isset($_SESSION[‘login_user’])){
header(“location: profile.php”);
}
?>
<html>
<head>
<title>Login Blogging CMS</title>
<link href=”style.css” rel=”stylesheet” type=”text/css”>
</head>
<body bgcolor=”#F1F1F1″>
<div id=”divlement”><div id=”main”>
<div id=”login”>
<h2>Login to Blogging CMS</h2>
<form action=”” method=”post”>
<br><br>
<label>UserName :</label>
<br><br>
<input id=”name” name=”username” placeholder=”username” type=”text”>
<br><br>
<label>Password :</label>
<br><br>
<input id=”password” name=”password” placeholder=”**********” type=”password”>
<br><br>
<input name=”submit” type=”submit” value=” Login “>
<span><?php echo $error; ?></span>
</form>
</div>
</div>
</div>
</body>
</html>
</html>
This is the login form from end of login form for user. you might also interested to learn how to create pages dynamically in php without codding.
- Logout.php
<?php
session_start();
if(session_destroy())
{
header(“Location: index.php”);
}
?>
when ever user click on logout link which is directed to logout.php then all sessions will be destroy and user will redirected to login form again (index.php)
- Profile.php
<?php
include(‘session.php’);
include(‘includes/db.php’);
?>
you just need to add the above code in profile.php or any other file which you want to only be accessed after login successfully.
- Style Sheet file :
Now you are done bellow is the css file which i am using in this form. you can download it from bellow.
Download Style.CSS for login script
Speak your mind :
So, this is my tutorial s well as code of how to make secure login in PHP with sessions. i hop you like it, feel free to share it. If you have question “DON’T BE SHY” ask it freely we are here to insist you in all regards. you may also like to read how to create simple php forum or Discussion Board.