Sie sind auf Seite 1von 10

4/20/2019 Login System using PHP with MySql Database with Session | Code And Course

Code And Course

Visual S… Expert C… Design… Pro CSS… C# 6.0 a… Beginni…

$44.99 $44.99 $29.99 $29.99 $44.99 $34.99

Login System using PHP with MySql Database with Session


21 Comments / Hosting, HTML, MySQL, PHP, Tutorial / By Nawaraj Shah

User login system tutorial using HTML, PHP, MySql, Session and CSS on which user can log in to the profile page and log out. This is a
very basic but effective tutorial. Where I do not only focus what it gives but also focus on how it gives. After watching this, I hope you
will learn about HTML, PHP, MySql and also Session.

index.php

1. <?php
2. include('login.php'); // Includes Login Script
3. if(isset($_SESSION['login_user'])){
4. header("location: profile.php"); // Redirecting To Profile Page
5. }
6. ?>
7. <!DOCTYPE html>
8. <html>
9. <head>
10. <title>Login Form in PHP with Session</title>
11. <link href="style.css" rel="stylesheet" type="text/css">
12. </head>
13. <body>
14. <div id="login">
15. <h2>Login Form</h2>
16. <form action="" method="post">
17. <label>UserName :</label>
18. <input id="name" name="username" placeholder="username" type="text">
19. <label>Password :</label>
20. <input id="password" name="password" placeholder="**********" type="password"><br><br>
21. <input name="submit" type="submit" value=" Login ">
22. <span><?php echo $error; ?></span>
23. </form>
24. </div>
25. </body>
26. </html>

This index.php is a default page and it contains an HTML login form.

Line 2. include('login.php'); allow us to connect with a login.php file.

Privacy Code Of Conduct


Ad Find out how you can judge the
efficacy of The Internet Society's…
Let’s Bring Privacy Back

Learn More

Line 3–5. If a user is already login then isset($_SESSION['login_user']) become true then the user does not need to log in and we
redirect the user to profile.php.

18, 20, 21. In an input field, we use name=" " attribute which we later use in a server side to grab the value of that input field.

login.php

1. <?php
2. session_start(); // Starting Session
Privacy - Terms
3. $error = ''; // Variable To Store Error Message

https://www.codeandcourse.com/2017/09/login-system-using-php-with-mysql-database-with-session/ 1/10
4/20/2019 Login System using PHP with MySql Database with Session | Code And Course
4. if (isset($_POST['submit'])) {
5. if (empty($_POST['username']) || empty($_POST['password'])) {
6. $error = "Username or Password is invalid";
7. }
8. else{
9. // Define $username and $password
10. $username = $_POST['username'];
11. $password = $_POST['password'];
12. // mysqli_connect() function opens a new connection to the MySQL server.
13. $conn = mysqli_connect("localhost", "root", "", "company");
14. // SQL query to fetch information of registerd users and finds user match.
15. $query = "SELECT username, password from login where username=? AND password=? LIMIT 1";
16. // To protect MySQL injection for Security purpose
17. $stmt = $conn->prepare($query);
18. $stmt->bind_param("ss", $username, $password);
19. $stmt->execute();
20. $stmt->bind_result($username, $password);
21. $stmt->store_result();
22. if($stmt->fetch()) //fetching the contents of the row {
23. $_SESSION['login_user'] = $username; // Initializing Session
24. header("location: profile.php"); // Redirecting To Profile Page
25. }
26. mysqli_close($conn); // Closing Connection
27. }
28. ?>

Here we start the code by starting the session.

3. We declare $error = ''; to store an error message.

4. By using isset($_POST['submit']) we check, did user click the submit button or not? If the submit button is not set then we do
nothing.

Privacy Code Of Conduct


Ad Find out how you can judge the
efficacy of The Internet Society's…
Let’s Bring Privacy Back

Learn More

5. We use empty($_POST['username']) || empty($_POST['password']) to check username and password are empty or not. If it is empty
then we give an error message to the user. If not then we can continue our login process from an else statement.

10–11. We grab username and password which is submitted by the user.

13. Make a database connection with this login system

15. The SQL query to see is this username and password exist in a database or not.

22–23. If exist, store the username in a session and then redirect the user to profile.php.

26. At last, close a database connection.

session.php

1. <?php
2. // mysqli_connect() function opens a new connection to the MySQL server.
3. $conn = mysqli_connect("localhost", "root", "", "company");
4. session_start();// Starting Session
5. // Storing Session
6. $user_check = $_SESSION['login_user'];
7. // SQL Query To Fetch Complete Information Of User
8. $query = "SELECT username from login where username = '$user_check'";
9. $ses_sql = mysqli_query($conn, $query);
10. $row = mysqli_fetch_assoc($ses_sql);
11. $login_session = $row['username'];
12. ?>

