Everybody should see the login pages in most of websites. You should have doubt about this login form, how they validate username and password. There is nothing, just only 2 steps as follows as for login validation using php.
1. You enter username and password already registered which is stored in mysql database.
2. After you submit a button to login, it check the database values whether the username and password is correct or not using php and mysql query. The mysql query for select a values from mysql database as like this:
ie, SELECT * FROM table_name WHERE name='username' and password='password'
If it return values correctly, then you can login. Otherwise it give error message.
Lets see how to validate the username and password using php.
3. The php script for login validation as follows as:
login.php:
Where,
if(isset($_POST['submit']){} means run after submit a results.
if($name!=''&&$pwd!=''){} means if either username or password is empty, it give message like as 'Enter both username and password'.
mysql_query("select * from login where name='".$name."' and password='".$pwd."' ") select the values if the username and password is present.
$res=mysql_fetch_row($query) return row as numeric array.
You don't know about mysql_fetch_row, then visit mysql-fetch-row in mysql.
if($res){} validate the result. If it is correct, then your name is stored in session variable and also you'll navigate to 'welcome.php' page. Otherwise you'll get 'You entered username or password is incorrect' message. You want to visit Session in php.
When you run the above file, it should be like this:
The php script for welcome page:
welcome.php:
Now you enter username and password 'guru' and 'guru', you will navigate to 'welcome.php' page and get output like this:
welcome: guru
Signout
If it is wrong, then you can't login.
When you click the signout.php, it destroy the session and return back to login page. The php script for signout page:
signout.php:
session_destroy is used to destroy the session variable in php.
Related Post:
Create Registration form using PHP and MySQL
Login form validation with remember function using php and mysql
Forgot password to mail in login form validation using PHP and Mysql
1. You enter username and password already registered which is stored in mysql database.
2. After you submit a button to login, it check the database values whether the username and password is correct or not using php and mysql query. The mysql query for select a values from mysql database as like this:
ie, SELECT * FROM table_name WHERE name='username' and password='password'
If it return values correctly, then you can login. Otherwise it give error message.
Simple login validation using php and mysql:
Lets see how to validate the username and password using php.
1. First you create table using mysql query like this:
CREATE TABLE login(name varchar(30), password varchar(30), PRIMARY KEY(name))
Now the table 'login' is created. The field 'name' is primary key. Because the username must be unique.
2. Then insert values into table using mysql query like this:
INSERT INTO login VALUES('guru','guru')
Now the table look like this:
3. The php script for login validation as follows as:
login.php:
<html>
<head>
<style type="text/css">
input{
border:1px solid olive;
border-radius:5px;
}
h1{
color:darkgreen;
font-size:22px;
text-align:center;
}
</style>
</head>
<body>
<h1>Login<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>User name:</td><td><input type='text' name='name'/></td></tr>
<tr><td>Password:</td><td><input type='password' name='pwd'/></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>
</form>
<?php
session_start();
if(isset($_POST['submit']))
{
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$name=$_POST['name'];
$pwd=$_POST['pwd'];
if($name!=''&&$pwd!='')
{
$query=mysql_query("select * from login where name='".$name."' and password='".$pwd."'") or die(mysql_error());
$res=mysql_fetch_row($query);
if($res)
{
$_SESSION['name']=$name;
header('location:welcome.php');
}
else
{
echo'You entered username or password is incorrect';
}
}
else
{
echo'Enter both username and password';
}
}
?>
</body>
</html>
<head>
<style type="text/css">
input{
border:1px solid olive;
border-radius:5px;
}
h1{
color:darkgreen;
font-size:22px;
text-align:center;
}
</style>
</head>
<body>
<h1>Login<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>User name:</td><td><input type='text' name='name'/></td></tr>
<tr><td>Password:</td><td><input type='password' name='pwd'/></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>
</form>
<?php
session_start();
if(isset($_POST['submit']))
{
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$name=$_POST['name'];
$pwd=$_POST['pwd'];
if($name!=''&&$pwd!='')
{
$query=mysql_query("select * from login where name='".$name."' and password='".$pwd."'") or die(mysql_error());
$res=mysql_fetch_row($query);
if($res)
{
$_SESSION['name']=$name;
header('location:welcome.php');
}
else
{
echo'You entered username or password is incorrect';
}
}
else
{
echo'Enter both username and password';
}
}
?>
</body>
</html>
Where,
if(isset($_POST['submit']){} means run after submit a results.
if($name!=''&&$pwd!=''){} means if either username or password is empty, it give message like as 'Enter both username and password'.
mysql_query("select * from login where name='".$name."' and password='".$pwd."' ") select the values if the username and password is present.
$res=mysql_fetch_row($query) return row as numeric array.
You don't know about mysql_fetch_row, then visit mysql-fetch-row in mysql.
if($res){} validate the result. If it is correct, then your name is stored in session variable and also you'll navigate to 'welcome.php' page. Otherwise you'll get 'You entered username or password is incorrect' message. You want to visit Session in php.
When you run the above file, it should be like this:
The php script for welcome page:
welcome.php:
<?php
session_start();
$name=$_SESSION['name'];
echo'welcome :'. $name.'<br>';
echo'<a href="signout.php">Signout</a>';
?>
session_start();
$name=$_SESSION['name'];
echo'welcome :'. $name.'<br>';
echo'<a href="signout.php">Signout</a>';
?>
welcome: guru
Signout
If it is wrong, then you can't login.
Signout in php:
When you click the signout.php, it destroy the session and return back to login page. The php script for signout page:
signout.php:
<?php
session_start();
session_destroy();
header('location:login.php');
?>
session_start();
session_destroy();
header('location:login.php');
?>
session_destroy is used to destroy the session variable in php.
Related Post:
Create Registration form using PHP and MySQL
Login form validation with remember function using php and mysql
Forgot password to mail in login form validation using PHP and Mysql
how to define database and table ? thanks
ReplyDeleteTo know about how to create database, table and resgsitration form, visit http://www.phponwebsites.com/2014/07/php-mysql-create-registration-sign-up-form.html
DeleteHello, your coding is great but i got this error "Notice: Undefined variable: name in C:\xampp\htdocs\testThis\welcome.php on line 4" when i tried to run the program
Deleteyou need to use error_reporting('E_ALL ^ E_NOTICE');
Deletetop of your welcome.php page
Thank you for your reply, i added it just now and the other problem appear.. the $name cannot be called. the output is just "welcome"
DeleteSorry,
DeleteYou need to use $name = $_SESSION['name']; at welcome.php
I changed now. Please check it.
it fixed now.. thank you so much ^^
Deletehello guru, do u mind if i ask u a favor? i am doing my final project for my last semester and i need some help..
ReplyDeletehow can i help you?
Deletethanks alot, its working fine
ReplyDeleteWhen i hit submit button ,it is showing php code on other tab .wat to do now? pls help me
ReplyDeleteThe problem is the format of your welcome.php page may be false. Please paste your code within "" tag.
DeleteUnknown column 'password' in 'where clause' why does it keeps saying this
ReplyDeletePlease check your table structure whether your table contains "password" column
ReplyDeletethank u sir.........
ReplyDeleteThanks guru ji, ur tutorial is realy held full for me ,
ReplyDeleteI have a question, i m trying to solve it from 3 days but i m failed
,,
how to show all colums of one user from table
welcome: guru
hence , Now i need to give information of user in same manner
welcome: guru
first_name:
last_name :
etc
please solve my problem thanks
as u have posted Name ,
select * from user where name="username"
DeleteWarning: Cannot modify header information - headers already sent by (output started at /Users/J/Sites/login.php:2) in /Users/J/Sites/login.php on line 44
ReplyDeletei get this error what shld i do
Then you will try to use below code instead of " header('location:welcome.php');"
ReplyDeleteThe php code is:
echo "<script> window.location.href='welcome.php'; </script>";
Parse error: syntax error, unexpected '"' in C:\xampp\htdocs\login.php on line 32
Deletei want to redirect the page if they have entered wrong uid
ReplyDeleteYou need to write redirect code instead of displayed message "You entered username or password is incorrect"
Deletehello i did the code
ReplyDelete';
echo'Signout';
?>
but i only get welcome:
with no name.. how do i fix this?
How to debug this warning
ReplyDeleteWarning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\mypage\login.php:25) in C:\xampp\htdocs\mypage\login.php on line 26
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\mypage\login.php:25) in C:\xampp\htdocs\mypage\login.php on line 40
Guru ji how to create publish / unpublish function in core php
ReplyDeleteon click
ReplyDeleteplz help me
like that wordpress / joomla publish / unpublish function.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletethank you boss.. it's helpful
ReplyDeletethanku guruji..awsome codefor me
ReplyDeletehow do u include maps in your code?
ReplyDeleteFirst Download map iframe code and put into your php code and then run
Deletevery helpful this code, how can I add regex, for exple minimum 8 char, at least 2 digit and one of special (@, $, &, _, #)?
ReplyDeleteHi guru,i am doing login page for particular user can access particular thing only,for example Region A,B,C an every user belongs to particular region (from id 100-103 A region),(104 to 106 B region) if suppose B region id(105) enter into A region it will show invalid credential give some idea
ReplyDeletehow to upload more then 5MB size pdf fileupload And store data base in pdf file location in php ??pls helpin vaildate replay
ReplyDeletePlease refer this: http://www.phponwebsites.com/2014/10/php-mysql-upload-images-to-database.html
Deletei try to run your code but their is error at line 29(mysql_connect('localhost','root','') or die(mysql_error());) say not defind how can i fix it
ReplyDeletethank you!
That code for connect your database.
Deletei try to run your code but their is error at line 29(mysql_connect('localhost','root','') or die(mysql_error());) say not defind how can i fix it
ReplyDeletethank you!
This is the database connection part. Please verify your db connections.
Deletecan you help me?
ReplyDeletehi mr guru.plz mr help me when i hit login btn i get the error sms like this.how to resolve this problem.
ReplyDelete"You entered username or password is incorrect"
I think, you've entered wrong username and password.
DeleteThis comment has been removed by the author.
ReplyDeleteSHALL i have to changea root with my databse address while connecting data in this code tomysql_connect('localhost','root','') or die(mysql_error());
ReplyDelete
ReplyDeleteWarning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\xampp\htdocs\displaydata\d12.php:25) in C:\xampp\xampp\htdocs\displaydata\d12.php on line 26
hi
ReplyDeletei have one small doubt can you please solve it
Notice: Undefined index: name in G:\Xampp\htdocs\login_vis\welcome.php on line 3
ReplyDeletewelcome :
Signout
error_reporting('E_ALL ^ E_NOTICE');
Deleteon top of welcome.php code
';
ReplyDeleteecho'Signout';
?>
please have a look into this once. i am getting this issue
session is not working!!!
ReplyDeleteplzzzzz help
i found this error how can i resolve it...
ReplyDeleteParse error: syntax error, unexpected '$res' (T_VARIABLE) in C:\xampp\htdocs\salt1\code1.php on line 8
Awesome thanks for your post. I did an iteration of this before but PHP deprecated a function that I used Anyway when I destroy the session I can simply hit back and can access the welcome.php page. How do you protect against that?
ReplyDeleteThank you. found a good tutorial after a lot of searching!
ReplyDeletewhen i enter a user name and password it shows fllowing:Access denied for user 'root'@'localhost' (using password: NO)
ReplyDeletei have given correct root password so what should i do
There is a problem on your mysql connection
Delete@guru : am not able to link to welcome.php page
ReplyDeleteSuch a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. SKYWESTONLINE login
ReplyDelete