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,

it should need 5 files for this login-logout system, they are
- login/index page
- datafetch/usercheck page
- session/check page
- welcome/profile page
- 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>
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
and we should have to start the session in all the page.
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
Thanks for the nice thing.
ReplyDeletethanks
ReplyDeleteAnd what if someone enters such password (without double quotes?): "' or 'a'='a" ?
ReplyDeleteThen that would be considered as SQL injection :D
Deleteuse $user=mysql_real_escape_string($_POST['username']);
ReplyDelete$pass=mysql_real_escape_string($_POST['password']);
for that. i'm missed to use that.
i live demo when i enter my name its not working
ReplyDeletethe code is not working, when i filles username and password, it remains on same page, not going to the profile page
ReplyDeleteOuch. very old code.
ReplyDeletemysql_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
I would add ob_end and flush, just to make it more flexible in future ;)
ReplyDeleteBtw. i recomend PDO to connect to the database and making query, much better and safety. Cheers
It is working thank you
ReplyDeleteit doesnt work it stays in same page after i click login
ReplyDeletegood one tq:)
ReplyDeleteit is not working it stays in the same page
ReplyDeleteif customer enters a wrong password. how to shoe ur username r password is invalid
ReplyDeletethankii @Arunkumar...nice code...i have learn it!!
ReplyDeleteits not working
ReplyDeletenice1..thnak you...
ReplyDeletecan you provide sql data for this code?? please.
ReplyDeletevery nice one ..really to much helpful
ReplyDeleteif i entered login, it shows same page
ReplyDeleteit is not working..
ReplyDeletehie 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
ReplyDeleteit is working guys...
ReplyDeleteit is working guys...
ReplyDeleteI get this in the demo.
ReplyDeleteFatal error: Call to undefined function session_register() in /home/my4edge/public_html/demos/login-system/login.php on line 11
Thanks for the article very helpful, but you can see more advance security for login page ai here
ReplyDeletehttp://www.kythuatmang.net/advance-security-login-system-using-php-mysql.php
more info.
ReplyDeletecheck this.
vsc-project.blogspot.com
Thanx a lot..................................
ReplyDeleteit is not working
ReplyDeletesir
ReplyDeletei want to know that which tools you use to create images for your post.
it would be really helpful...
thanx a lot...
the code is not working, when i filles username and password, it remains on same page, not going to the profile page
ReplyDeletemany thanks for sharing all code step by step.
ReplyDeleteI like this tutorial and its excellent for beginners and more users
thanks again
Thanks!!! A ton mate,
ReplyDeleteIt works!!
ReplyDeleteI am student of BMIT college. if you want to more information regarding same than please contact us by
email address is webteam@bmitjaipur.org
ReplyDeleteI am student of BMIT college. if you want to more information regarding same than please contact us by
email address is webteam@bmitjaipur.org
it doesnt work it stays in same page after i click login
ReplyDeleteWas very useful for my project. Thanks
ReplyDeleteA very good article. Thanks
ReplyDeletefor who didnt work ,. just change the sql code like this SELECT * FROM `login` WHERE ,, instead of SELECT id FROM `login` WHERE ..
ReplyDeleteand delete the session_register()
Thanx bro..
ReplyDeleteIt helps me a lot..
Please try to send some tricks with jQuery..
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
ReplyDeleteWarning: 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
why this problem i get?
ReplyDeleteRemains on the same page :(
ReplyDeleteRemains on the same page :(
ReplyDeleteSIR. 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.
ReplyDeleteI have posted this on 23/01/2016
ReplyDeleteThnx for the simple code for php....Perfect defining easy to understand
ReplyDeleteCall to undefined function session_register()
ReplyDeletenice demo
ReplyDeletenice work frnds
ReplyDeleteis it working
Deleteno
Deleteits not working bro
ReplyDeleteits not working...
ReplyDeleteits not working
ReplyDeleteThanks a lot. For me code get run...
ReplyDeletethank u so much!!
ReplyDeleteWhat is the username and password??
ReplyDeletehellow
ReplyDeleteThe code is not working,it remain on the same page after clicking Login button.
ReplyDeletesalah lah coding tu. tak keluar langsung.
ReplyDeleteSome old codes.
ReplyDeleteBut overall it works with minor modifications.
Great job
Thanks a lot, its working fine, its amusing code for login and logout in php
ReplyDeletethanks a lot, its working fine, amusing code, only think is remove session_register("sessionusername"); in login.php,
ReplyDeletethanks bro
ReplyDeleteI am download you code & try but don't working. Please help me.
ReplyDeletehttps://youtu.be/_gjJvp3wPEo
ReplyDeleteIt is not working ..
ReplyDeleteKanhasoft is the PHP Application Development Company based in India and USA. We are promoting business services across USA, UK, Japan, Canada, Singapore, Australia, Sweden, Norway, Switzerland, New Zealand, Germany, South Africa and 45 more.
ReplyDelete