Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Session in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 223
    Comment on it

    Session is used in php to store information with the help of session variable. By creating session we can use that information across multiple pages. When we create an application we write the code, do some changes and then we close it our system know who you are but our server doesn't  know who we are that's why we create session so that the server knows who we are and what work we are doing

    HOW TO START A PHP SESSION

    <?php
    //to start session
    session_start();
    ?>

     After starting the session we create, read ,destroy and unset the session variables.

    <?php
    // to start sessionsession_start();
    // to initializing session with php variable$_SESSION[login_user]= $empname;
    // to read sessionecho $_SESSION[login_emp];
    // to destroy all sessionssession_destroy();
    // to destroy specific sessionif(isset($_SESSION[id]))unset($_SESSION[id]);
    ?>

    Then we make a login form with the help of session

    Index.php page goes here:

    
    <?php
    session_start();
    if(!$_SESSION[emp_logged_in])
    {
    if(!$_SESSION[user_id])
    {header(location:login_form.php?problem=not logged in);
    exit;
    }
    }else{
    $username=$_SESSION[emp_name];
    }?>
    <!DOCTYPE html>
    <html lang=en><head>
    <title>PHP Login Form with Sessions</title>
    <meta charset=utf-8>
    <meta name=viewport content=width=device-width, initial-scale=1>
    <! Bootstrap CSS >
    <link rel=stylesheet href=http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css>
    <! Google Fonts>
    <link href=https://fonts.googleapis.com/css?family=Roboto rel=stylesheet type=text/css>
    <! Custom CSS >
    <link rel=stylesheet href=css/home.css>
    </head><body><div class=container id=main_div>
    <h1>Welcome Employee :<?php echo $emp;?></h1>
    <a class=btn btn-primary href=logout.php>Logout</a>
    </div><footer class=footer>
    <div class=container>
    
    </div>
    </footer><! Jquery Script lib>
    <script src=https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js></script>
    <! Bootstrap Script lib>
    <script src=http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js></script></body></html>

    Login_form.php

    <!DOCTYPE html>
    <html lang=en><head>
    <title>PHP Login Form with Sessions</title>
    <meta charset=utf-8>
    <meta name=viewport content=width=device-width, initial-scale=1>
    <! Bootstrap CSS >
    <link rel=stylesheet href=http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css>
    <! Google Fonts>
    <link href=https://fonts.googleapis.com/css?family=Roboto rel=stylesheet type=text/css>
    <! Custom CSS >
    <link rel=stylesheet href=css/login_form.css>
    </head><body><div class=container id=log_div>
    <?php
    if(isset($_GET[problem]))
    {
    $problem=$_GET[problem];
    echo <div class=container><div class=alert alert-warning alert-dismissable>
    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
    <i class=icon fa fa-warning></i> Alert! $problem.</div></div>;
    }if(isset($_GET[reg_success]))
    {
    $reg_success=$_GET[reg_success];
    echo <div class=container><div class=alert alert-success alert-dismissable>
    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
    <i class=icon fa fa-warning></i> Alert! $reg_success.</div></div>;
    }
    ?>
    <h3 id=log_heading>PHP Login Form with Sessions</h3>
    <div class=wrapper><form action=login_validate.php method=post name=Login_Form class=form-signin>
    <h3 class=form-signin-heading>Welcome Back! Please Sign In</h3>
    <hr class=colorgraph>
    <br><input type=email class=form-control input_field name=Email_ID placeholder=Email ID required= autofocus= />
    <input type=password class=form-control input_field name=Password placeholder=Password required= /><button class=btn btn-primary  name=login_Submit value=Login type=Submit>Login</button>
    <a href=register.php class=btn btn-primary>Register</a>
    </form>
    </div></div><footer class=footer>
    <div class=container>
    <p class=text-muted>For more useful articles visit:<a href=http://buzzland.in> http://Buzzland.in</a></p>
    </div>
    </footer><! Jquery Script lib>
    <script src=https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js></script>
    <! Bootstrap Script lib>
    <script src=http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js></script></body></html>

    Register.php page goes here:

    <!DOCTYPE html>
    <html lang=en><head>
    <title>PHP Login Form with Sessions</title>
    <meta charset=utf-8>
    <meta name=viewport content=width=device-width, initial-scale=1>
    <! Bootstrap CSS >
    <link rel=stylesheet href=http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css>
    <! Google Fonts>
    <link href=https://fonts.googleapis.com/css?family=Roboto rel=stylesheet type=text/css>
    <! Custom CSS >
    <link rel=stylesheet href=css/reg_form.css>
    </head><body><div class=container id=log_div>
    <h3 id=log_heading>PHP Login Form with Sessions</h3>
    <div class=wrapper>
    <form action=add_user.php method=post name=Register_Form class=form-signin>
    <h3 class=form-signin-heading>Register Form</h3>
    <hr class=colorgraph>
    <br><input type=text class=form-control input_field name=Username placeholder=Username required= autofocus= />
    <input type=email class=form-control input_field name=Email_id placeholder=Email ID required= />
    <input type=password class=form-control input_field name=Password placeholder=Password required= /><button class=btn btn-primary name=Reset value=Reset type=Reset>Reset</button>
    <button class=btn btn-primary name=Reg_Submit value=Login type=Submit>Register</button>
    </form>
    </div>
    </div><footer class=footer>
    <div class=container>
    
    </div>
    </footer>
    <! Jquery Script lib>
    <script src=https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js></script>
    <! Bootstrap Script lib>
    <script src=http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js></script></body></html>

    Then we destroy the session :

    <?php
    session_start();
    ?>
    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    // remove all session variables
    session_unset(); 
    
    // destroy the session 
    session_destroy(); 
    ?>
    
    </body>
    </html>

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: