How to Create Multi User Login Form in PHP using MySQL Database?
Hello guyz, if you are looking for the tutorial that shows how to create a multi user login form in PHP using MySQL database and redirect to different pages based on user role, then you are in the right tutorial.
In the process of developing a webapps that is targeting for multiuser that they can login into the webapps, you need to create different user login in your webapps and must define different role of each end user and then redirect to the different pages when they login to the webapps by using their associated username and password. When each end user login to the their account in that webapps they must need to access only their web pages. The perfect example is facebook. So, this tutorial is all about this.
Steps:
NOTE: I am considering that you have basic knowledge of PHP. If you are new to PHP, its better to read this tutorials 🙂
[sociallocker]
1. If you guyz are working with the PHP for the very first time, then you should know that PHP is server side scripting language, so it executes only in the server. if you create PHP file in your computer and try to run in any browser, there is no output. So working with PHP is far more Different than working in the plain HTML. That’s why you need to install server application in your computer like wamp server, xampp server etc for window.
Download wamp server from here: CLICK HERE
Now you should have text editing software in your PC, every window based PC have Notepad but i am using Dreamweaver CC. After downloading and installing these two software(Dreamweaver and wamp server) you need to define site and testing server in Dreamweaver cc so that every time when you try to execute PHP file, you do not need to save your PHP file by browsing the location of the server. if you don’t know how to do this. Read this article: CLICK HERE
2. After installing wamp server application, Start your wampserver, then open your localhost, for this open up your any browsing software and type localhost or 127.0.0.1 in the address bar. Then new page will appear then scroll down and find phpmyadmin and click on that. Then you will be able to see the login page. Login in by entering username and password (Default username is root and password is empty).
And then click on new and create one database having any name you want and the click OK, then create table having three field in and click on go. After that, you need to insert the field name like: username, password and usertype. and then select the length (like 50, 30, 100 etc) as well as data type (recommend varchar) as your requirement and then click on save. OR You can watch the video for this designing part, you may understand better in video. some screenshot of creating database are:
NOTE: we have to insert username, password and usertype in our database. So, for this, Click on insert tab and insert username, password and usertype as:
3. After completing this task, write the following piece of code and save it in .php extension.
<!doctype html> session_start(); <?php $servername="localhost"; $username="root"; $password=""; $dbname="multiuserlogin"; $conn = mysqli_connect($servername, $username, $password, $dbname); echo("conneciton"); if(isset($_POST['Login'])){ $user=$_POST['user']; $pass = $_POST['pass']; $usertype=$_POST['usertype']; $query = "SELECT * FROM `multiuserlogin` WHERE username='".$user."' and password = '".$pass."' and usertype='".$usertype."'"; $result = mysqli_query($conn, $query); if($result){ while($row=mysqli_fetch_array($result)){ echo'<script type="text/javascript">alert("you are login successfully and you are logined as ' .$row['usertype'].'")</script>'; } if($usertype=="admin"){ ?> <script type="text/javascript"> window.location.href="admin.php"</script> <?php }else{ ?> <script type="text/javascript"> window.location.href="user.php"</script> <?php } }else{ echo 'no result'; } } $_SESSION['login'] = true; ?> <html> <head> <meta charset="utf-8"> <title>Multi user login system</title> </head> <body> <form method="post"> <table> <tr> <td>Username:<input type="text" name="user" placeholder="enter your username"</td> </tr> <tr> <td>Password:<input type="text" name="pass" placeholder="enter your password"</td> </tr> <tr> <td> Select user type: <select name="usertype"> <option value="admin">admin</option> <option value="user">user</option> </select> </td> </tr> <tr> <td><input type="submit" name="Login" value="Login"></td> </tr> </table> </form> </body> </html>
4. Open this .php file in any browser you want. 🙂 That’s it guyz. if you have any confusion you can comment your confusion in the following comment section or watch the following
[/sociallocker]
videos: