Loading...

Simple PHP Login Script with Mysql

Hi, here I have simple tutorial about simple php login script with mysql. If you beginner in php, this article should be good for you to understanding session that use in authentication process. Let me show you process of this simple login with php & mysql.

guseka-simple-login-php-mysql-concept

Have a look that little concept. When user submit the form, form will go to authentication to check username and password, then if username and password match, system will redirect you to homepage or welcome page. If not it will redirect you to login form or give you a comment that your input maybe wrong. Then after that, to logout session will destroy to  clearing data. Simple right? now, let’s action. First, you need to create table on you database. In this case I use table name login.

structure

After you create it, we need 4 file php script. index.php, auth.php, welcome.php, logout.php and also login.css to styling form. Below the content of the file

index.php

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>Login</title>
<link rel=”stylesheet” href=”//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css”>
<link href=’http://fonts.googleapis.com/css?family=Raleway’ rel=’stylesheet’ type=’text/css’>
<link rel=”stylesheet” href=”login.css”>
</head>
<body>

<div class=”loginwrap”>
<div class=”formclass”>
<h1><i class=”fa fa-lock”></i> Login</h1>
<form method=”post” action=”auth.php”>
<span><label>Username</label> <input type=”text” name=”username” maxlength=”20″></span><br>
<div class=”clear”></div>
<span><label>Password</label> <input type=”password” name=”password” maxlength=”20″></span><br>
<div class=”clear”></div>
<span><input type=”submit” name=”submit” value=”Login”></span><br>
<div class=”clear”></div>
</form>
</div>
</div>

</body>
</html>

auth.php

<?php
session_start();
$uname = $_POST[‘username’];
$passwd = $_POST[‘password’];
if (empty($uname) or empty($passwd)){
echo “There is empty field. Please fill it!”;

echo “<a href=\”index.php\”>[ index ]</a>”;
}else{
$p = md5($passwd);

$con = mysql_connect(‘localhost’,’root’,’passwordmysql’) or die(‘Cannot connect to db!’);
if ($con)
mysql_select_db(“gec”,$con);

$query = mysql_query(“select * from login where username=’$uname’ and password=’$p’ limit 1″);
$row = mysql_num_rows($query);

if ($row == 0){
echo “Username or password maybe wrong. Please try again!”;

echo “<a href=\”index.php\”>[ index ]</a>”;
}else{

$r = mysql_fetch_array($query);

$_SESSION[‘allowuser’] = $r[‘username’];
$_SESSION[‘acceptedpass’] = $r[‘password’];

header(‘location:welcome.php’);

}

}
?>

welcome.php

<?php
session_start();

if(isset($_SESSION[‘allowuser’]) && isset($_SESSION[‘acceptedpass’])){
echo “HELO <b>”.$_SESSION[‘allowuser’].”</b>”;
echo “<a href=\”logout.php\”>[ Logout ]</a>”;

}else{
echo “You don’t have authorized into this page!”;
}
?>

logout.php

<?php
session_start();
session_destroy();

header(‘location:index.php’);
?>

login.css

*{
font-family: ‘Raleway’, sans-serif;
}
.clear{
clear:both;
}
body{
background: #f2f2f2;
}
.loginwrap{
background: #fff;
width: 320px;
margin:0 auto;
padding:20px;
}
.loginwrap .formclass h1{
margin:10px;
border-bottom: 1px solid #f3f3f3;
padding:5px;
}

.loginwrap .formclass span{
display: block;
}

.loginwrap .formclass span input[type=text],
.loginwrap .formclass span input[type=password]{
width: 100%;
height: 35px;
}
.loginwrap .formclass span input[type=submit]{
background: rgb(79, 218, 79);
border: none;
padding: 10px 30px;
float: right;
text-transform: uppercase;
color: #fff;
font-weight: bold;
letter-spacing: 1px;
box-shadow: 3px 3px 0px 0px #2C892C;
cursor: pointer;
transition: all 0.3s ease-in-out;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
}
.loginwrap .formclass span input[type=submit]:hover{
box-shadow: none;
transform: translate(4px,4px);
-webkit-transform: translate(4px,4px);
-o-transform: translate(4px,4px);
-ms-transform: translate(4px,4px);
}
.loginwrap .formclass span label{
width: 100%;
display: block;
}

See the code. The point is, after user submit the form system will go to auth.php. Here username and password checked. If match , php will give username and password session to go throught to welcome.php page.
In welcome.php page, session will check again to make sure user already login, after that if session already register, welcome page will view the content page. In this page there is logout link to destroy session. I add a little bit style, to styling the form login.

Ok that is my simple tutorial. Have fun!

SHARE NOW
Share

Leave a comment

Get the latest update about web development.