profile.php
Privacy - Terms

https://www.codeandcourse.com/2017/09/login-system-using-php-with-mysql-database-with-session/ 2/10
4/20/2019 Login System using PHP with MySql Database with Session | Code And Course

1. <?php
2. include('session.php');
3. if(!isset($_SESSION['login_user'])){
4. header("location: index.php"); // Redirecting To Home Page
5. }
6. ?>
7. <!DOCTYPE html>
8. <html>
9. <head>
10. <title>Your Home Page</title>
11. <link href="style.css" rel="stylesheet" type="text/css">
12. </head>
13. <body>
14. <div id="profile">
15. <b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
16. <b id="logout"><a href="logout.php">Log Out</a></b>
17. </div>
18. </body>
19. </html>

3–5. We check, did ‘login user’ session exist or not? If ‘login user’ did not exist then that means, a user did not login we redirect the
user to the login page which is index.php.

15. We use <?php echo $login_session; ?> to display username in a profile page.

Privacy Code Of Conduct


Ad Find out how you can judge the
efficacy of The Internet Society's…
Let’s Bring Privacy Back

Learn More

16. If a user clicks the logout link then they will redirect to the logout page.

logout.php

1. <?php
2. session_start();
3. if(session_destroy()) // Destroying All Sessions {
4. header("Location: index.php"); // Redirecting To Home Page
5. }
6. ?>

In this logout page, we simply destroy the session and redirect the user to login page.

style.css

1. /*----------------------------------------------
2. CSS Settings For HTML Div ExactCenter
3. ------------------------------------------------*/
4. span {
5. color:red
6. }
7. h2
8.
9. {
10. text-align:center;
11. border-radius:10px 10px 0 0;
12. margin:-10px -40px;
13. padding:15px
14. }
15. hr
16.
17. {
18. border:0;
19. border-bottom:1px solid;
20. margin:10px -40px;
21. margin-bottom:30px
22. }
23. #login
24.
25. { Privacy - Terms

https://www.codeandcourse.com/2017/09/login-system-using-php-with-mysql-database-with-session/ 3/10
4/20/2019 Login System using PHP with MySql Database with Session | Code And Course
26. margin: auto;
27. width:450px;
28. border-radius:10px;
29. border:2px solid #ccc;
30. padding:10px 40px 25px;
31. background-color:#b6e6ff;
32. }
33. input[type=text],input[type=password]
34.
35. {
36. width:96.5%;
37. padding:10px;
38. margin-top:8px;
39. border:1px solid #ccc;
40. padding-left:5px;
41. font-size:20px;
42. }
43. input[type=submit]
44.
45. {
46. width:100%;
47. background-color:#2f90ff;
48. color:#fff;
49. border:2px solid #white;
50. padding:10px;
51. font-size:20px;
52. cursor:pointer;
53. border-radius:5px;
54. }
55. #profile
56.
57. {
58. padding:50px;
59. border:1px dashed grey;
60. font-size:20px;
61. background-color:#DCE6F7
62. }
63. #logout
64.
65. {
66. float:right;
67. padding:5px;
68. border:dashed 1px gray
69. }
70. a
71.
72. {
73. text-decoration:none;
74. color:#6495ed
75. }
76. i
77.
78. {
79. color:#6495ed
80. }

MySql

1. CREATE DATABASE company;


2.
3. CREATE TABLE login(
4. id int(10) NOT NULL AUTO_INCREMENT,
5. username varchar(255) NOT NULL,
6. password varchar(255) NOT NULL,
7. PRIMARY KEY (id)
8. )

Execute this code and then create a database and table for this login system.

Twitter: https://twitter.com/CodeAndCoins
Google+ : https://goo.gl/7vjhrp
Facebook: https://www.facebook.com/CodeAndCoins
Blog: https://CodeAndCoins.blogspot.com

Share this Video and Subscribe to my channel


Privacy - Terms

https://www.codeandcourse.com/2017/09/login-system-using-php-with-mysql-database-with-session/ 4/10
4/20/2019 Login System using PHP with MySql Database with Session | Code And Course

Login System with PHP and MySQL Database wit…


wit…

← Previous Post Next Post →

21 thoughts on “Login System using PHP with MySql Database with Session”

IBTASAM SOFT
OCTOBER 22, 2017 AT 12:46 PM

A.O.A
sir,
kindley help me.

Reply

IBTASAM SOFT
OCTOBER 22, 2017 AT 12:48 PM

sir iam need a log in page and sin up page plz help me

Reply

NAWARAJ SHAH
OCTOBER 23, 2017 AT 7:38 AM

You can check out my channel for registration form.

Reply

BIJOY SK
APRIL 7, 2018 AT 9:12 AM

good

Reply

Privacy - Terms

