FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Never Pay Taxes Again HGH Domain name registration
Web Hosting and Dedicated Servers Insurance Affordable web-hosting


HomeWatched TopicsRegisterSearchDirectory
FAQMemberlistUsergroupsLog inStoresItemsBank
Google

Reply to topic Page 1 of 1
Using Sessions in PHP
Message  

Reply with quote
Post Using Sessions in PHP 
Introduction
Building web applications with membership management is one of the most frequent tasks that every programmer does. Managing membership data, such as username, password and the member's profile with sessions in PHP is the easiest and simplest solution, although it is not the only one.

Preparation
With this tutorial, I assume you are using PHP 4.1.0 or the later. The first thing that we should know to use sessions is that you have to initialize the session.

session_start();  


Any script which has that line would make the script available to register a new session or read an existing session which weÂ’ve defined on another page.

Enough of the blah blah stuff, shall we now go to the example stuff?

Source: http://codewalkers.com/tutorials/32/1.html

View user's profile Send private message

Reply with quote
Post  
Basic Sessions
<?php
// page1.php
session_start();
$_SESSION["real_name"] = "Hermawan Haryanto";
print "<a href='page2.php'>Go to this page</a>";
?>  

<?php
// page2.php
session_start();
print $_SESSION["real_name"];
?>  


The above example shows you that I create a session variable named "real_name" storing my name as the value. Then on the second page, I print out the session variable and my name will show up.

LetÂ’s do another example:

<?php
session_start();
$count = $_SESSION["counter"] + 1;
$_SESSION["counter"] = $count;
print $_SESSION["counter"];
?>  


When you first access that page, it will display 1. Try to refresh the page and the number will grow.

To destroy or delete an existing session variable, you can use the unset command.

unset($_SESSION["session_name"]);  


Or if you want to delete all session variables (and the session itself), you can do it by using the destroy command.

session_destroy();  


The destroy command is usually used to log-off a user from the membership area. LetÂ’s make a membership area for our example.


Source: http://codewalkers.com/tutorials/32/2.html

View user's profile Send private message

Reply with quote
Post  
Running Membership with Sessions
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.


Source: http://codewalkers.com/tutorials/32/3.html

View user's profile Send private message

Reply with quote
Post  
Another Example
Still donÂ’t understand how to use sessions? Here is another example for you. LetÂ’s make an online user counter using sessions.

Step 1: Make a database table with 2 fields:

NAME                  TYPE
onlineuser_session    varchar(100)
onlineuser_time       varchar(30)  


Step 2: Download the dbal from my site: here.

Step 3: Make these files

<?php
// counter.php
class counter {
  var $db;
  var $sid;
  var $dbtable;
  var $online;
  function counter () {
    global $db, $tblcounter;
    $this->db = $db;
    $this->table = $tblcounter;
    $this->sid = session_id();
    $this->do_count();
  }
  function do_count () {
    if ($this->is_logged()) $this->update_log();
    else $this->new_log();
    $sql = "SELECT COUNT(onlineuser_session) as total " .
           "FROM {$this->table} " .
           "WHERE onlineuser_time=Â’".(time()-60)."Â’";
    $this->db->Query ($sql);
    $RS = $this->db->FirstRow();
    $this->online = $RS["total"];
  }
  function new_log () {
    $sql = "INSERT INTO {$this->table} " .
           "(onlineuser_session, onlineuser_time) " .
           "VALUES (‘{$this->sid}Â’,Â’".time()."Â’)";
    $this->db->Query ($sql);
  }
  function update_log () {
    $sql = "UPDATE {$this->table} " .
           "SET onlineuser_time=Â’".time()."Â’ " .
           "WHERE onlineuser_session=Â’{$this->sid}Â’";
    $this->db->Query ($sql);
  }
  function is_logged () {
    $sql = "SELECT * FROM {$this->table} " .
           "WHERE onlineuser_session=Â’{$this->sid}Â’";
    $this->db->Query ($sql);
    if ($this->db->RowCount>0) return true;
    else return false;
  }
};
?>  

<?php
  // index.php
  require_once("dbal.php");
  require_once("counter.php");
  session_start();
  $db = new dbMySql ("hostname", "username", "password", "database");
  $counter = new counter;
  print $counter->online;
?>  

Source: http://codewalkers.com/tutorials/32/4.html

View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
  



Google

FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Never Pay Taxes Again HGH Domain name registration
Web Hosting and Dedicated Servers Insurance Affordable web-hosting


Web Design by PlatinumShore.com & Web Hosting by TradeWebHosting.com