PHP login script tutorial – login page example

Are you looking for simple PHP login script tutorial with a very beginner level then i think this post is very much useful for you. PHP login form or login page in php is require in any simple PHP application, so to start a basic web application in PHP you may require to create a login form or login system. Here i want to discuss about simple login script with session and a welcome screen, checking post value with database and authenticate, session create, store session value and upon logout destroy the session.You can free download PHP login script from here. Here in this login example I used login code with mysqli. Hope this PHP login tutorial will help you to get the basic knowledge about the login authentication as a beginner in php.
Take a quick look at this live demo with username – demo, pass – demo to understand the flow of the PHP login script. Also you can download this php login script from here and can try in your localhost or live server where you want to do.
Some advance level of login script available on PHP secure login script with md5 encryption.

php login page example – tutorial step by step:

Learn how to create login page in php.
Creating the Database

CREATE TABLE user
(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255) UNIQUE,
password VARCHAR(255)
);

create dbconfig.php for connecting with the database

<?php
$host = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';
$dbconfig = mysqli_connect($host,$username,$password,$database);
?>

Now we have to create the login page. It contains the html and php with the login form.
login.php

<?php
include("dbconfig.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password received from loginform
$username=mysqli_real_escape_string($dbconfig,$_POST['username']);
$password=mysqli_real_escape_string($dbconfig,$_POST['password']);
$sql_query="SELECT id FROM user WHERE username='$username' and password='$password'";
$result=mysqli_query($dbconfig,$sql_query);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
$count=mysqli_num_rows($result);// If result matched $username and $password, table row must be 1 row
if($count==1)
{
$_SESSION['login_user']=$username;

header("location: welcome-home.php");
}
else
{
$error="Username or Password is invalid";
}
}
?>
//And now in body part of this page we have to include the html login form.......

welcome-home.php
Session verification. If no session value page redirect to login.php and if session exist then show welcome screen. Also user can get logout link after succesfull login.

<?php
if(!isset($_SESSION['login_user']))
{
header("Location: login.php");
}
else
{
$login_session=$_SESSION['login_user'];
?>
Welcome <?php echo $login_session;?>
<?php
}
?>

logout.php
Logged out with session destroy – destroy all the session when logout link click by user and redirect to login.php.

<?php
session_start();
if(session_destroy())
{
header("Location: login.php");
}
?>

Thanks for watching this tutorial. You can try yourself. I will share some useful script in PHP as well as some tips and tricks in PHP programming. Viewers can subscribe our newsletter to get all the updates about web programming.

20 responses to “PHP login script tutorial – login page example”

  1. FrancisFem says:

    My name is Alexa Waldman What a nice surprise that I found this website! Thanks for contributing excellent knowledge. The web site is so life changing and will help me tremendously. I’m impressed by the particular information that you’ve put on this forum.

  2. Prosun Guha says:

    Thanks Alexa Waldman

  3. furtdsolinopv says:

    Woah! I’m really loving the template/theme of this website. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between superb usability and visual appeal. I must say that you’ve done a amazing job with this. Additionally, the blog loads super fast for me on Opera. Exceptional Blog!

  4. Grant Ankrum says:

    Good article, thanks.

  5. Nga Mcfaddin says:

    Great post. Thanks for the article.

  6. Warren says:

    Would like to thank you, have tried all the other websites to find this and only yours worked. This is much appreciated and thank you again.
    regards
    Warren

  7. Guest says:

    One of our visitors lately suggested the following website.

  8. TroyKGinns says:

    Keep this going please, great job!

  9. JessLNimtz says:

    Having look at this I thought it was extremely informative. I appreciate you spending some time and effort to put this information together.
    One time i again find myself spending way too much time both reading and posting comments.

  10. Ida Rosko says:

    Wonderful Post. Thanks for share. More wait.

  11. Cole says:

    mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

  12. joeM says:

    Dear Prosun Guha,
    Thanks for a great tutorial! You rock.
    Now, I would like to integrate your login function in a Popup box. But I can not figure out how to call the login.php file you made, I wonder if you can help?
    Thanks

  13. Sajjad says:

    Great and Helpful Tutorial. So easy to understand and use, Thanks for sharing with us.

  14. doc2g says:

    I have a problem with this code . I noticed that even when I came out of the session, still the user who left can delete an entry on my website.

    • Hi Dear, This is a session checking issue. Basically you have to check that session exist or not for that particular user on the top of the page. If yes run the following lines of code and if not exist then redirect to the login page.

  15. doc2g says:

    Thank you! And I realized this problem and solved. Thank you again for the wonderful article.

  16. Javed Ur Rehman says:

    This is really good tutorial, hope you also find it helpful.

Leave a Reply

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