Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - Using Sessions in PHP
MISCELLANEOUS

Using Sessions in PHP
By: Hermawan Haryanto
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 34
    2002-11-21

    Table of Contents:
  • Using Sessions in PHP
  • Basic Sessions
  • Running Membership with Sessions
  • Another Example
  • Conclusions

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Using Sessions in PHP - Running Membership with Sessions


    (Page 3 of 5 )

    In the first page, I stated that sessions are usually used to create a membership management. Now, I want to show you a little snippet to create a login system, which you have to complete to create your own membership management.

    <?php
    // functions.php

    function secure () {
      if (!($_SESSION["member_id"]) || ($_SESSION["member_id"] == "")) {
        Header("Location: ./login.php");
        exit();
      }
    }
    function login_check ($forms) {
      $error = "";
      $username = $forms["username"];
      $password = $forms["password"];
      if (trim($username) == "") $error .= "<li>Your username is empty.</li>";
      if (trim($password) == "") $error .= "<li>Your password is empty.</li>";
      /* from here, do your sql query to query the database to search for existing record with correct username and password */
      if (trim($error)!="") return $error;
    }

    function login ($forms) {
      $username = $forms["username"];
      $password = $forms["password"];
      /* do your sql query again, but now returning the id of member */
      return $member_id;
    }
    ?>

    <?php
    // login.php
    session_start();
    include ("functions.php");
    if ($_POST) {
      $error = login_check($_POST);
      if (trim($error)=="") {
        $_SESSION["member_id"] = login($_POST);
        Header("Location: ./index.php") // Redirect correct member
        exit();
      } else {
        print "Error:$error";
      }
    }
    ?>
    <form method="post">
    Username : <input type="text" name="username"><br />
    Password : <input type="password" name="password"><br />
    <input type="submit" value="Login">
    </form>

    <?php
      // index.php
      include("functions.php");
      session_start();
      secure();
    ?>

    In the above example, we have built three pages. The first page is functions.php. In this page we build all the functions to do login checking, the login and the login detector.

    The second page is login.php. We will show the login form to our user and do some processes to register sessions when they have passed the login check.

    Third and the last page is the sample of how to use the system. This page is only available when user has logged in or has the session variable "member_id" with some value, not null.

    More Miscellaneous Articles
    More By Hermawan Haryanto

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap