Steps to create Simple Login Script in PHP and mysql
- First step we will connect to the database.
- Then we will select the database.
- We are checking, if the form is submitted or not. In this step we have two logic’s.
- What if the form is submitted.
- If the form is submitted, we are assigning the posted values to variables and checking the values are existing in the database or not.
- If the values submitted in the form and the values in the database are equal, then we will create a session for the user.
- If the values are not equal then it will display an error message.
- And then we checks for the session, if the session exists we will great him with the username, otherwise the form will be displayed.
- What if not the form is submitted.
- When the user comes first time, he will be displayed with a simple form.
- User Name, Password and a submit button.
1
Create a table
If you are already following from previous article, you should already have table created. If you don’t have create the table.
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
)
2Creating HTML Form
This is the form, only displayed if message variable in not set.<div class="register-form"> <?php if(isset($msg) & !empty($msg)){ echo $msg; } ?> <h1>Login</h1> <form action="register.php" method="POST"> <p><label>User Name : </label> <input id="username" type="text" name="username" placeholder="username" /></p> <p><label>Password : </label> <input id="password" type="password" name="password" placeholder="password" /></p> <a class="btn" href="register.php">Signup</a> <input class="btn register" type="submit" name="submit" value="Login" /> </form> </div> <?php } ?>3Adding styles to form
And the styles for the form, if you have added styles in previous article. Skip this step.<style>.register-form{ width: 500px; margin: 0 auto; text-align: center; padding: 10px; color: #fff; background : #c4c4c4; border-radius: 10px; -webkit-border-radius:10px; -moz-border-radius:10px; } .register-form form input{padding: 5px;} .register-form .btn{background: #726E6E; padding: 7px; border-radius: 5px; text-decoration: none; width: 50px; display: inline-block; color: #FFF;} .register-form .register{ border: 0; width: 60px; padding: 8px; }</style>4Connect to Database
If you are following from previous user registration article, no need to create this file. Other wise create connect.php file.<?php $connection = mysql_connect('localhost', 'root', ''); if (!$connection){ die("Database Connection Failed" . mysql_error()); } $select_db = mysql_select_db('test'); if (!$select_db){ die("Database Selection Failed" . mysql_error()); }?>
5
PHP Logic for User Login
And this is the PHP code for logging in user
<?php //Start the Session
session_start();
require('connect.php');
//3. If the form is submitted or not.
//3.1 If the form is submitted
if (isset($_POST['username']) and isset($_POST['password'])){
//3.1.1 Assigning posted values to variables.
$username = $_POST['username'];
$password = $_POST['password'];
//3.1.2 Checking the values are existing in the database or not
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
//3.1.2 If the posted values are equal to the database values, then session will be created for the user.
if ($count == 1){
$_SESSION['username'] = $username;
}else{
//3.1.3 If the login credentials doesn't match, he will be shown with an error message.
echo "Invalid Login Credentials.";
}
}
//3.1.4 if the user is logged in Greets the user with message
if (isset($_SESSION['username'])){
$username = $_SESSION['username'];
echo "Hai " . $username . "
";
echo "This is the Members Area
";
echo "<a href='logout.php'>Logout</a>";
}else{
//3.2 When the user visits the page first time, simple login form will be displayed.
?>








0 comments:
Post a Comment