Dear Reader, let us start to learn about ‘Login Module creation using MySQL, PHP’.
As after Registration page creation, you need to make user logged into your software system.
For that purpose, you need to create a Login Page.
And if till now you have not created a Registration Page then please go with this ‘ Registration Module creation using MySQL, PHP ‘ first.
Now create user_login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
<?php ob_start(); session_start(); include 'db_connect.php'; // if session is set then you will be redirected to home page, not to login page if ( isset($_SESSION['user_id'])!="" ) { header("Location: home.php"); exit; } $error = false; if( isset($_POST['login-save']) ) { // prevent sql injections/ clear user invalid inputs $email = trim($_POST['email']); $email = strip_tags($email); $email = htmlspecialchars($email); $pass = trim($_POST['pass']); $pass = strip_tags($pass); $pass = htmlspecialchars($pass); // prevent sql injections / clear user invalid inputs if(empty($email)){ $error = true; $emailError = "Please enter your email address."; } else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) { $error = true; $emailError = "Please enter valid email address."; } if(empty($pass)){ $error = true; $passError = "Please enter your password."; } // if there's no error, continue to login if (!$error) { $password = md5($pass); // password encription using md5() $sql_users = "SELECT id, email, password, status FROM users WHERE email='$email' AND password='$password'"; $result_users= mysqli_query($conn,$sql_users); $row_users = mysqli_fetch_array($result_users,MYSQLI_ASSOC); $count = mysqli_num_rows($result_users); // if uname/pass correct it returns at least 1 row if( $count == 1 && $row_users['password']==$password ) { $_SESSION['user_id'] = $row_users['id']; header("Location: home.php"); } else { $errMSG = "Incorrect Credentials, Try again..."; } } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login Module creation using MySQL, PHP</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div class="container"> <div id="login-form"> <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off"> <div class="col-md-12"> <div class="form-group"> <h2 class="">Sign In.</h2> </div> <div class="form-group"> <hr /> </div> <?php if ( isset($errMSG) ) { ?> <div class="form-group"> <div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> <?php echo $errMSG; ?> </div> </div> <?php } ?> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span> <input type="email" name="email" class="form-control" placeholder="Your Email" value="<?php echo $email; ?>" maxlength="50" /> </div> <span class="text-danger"><?php echo $emailError; ?></span> </div> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span> <input type="password" name="pass" class="form-control" placeholder="Your Password" maxlength="21" /> </div> <span class="text-danger"><?php echo $passError; ?></span> </div> <div class="form-group"> <hr /> </div> <div class="form-group"> <button type="submit" class="btn btn-block btn-primary" name="login-save">Sign In</button> </div> <div class="form-group"> <hr /> </div> <div class="form-group"> Not a Member ? <a href="registration.php">Sign Up</a> </div> </div> </form> </div> </div> </body> </html> <?php ob_end_flush(); ?> |
Now create a home page, where the user will be redirected to log into your system.
So next home.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?php ob_start(); session_start(); include 'db_connect.php'; // User will be redirected to login page, when session is not set if( !isset($_SESSION['user_id']) ) { header("Location: user_login.php"); exit; } // select loggedin users detail $sql_users = "SELECT * FROM users WHERE id=".$_SESSION['user_id']; $result_users= mysqli_query($conn,$sql_users); $row_users = mysqli_fetch_array($result_users); ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome - <?php echo $row_users['email']; ?></title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <nav class="navbar navbar-default navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">WebSite Name</a> </div> <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="#">Back to Article</a></li> <li><a href="#">WordPress</a></li> <li><a href="#">PHP</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> <span class="glyphicon glyphicon-user"></span> Hi ! <?php echo $row_users['email']; ?> <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="user_logout.php?action=logout"><span class="glyphicon glyphicon-log-out"></span> Sign Out</a></li> </ul> </li> </ul> </div><!--/.nav-collapse --> </div> </nav> <div id="wrapper"> <div class="container"> <div class="page-header"> <h3>WebSite Name</h3> </div> <div class="row"> <div class="col-lg-12"> <h1>Hello, Hi, How are you ?</h1> </div> </div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html> <?php ob_end_flush(); ?> |
Now if a user wants to be logged out from the system, then he needs to click on the drop-down ‘Sign Out‘ link, and he will automatically will be redirected to a login page. As the whole user’s logged in session has been removed.
Congratulations you have successfully Completed Login Module creation using MySQL, PHP. If this post was helpful for you then please share this post on social media and if you have any query then please contact us or comment below and don’t forget us follow and like on Social Media, Thanks