Simple Login logout system using php Mysql
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"); } ?>
and we should have to start the session in all the page.
Comments
Post a Comment