Simple Login logout system using php | 2my4edge

06 July 2013

Simple Login logout system using php

Login and logout system is the most important thing for the user management, session management is one important thing for manage the user for the whole time of login time. for that we have to use session for this. it should have 5 files for this. we have to fetch the same value for the user and check the session and register the session value. and make destroy for logout. let see it detail,
Login logout system

DOWNLOAD             LIVE DEMO

it should need 5 files for this login-logout system, they are
  1. login/index page
  2. datafetch/usercheck page
  3. session/check page
  4. welcome/profile page
  5. logout page

the above files must me needed for this. we can named it as our wish. let see what i done here. and the database file is needed, if you have any doubt about insert coding check here for insert code 

database name --> 2mylogin
table name --> login

DB.PHP

<?php 
$conn=mysql_connect('localhost','root','');
$db=mysql_select_db('2my4edge',$conn);
?>

INDEX.PHP

<?php
session_start();
?>
<form method="post" name="login" action="login.php">
<label for="name" class="labelname"> Username </label>
<input type="text" name="username" id="userid" required="required" /><br />
<label for="name" class="labelname"> Password </label>
<input type="password" name="password" id="passid" required="required"  /><br />
<input type="submit" name="submit" id="submit"  value="Login" />
</form>
in the index page we have to start the session, as the above mentioned. action is performed in login.php page.

LOGIN.PHP

