Expertwebadvisor

  • Magento 2
  • PHP
  • WordPress
  • MySQL
  • SEO
  • Java Script
  • Others
  • Top 10’s

Login module script In PHP

By Expertwebadvisor | Last Updated: July 7, 2013 | In: PHP


Suppose you want to develop a login module in PHP,login module script in PHP is very important. In this post I am trying to describe how to create a login module in PHP and mysql. It is very easy you can develop it through the steps given below
Login module script In PHP

Step 1. We have to create 4 php files for testing our code.
1.1. index.php
1.2. login_check.php
1.3. successful.php
1.4. logout.php

Step 2. Now I am going to create the database table named as “user_data” in to the database “ewa_testing_environment”.

Don’t know how to create database, click here

Below is the process how to create the database table

login1

CREATE TABLE `user_data` (
`id` int(1) NOT NULL auto_increment,
`userId` varchar(200) NOT NULL default '',
`user_password` varchar(200) NOT NULL default '',
`user_email` varchar(200) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1; 
-- 
-- Dumping data for table `user_data`
--
INSERT INTO `user_data` VALUES (1, 'expertwebadvisor', 'ewa@test','info@expertwebadvisor.com');

Step 3. Now create the index.php file and insert the code below

<table width="350" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000">
<tr>
	<form name="login" method="post" action="login_check.php">
		<td>
			<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#fff">
					<tr>
						<td colspan="3"><strong>User Login </strong></td>
					</tr>
					<tr>
						<td>Username</td>
						<td>:</td>
						<td><input name="username" type="text" id="username"></td>
					</tr>
					<tr>
						<td>Password</td>
						<td>:</td>
						<td><input name="pass" type="text" id="pass"></td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td><input type="submit" name="Submit" value="Submit"></td>
					</tr>
			</table>
		</td>
	</form>
</tr>
</table>

It will look like as the Screenshot given below:

login2

Step 4. When a user submit their credentials i.e. username and password, then it will redirects it on the login_check.php page because I have putted the login_check.php into the form action and on this file there will be the code to check that this user enters the valid information and it is exist in our database or not. Whole code of the login_check.php is given below:

<?php
	$host_name="localhost"; // Hostname 
	$user_name=""; // Mysql Username 
	$password=""; // Mysql Password 
	$dbname="ewa_testing_environment"; // Database name 

	mysql_connect("$host_name", "$user_name", "$password")or die("cannot connect"); 
	mysql_select_db("$dbname")or die("cannot select DB");

	// Fetching the username and password sent from form 
	$username=$_POST['username']; 
	$password=$_POST['pass']; 

	$username = stripslashes($username);
	$password = stripslashes($password);
	$username = mysql_real_escape_string($username);
	$password = mysql_real_escape_string($password);
	$query="SELECT * FROM user_data WHERE userId ='$username' and user_password ='$password'";
	$result=mysql_query($query);

	$count=mysql_num_rows($result);

	// If result matched table row must be 1;
	if($count==1){
		session_register("username");
		session_register("password"); 
		header("location:successful.php");
	}
	else {
		echo "Please check User Name/Password is wrong";
	}
?>

Step 6. If provided User Name, Password is write then it will redirect user on the successful.php page other wise it will be on the same page.

<?php
		session_start();
		if(!session_is_registered(username)){
		header("location:index.php");
	} else {
		echo "Welcome On Admin Panel";
	}
?>

Step 7. Now you have successfully Logged In. Now here is the process to create the Logout link

Logout

and the code of the logout.php file is given below:

<?php
	session_start();
	unset($_SESSION["username"]);
	header("location: index.php");
?>

You have done with the Login and Logout in PHP, Enjoy the PHP coding.

Tags:Login Module In Core PHP,Login script in PHP,PHP,PHP login module


Related Posts

  • PHP array_​count_​values Function PHP array_​count_​values Function
  • Create a copy of a Database by using the phpMyAdmin Create a copy of a Database by using the phpMyAdmin
  • Create Database, Tables by using the phpMyAdmin Create Database, Tables by using the phpMyAdmin
  • Remove Zero From Array In PHP Remove Zero From Array In PHP
  • Be Social, Share Us Here !!

  • Looking For Something !!

  • Popular Categories !!

    • Magento 2 Guide and Tutorials
    • Home Loan EMI Calculator
    • Car Loan EMI Calculator
    • Personal Loan EMI Calculator
    • Magento Guide & Tutorials
    • PHP Tutorials
    • WordPress Tutorials
    • SEO Tutorials
    • MySQL Guide
    • Opencart Tutorials
    • Linux/Ubuntu Guide
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Facebook
  • Twitter
  • DMCA.com Protection Status

Recent Posts

  • PHP array_​diff_​key Function
  • PHP array_​diff_​assoc Function
  • PHP array_​diff Function
  • PHP array_​count_​values Function
  • PHP array_​combine Function

Welcome to my Blog

Thanks for visiting my Blog, I have started this blog before 8 years ago and sharing my own experience, problem and solutions while working on the different type of projects. Enjoy the reading and don't forget to Facebook likes, Tweets or G+. You can also write me expertwebadvisor@gmail.com
© 2013 - 2025 Powered By
Top