Before proceeding to this tutorial, we like to confirm that this is a basic tutorial about how to signup and login in PHP.
In this tutorial, you can see we have created a form switching method to switch signup and login page.
This is the basic tutorial in which you can see login and registration form in PHP with MySQL.
Let us see in details
We have created one PHP file and a database to store user registration detail.
Database :
DB name = test
Table name= user
user.sql
Given below snippet is of user.sql just run this in sql to create a user table within database.
CREATE TABLE IF NOT EXISTS `user` ( `id` int(25) NOT NULL AUTO_INCREMENT, `name` varchar(250) NOT NULL, `email` varchar(250) NOT NULL, `password` varchar(250) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
config.php
We have created a database connection file with the name of config.php. mysql_connect is depreciated now that is why we are using mysqli_connect here as you see below.
<?php $conn = mysqli_connect(“host name”,”user name”,”password”,”DB name"); ?>
index.php
In this php page we have perform action to register and login a user after successful signup. In this php page we have created a form for login and signup process.
<h1>Login Registration System in PHP</h1> <div id="log"> <form action="#" method="POST" id="login"> <center><h4>Login Here</h4></center> <label>Email</label><input type="email" name="email" id="email"><br> <label>Password</label><input type="password" name="pass" id="name"><br> <input type="submit" name="login" value="Login"><br><br><br> </form> <a href="#" onclick="next()" >You don't have an account yet? Register Here</a> </div> <div id="regis"> <center><h4>Register</h4></center> <form action="#" method="POST" id="register"> <label>Name</label><input type="text" name="name" ><br> <label>Email</label><input type="email" name="remail" ><br> <label>Password</label><input type="password" name="password" ><br> <input type="submit" name="signup" value="Register"><br><br><br> </form> <a href="#" onclick="prev()" >You have an account already? Login Here</a> </div> <?php include 'config.php'; if(isset($_POST['signup'])) { $name= mysqli_real_escape_string($conn,$_POST['name']); $remail= mysqli_real_escape_string($conn,$_POST['remail']); $password= mysqli_real_escape_string($conn,$_POST['password']); $query = "SELECT * from `user` WHERE `email`='$remail'"; $val = mysqli_query($conn,$query); if (!filter_var($remail, FILTER_VALIDATE_EMAIL)) { echo("$remail is not a valid email address"); } elseif(mysqli_num_rows($val)>=1) { echo "Email Address already Exist"; } else { $suc="INSERT INTO `user` SET `name`='$name',`email`='$remail',`password`='".md5($password)."'"; if(mysqli_query($conn,$suc)){ echo "Registered Successfully"; } else { echo "Try Again"; } } } ?> <?php if(isset($_POST['login'])) { $email = mysqli_real_escape_string($conn,$_POST['email']); $pass = mysqli_real_escape_string($conn,$_POST['pass']); $result = mysqli_query($conn,"SELECT * FROM `user` WHERE `email`='".$email."' and `password`='".md5($pass)."'"); $logresult = mysqli_fetch_array($result); if(count($logresult)>=1) { echo "Login Successfully"; echo "HI".$result['name']; } else { echo "Email Address or password is incorrect"."<br>"; echo "try again"; } } ?>
Here during the signup process, we are checking for duplicate email and validating email of the user through inbuilt function ( !filter_var($remail, FILTER_VALIDATE_EMAIL).
CSS
#log{ display:block; width:320px; height: 345px; border-radius:5px; padding:5px; margin:10px; box-shadow:2px 2px 8px 5px grey; } input[type="text"] { width: 212px; height: 38px; padding: 2px; border-radius: 5px; margin: 7px; margin-left: 23px; } input[type="password"] { width: 212px; height: 38px; padding: 2px; border-radius: 5px; margin: 7px; margin-left: 20px; } input[type="email"] { width: 212px; height: 38px; padding: 2px; border-radius: 5px; margin: 7px; margin-left: 20px; } input[type="submit"] { width: 124px; float: right; border-radius: 15px; padding: 9px; background: cadetblue; color: #fff; font-weight: 900; border: 2px solid #ededed; margin-top:7px; } input#email { margin-left: 46px; } h4{ color:blue; } h1{ color: red; }