<?php 
include('db.php');
session_start();
{
    $user=mysql_real_escape_string($_POST['username']);
    $pass=mysql_real_escape_string($_POST['password']);
    $fetch=mysql_query("SELECT id FROM `login` WHERE 
                         username='$user' and password='$pass'");
    $count=mysql_num_rows($fetch);
    if($count!="")
    {
    session_register("sessionusername");
    $_SESSION['login_username']=$user;
    header("Location:profile.php"); 
    }
    else
    {
       header('Location:index.php');
    }

}
?>
if the session is registered then go to check the profile.php page there session.php file is included so the session is checked.

SESSION.PHP

<?php
include('db.php');
session_start();
$check=$_SESSION['login_username'];
$session=mysql_query("SELECT username FROM `login` WHERE username='$check' ");
$row=mysql_fetch_array($session);
$login_session=$row['username'];
if(!isset($login_session))
{
header("Location:index.php");
}
?>
check the session value is correct or not. if the session is value is not set, then again redirect to index.php page.

PROFILE.PHP

<?php
include("session.php");
?>

<h3 align="center"> Hellow <?php echo $login_session; ?></h3> 
<h2 align="center" >Welcome to login system</h2> 

<h4 align="center">  click here to <a href="logout.php">LogOut</a> </h4>

assign the logout.php page in the profile.php page.

LOGOUT.PHP

<?php
session_start();
if(session_destroy())
{
header("Location: index.php");
}
?>
in the logout.php page, we should unset or destroy the session value as the above you seen. then locate to index.php page.
and we should have to start the session in all the page.

RELATED POSTS:

Export the MySQL database table as CSV format using PHP

Simple Pagination with loading effect using Qjuery, PHP and MySql

Marquee style in different manner with PHP and MySql

File download coding using PHP and Mysql

Username live availability Check using php and Ajax

Comment System using PHP and MySql

Detect Operating System and Detect the Browser using Php

Create Database with MySql and INSERT coding in php

70 comments:

  1. Thanks for the nice thing.

    ReplyDelete
  2. And what if someone enters such password (without double quotes?): "' or 'a'='a" ?

    ReplyDelete
    Replies
    1. Then that would be considered as SQL injection :D

      Delete
  3. use $user=mysql_real_escape_string($_POST['username']);
    $pass=mysql_real_escape_string($_POST['password']);
    for that. i'm missed to use that.

    ReplyDelete
  4. i live demo when i enter my name its not working

    ReplyDelete
  5. the code is not working, when i filles username and password, it remains on same page, not going to the profile page

    ReplyDelete
  6. Ouch. very old code.
    mysql_connect is deprecated as of PHP 5.5.0.
    I recommend enabling mysqli extension inside your php.ini configuration file. Then changing yuor code.
    Check http://php.net/manual/en/function.mysql-connect.php

    ReplyDelete
  7. I would add ob_end and flush, just to make it more flexible in future ;)
    Btw. i recomend PDO to connect to the database and making query, much better and safety. Cheers

    ReplyDelete
  8. it doesnt work it stays in same page after i click login

    ReplyDelete
  9. it is not working it stays in the same page

    ReplyDelete
  10. if customer enters a wrong password. how to shoe ur username r password is invalid

    ReplyDelete
  11. thankii @Arunkumar...nice code...i have learn it!!

    ReplyDelete
  12. its not working

    ReplyDelete
  13. can you provide sql data for this code?? please.

    ReplyDelete
  14. very nice one ..really to much helpful

    ReplyDelete
  15. if i entered login, it shows same page

    ReplyDelete
  16. hie i want to know if on one page my loop code is running continouly and then i use command of logout session_destroy() then my loop will stop or not plz reply and thank you in advance

    ReplyDelete
  17. I get this in the demo.
    Fatal error: Call to undefined function session_register() in /home/my4edge/public_html/demos/login-system/login.php on line 11

    ReplyDelete
  18. that's because session_register() has been removed from 5.4.0. see here; http://php.net/manual/en/function.session-register.php. this code example should be updated with the newer specification.

    ReplyDelete
  19. Thanks for the article very helpful, but you can see more advance security for login page ai here
    http://www.kythuatmang.net/advance-security-login-system-using-php-mysql.php

    ReplyDelete
  20. more info.
    check this.
    vsc-project.blogspot.com

    ReplyDelete
  21. Thanx a lot..................................

    ReplyDelete
  22. sir
    i want to know that which tools you use to create images for your post.
    it would be really helpful...
    thanx a lot...

    ReplyDelete
  23. the code is not working, when i filles username and password, it remains on same page, not going to the profile page

    ReplyDelete
  24. many thanks for sharing all code step by step.

    I like this tutorial and its excellent for beginners and more users

    thanks again

    ReplyDelete
  25. Thanks!!! A ton mate,
    It works!!

    ReplyDelete


  26. I am student of BMIT college. if you want to more information regarding same than please contact us by
    email address is webteam@bmitjaipur.org

    ReplyDelete


  27. I am student of BMIT college. if you want to more information regarding same than please contact us by
    email address is webteam@bmitjaipur.org

    ReplyDelete
  28. it doesnt work it stays in same page after i click login

    ReplyDelete
  29. Was very useful for my project. Thanks

    ReplyDelete
  30. A very good article. Thanks

    ReplyDelete
  31. for who didnt work ,. just change the sql code like this SELECT * FROM `login` WHERE ,, instead of SELECT id FROM `login` WHERE ..

    and delete the session_register()

    ReplyDelete
  32. Thanx bro..
    It helps me a lot..

    Please try to send some tricks with jQuery..

    ReplyDelete
  33. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\Training\my project\login.php:3) in C:\xampp\htdocs\Training\my project\login.php on line 5

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\Training\my project\login.php on line 7

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\Training\my project\login.php on line 7

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\Training\my project\login.php on line 8

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\Training\my project\login.php on line 8

    Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\Training\my project\login.php on line 11

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\Training\my project\login.php on line 11

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\Training\my project\login.php on line 12

    Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\Training\my project\login.php:3) in C:\xampp\htdocs\Training\my project\login.php on line 21

    ReplyDelete
  34. SIR. THATS A NICE PROJECT.. CAN U HELP ME WITH A FUNCTION WHEREBY ONCE THE USER LOGS IN HIS LOGIN CREDENTIALS BECOMES INVALID DO DAT WHILE HE IS LOGGED IN, SOMEONE ELSE CANNOT USE DSAME CREDENTIALS TO LOGIN.

    ReplyDelete
  35. I have posted this on 23/01/2016

    ReplyDelete
  36. Thnx for the simple code for php....Perfect defining easy to understand

    ReplyDelete
  37. Call to undefined function session_register()

    ReplyDelete
  38. its not working bro

    ReplyDelete
  39. Thanks a lot. For me code get run...

    ReplyDelete
  40. What is the username and password??

    ReplyDelete
  41. Ayesha ishrath7/22/2016 11:53 AM

    The code is not working,it remain on the same page after clicking Login button.

    ReplyDelete
  42. salah lah coding tu. tak keluar langsung.

    ReplyDelete
  43. Some old codes.
    But overall it works with minor modifications.
    Great job

    ReplyDelete
  44. Thanks a lot, its working fine, its amusing code for login and logout in php

    ReplyDelete
  45. thanks a lot, its working fine, amusing code, only think is remove session_register("sessionusername"); in login.php,

    ReplyDelete
  46. I am download you code & try but don't working. Please help me.

    ReplyDelete
  47. https://youtu.be/_gjJvp3wPEo

    ReplyDelete

^