Академический Документы
Профессиональный Документы
Культура Документы
org/simple-login-script-php-and-mysql-64/
Home → PHP → 5 Steps to Create Simple Secure Login Script in PHP and MySql
Here you will learn about Simple Login System & also an advanced system. Simple Login
Script System is targetted towards beginners and helps you to learn the script easily.
Advanced Login Script System is more secure than the simple Login Script and it is targetted
towards intermediate, advanced users even beginners also can easily understand it. I’ve also
created video tutorials for both the Login Systems. And also I’ve created a video course, I
highly encourage you to join my course. So that, you will learn more advanced topics in user
login & registration system.
Subscribe on Youtube
Vivek Vengala
3 <div class="input-group">
4 <span class="input-group-addon" id="basic-addon1">@</span>
5 <input type="text" name="username" class="form-control" placeholder="Username" required
6 </div>
7 <label for="inputPassword" class="sr-only">Password</label>
8 <input type="password" name="password" id="inputPassword" class="form-control" placeholder
9 <button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
10 <a class="btn btn-lg btn-primary btn-block" href="register.php">Register</a>
11 </form>
1 body {
2 padding-top: 40px;
3 padding-bottom: 40px;
4 background-color: #eee;
5 }
6
7 .form-signin {
8 max-width: 330px;
9 padding: 15px;
10 margin: 0 auto;
11 }
12 .form-signin .form-signin-heading,
13 .form-signin .checkbox {
14 margin-bottom: 10px;
15 }
16 .form-signin .checkbox {
17 font-weight: normal;
18 }
19 .form-signin .form-control {
20 position: relative;
21 height: auto;
22 -webkit-box-sizing: border-box;
23 -moz-box-sizing: border-box;
24 box-sizing: border-box;
25 padding: 10px;
26 font-size: 16px;
27 }
28 .form-signin .form-control:focus {
29 z-index: 2;
30 }
31 .form-signin input[type="email"] {
32 margin-bottom: -1px;
33 border-bottom-right-radius: 0;
34 border-bottom-left-radius: 0;
35 }
36 .form-signin input[type="password"] {
37 margin-bottom: 10px;
38 border-top-left-radius: 0;
39 border-top-right-radius: 0;
40 }
4. Connect to Database
If you are following from previous user registration article, no need to create this �le. Other
wise create connect.php �le.
1 <?php
2 $connection = mysqli_connect('localhost', 'root', 'Rvm@i[9)0?~=');
3 if (!$connection){
4 die("Database Connection Failed" . mysqli_error($connection));
5 }
6 $select_db = mysqli_select_db($connection, 'test');
7 if (!$select_db){
8 die("Database Selection Failed" . mysqli_error($connection));
9 }
32 }else{
33 //3.2 When the user visits the page first time, simple login form will be displayed.
34 ?>
Logout.php
1 <?php
2 session_start();
3 session_destroy();
4 header('Location: login.php');
5 ?>
46
47 <!-- Latest compiled and minified JavaScript -->
48 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
49 </head>
50 <body>
51
52 <div class="container">
53 <form class="form-signin" method="POST">
54 <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg
55 <h2 class="form-signin-heading">Please Login</h2>
56 <div class="input-group">
57 <span class="input-group-addon" id="basic-addon1">@</span>
58 <input type="text" name="username" class="form-control" placeholder="Username" required
59 </div>
60 <label for="inputPassword" class="sr-only">Password</label>
61 <input type="password" name="password" id="inputPassword" class="form-control" placeholder
62 <button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
63 <a class="btn btn-lg btn-primary btn-block" href="register.php">Register</a>
64 </form>
65 </div>
66
67 </body>
68
69 </html>
70 <?php } ?>
If you want to learn other things you can read other posts – registration, login, user login
with activation check, and sending forgotten password by email, Check Username
Availability .
Here in this advanced login system, you will learn a lot of advanced things and some of the
features are below.
In this script we will start by checking the session, if the user already logged in he will be
redirected to index.php page that is members area. After the form submission, we check
post superglobal isset and not empty. In this code, I’m using DB connection from above
code, database table and HTML form from our above code.
Here I’m just working on improving PHP logic from above code.
Subscribe on Youtube
Vivek Vengala
YouTube 1 k
If the post superglobal is set and not empty, if it’s true I’m assigning the submitted values to
variables using this code and encrypting password to md5 hashing instead of plain text
password.
After that, I’m writing an SQL query to check the user details in database tables with
submitted user information. Executing the query and also counting the number of rows in
result set. If the number of rows is equal to one then we will set the session and redirect the
user to members area page. Otherwise, we will display an error message. I’m already
displaying these messages in above the form HTML code, if you are following from above
you will already have it.
And next important cocept is user will be able to login with username or email. For that I’ll
modify the SQL query little bit. So, that we will check the submitted value if it’s email we will
check it with email column. If it’s not email we will check it in username column. And I’m
creating SQL query over couple of conditions and statments.
Checking the user, if his account is activated then only we will allow hime to login otherwise
he won’t be able to login. For that I’m using active column, if the user activates his account
by verifying his email. Then only his account status becomes active, that means active
column becomes to 1.
Youtube
YouTube 1 k
I hope this article helped you to learn user login using PHP and MySQL . To get latest news
and updates follow us on twitter & facebook, subscribe to newsletter. If you have any
feedback please let us know by using comment form.
simple login form in php php login script simple login form in php with mysql database
simple php login script simple login php simple login page in php login php mysql simple
php login php login code php simple login
Share this:
Re late d Posts
How to Create Secure User Login Script in PHP PDO – Free Download
Viv e k Ve ngala
Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad
India.
Max
Reply
Alan
Reply
Onutu Mihai
session_destroy();
Reply
Sarah Ellix
Reply
62arts
session_destroy();
Reply
stu�trivia
Reply
ganesh
Reply
Dennis Briggs
Very nice on the html form after register I added forgot password line
Forgot Password
Reply
Vivek Vengala
Reply
Jomar
Reply
Vivek Vengala
Hello Jomar,
Reply
mony
Hi dear,
It works perfectly but when a user logout and then again he click on
login button “which will take him to login page ” it took him directly to
the user dashboard using old stored details.
How to prevent accessing page directly if I have the URL. so you always
be redirected to login page if you are not already logged in may be
setting sessions . can you help please ?
Reply
Vivek Vengala
Hi mony,
Reply
Andrew
Reply
Vivek Vengala
Hi Andrew,
Please post your code, so that I can see the problem clearly.
Reply
Minitek
$result query returned false. And i, for the life of me, can’t �gure out
why.
My code is as follows:
**(note i have changed the names of variables a bit, but everything else
is the same as your code shown in the video, at least as far as i can
see).
Reply
Vivek Vengala
Please check the SQL query, if there are no errors use this below
code with mysqli_query()
mysqli_query($connection, $sql) or
die(mysqli_error($connection));
By using above code, you can get the error… If your problem
didn’t solve please let me know…
Reply
Zayn
Reply
lauro
Hello all i was wondering something i manage to get the code to work
??
lets say i have a website and i want to use this “login” how do i send my
members to MYpage once they register ? could someone please help
me on this ?
Reply
Vivek Vengala
Hi lauro,
Reply
hixtox
Reply
hixtox
i used
Reply
Vivek Vengala
Reply
Ishara
After sur�ng 2 days �nally found the working one for new mysql
version. The best tutorial found over the internet. Thank you. You
saved my day �
Reply
om
Interesting….
But that isn’t secure login! First escaping the variables then match
password with strcmp and password encryption is required. Using
password_hash will be ok and at the time login verify with
password_verify function. Wish you a good luck!
Reply
Vivek Vengala
Hi Om Prakash,
Reply
rajni
Hi
Thanks for the wonderful post. It is very helpful. Just one thing I am
looking for i.e redirecting to another page if the user successfully
logged in. It will be very great if you guide me.
Regards,
Rajni
Reply
Vivek Vengala
Hi Rajni, you can use header PHP function to redirect users after
login.
Reply
Leave a Reply:
Name* Email* W ebsite
Save my name, email, and website in this browser for the next time I comment.
SUBMIT