- 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.
If you are already following from previous article, you should already have table created. If you don’t have create the table.
2 | `id` int(11) NOT NULL AUTO_INCREMENT, |
3 | `username` varchar(255) NOT NULL, |
4 | `email` varchar(255) NOT NULL, |
5 | `password` varchar(255) NOT NULL, |
6 | `active` tinyint(1) NOT NULL, |
8 | UNIQUE KEY `username` (`username`) |
This is the form, only displayed if message variable in not set.
1 | <div class="register-form"> |
3 | if(isset($msg) & !empty($msg)){ |
8 | <form action="" method="POST"> |
9 | <p><label>User Name : </label> |
10 | <input id="username" type="text" name="username"placeholder="username" /></p> |
12 | <p><label>Password : </label> |
13 | <input id="password" type="password" name="password"placeholder="password" /></p> |
15 | <a class="btn" href="register.php">Signup</a> |
16 | <input class="btn register" type="submit" name="submit" value="Login"/> |
And the styles for the form, if you have added styles in previous article. Skip this step.
9 | -webkit-border-radius:10px; |
10 | -moz-border-radius:10px; |
13 | .register-form form input{padding: 5px;} |
14 | .register-form .btn{background: #726E6E; |
22 | .register-form .register{ |
If you are following from previous user registration article, no need to create this file. Other wise create connect.php file.
2 | $connection = mysql_connect('localhost', 'root', ''); |
4 | die("Database Connection Failed" . mysql_error()); |
6 | $select_db = mysql_select_db('test'); |
8 | die("Database Selection Failed" . mysql_error()); |
5
PHP Logic for User Login
And this is the PHP code for logging in user
3 | require('connect.php'); |
6 | if (isset($_POST['username']) and isset($_POST['password'])){ |
8 | $username = $_POST['username']; |
9 | $password = $_POST['password']; |
11 | $query = "SELECT * FROM `user` WHERE username='$username' and password='$password'"; |
13 | $result = mysql_query($query) or die(mysql_error()); |
14 | $count = mysql_num_rows($result); |
17 | $_SESSION['username'] = $username; |
20 | echo "Invalid Login Credentials."; |
24 | if (isset($_SESSION['username'])){ |
25 | $username = $_SESSION['username']; |
26 | echo "Hai " . $username . " |
28 | echo "This is the Members Area |
30 | echo "<a href='logout.php'>Logout</a>"; |
Comments
Post a Comment
Comment