• Simple Login logout system using php

    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.
  • 0 comments:

    Post a Comment

    FAVOURITE LINE

    To steal ideas from one is plagiarism. To steal from many is Research.

    ADDRESS

    Mumbai , Maharashtra

    EMAIL

    shikha.pathak6@gmail.com
    shikha.the.swt.pari@gmail.com

    Skype

    shikha_pari