https://www.codeandcourse.com/2017/09/login-system-using-php-with-mysql-database-with-session/ 5/10
4/20/2019 Login System using PHP with MySql Database with Session | Code And Course

MATHI
APRIL 16, 2018 AT 11:12 AM

The provided information’s are very useful to me. It’s a wonderful site for learning web application. Thank you for sharing this wonderful
blog.
PHP Training in Chennai | PHP Course in Chennai | PHP Institutes in Chennai | PHP Training Center in Chennai

Reply

3D ARTIST
MAY 3, 2018 AT 8:15 AM

why not work on xamp 5.0

Reply

KAMAL
JUNE 19, 2018 AT 4:59 AM

The gave data's are exceptionally valuable to me. It's a magnificent site for learning web application. Much obliged to you for sharing
this awesome blog.
MBA Talks | Article Submission sites | Education | Technology

Reply

YGO PRO
JUNE 22, 2018 AT 10:26 PM

Warning: session_start(): Cannot send session cache limiter – headers already sent (output started at
/storage/ssd5/928/6222928/public_html/03_Login.html:1) in /storage/ssd5/928/6222928/public_html/03_Login.php on line 4

HELP

Reply

RADHA KARTHI
JULY 20, 2018 AT 1:09 PM

Thank you for this post!! I have just discovered your blog recently and I really like it! I will definitely try some of your insights.
PHP Training
PHP Institutes in Chennai

Reply

SUMAYA MANZOOR
JULY 24, 2018 AT 12:24 PM

Privacy - Terms

https://www.codeandcourse.com/2017/09/login-system-using-php-with-mysql-database-with-session/ 6/10
4/20/2019 Login System using PHP with MySql Database with Session | Code And Course
This comment has been removed by the author.

Reply

SUMAYA MANZOOR
JULY 24, 2018 AT 12:24 PM

Big Thanks for this wonderful read. I enjoyed every little bit of reading and I have bookmarked your website to check out new stuff of
your blog a must read blog.
web designing institute
web designing training

Reply

RAM RAMKY
AUGUST 14, 2018 AT 12:54 PM

Nice post! definitively I will come back to update me on this technology Thanks for the informative post. Keep doing.
java j2ee training
core java training in chennai

Reply

SUMANT KUMAR
SEPTEMBER 25, 2018 AT 1:47 PM

thank you for sharing useful post.


web programming tutorial
welookups

Reply

TEJU TEJU
OCTOBER 6, 2018 AT 9:58 AM

It is nice blog Thank you porovide important information and i am searching for same information to save my time The blog is so
interactive and Informative , you should write more blogs like this Ruby on rails Online Training

Reply

KEVIN COOPER
OCTOBER 18, 2018 AT 7:55 AM

Thanks for this post! I just discovered your blog recently and I really like it!
http://www.digiorbite.com

Reply

Privacy - Terms

https://www.codeandcourse.com/2017/09/login-system-using-php-with-mysql-database-with-session/ 7/10
4/20/2019 Login System using PHP with MySql Database with Session | Code And Course

XPLORE IT CORP
OCTOBER 27, 2018 AT 6:08 AM

Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
php training with placement in coimbatore
php training centre in coimbatore

Reply

PROPLUS ACADEMY
OCTOBER 30, 2018 AT 10:44 AM

Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
php course
Web development

Reply

KATE TECHNOLOGIES
DECEMBER 6, 2018 AT 12:01 PM

Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
php application development
php web application development company india

Reply

UNKNOWN
DECEMBER 17, 2018 AT 2:14 AM

THANK YOU!@:)))

Reply

PIFORD MOHALI
JANUARY 10, 2019 AT 9:11 AM

Information technology has much more to serve the students. There is wide range of services and scope for students that can help them
to improve their future. We are also providing Training to the students at very low and reasonable prices that a student or another
person can easily afford and make their future bright and shine. For more details you can visit our website and know more about our
services 6 Months industrial training in Chandigarh python training in mohali android training in chandigarh

Reply

ROMEIN
MARCH 22, 2019 AT 9:10 PM

Privacy - Terms

https://www.codeandcourse.com/2017/09/login-system-using-php-with-mysql-database-with-session/ 8/10
4/20/2019 Login System using PHP with MySql Database with Session | Code And Course
Maybe add hash function?

Reply

Leave a Comment
Your email address will not be published. Required fields are marked *

Type here..

Name*

Email*

Website

Post Comment »

Search a post

Search … 

Copyright © [2019] Code And Course Privacy - Terms

https://www.codeandcourse.com/2017/09/login-system-using-php-with-mysql-database-with-session/ 9/10
4/20/2019 Login System using PHP with MySql Database with Session | Code And Course

https://www.codeandcourse.com/2017/09/login-system-using-php-with-mysql-database-with-session/ 10/10