Miscellaneous Code
  Home arrow Miscellaneous Code arrow Session class
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS CODE

Session class
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2003-08-14

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A session class i wrote to learn more about classes. I think it's working good. And if there are any bugs, please post a comment.
    And don't kill me for the bad english explanation, am no native!

    By : Dios

    <?php
    /* Author: Stephan Six
    E-Mail: DiosV2@AOL.com / Dios@six-Design.de
    Homepage: http://www.six-design.de
    Last-Modified: 14.08.2003

    Copyright: Stephan Six

    ****************************************************

    HowToUse:
    Methods:
    $foo = new my_session([bool Session_starten]); <-- Declare a new object of the class.
    Use "new my_session(true)" to start a session.
    $foo->start(); <-- Start a session.
    $foo->session([Session_id]); <-- Gives you the session_id.
    If a session_id is passed to the method, a new session will be started,
    using the given session_id (See PHP-function: session_id())
    $foo->stats(); <-- Shows you information about the environment. (PHP-version, reg_globals)
    $foo->clear(); <-- Ends a session
    $foo->test_cookies(); <-- Returns true if cookies are allowed, false if not.
    Needs to be executed before any output is displayed. Because the page, needs
    to be reloaded.
    $foo->show(); <-- Shows all variables in a session
    $foo->show_errors(); <-- Shows you an error-log. (To check if theres something wrong with the values,
    given to the set-method.)
    $foo->clear_errors(); <-- Clears the error-log (No real use, but anyway)

    $foo->set(); <-- Registers a variable in a session. Values passed to the method, need to be:
    A pair (or a number of pairs) of "Name => Value", or an Array where the key will be used as the var-name
    and the value as the var-value.

    Example:
    $test = "text"; is to be registerd:

    objekt->set(VarName, VarValue);
    $foo->set("test", $test);
    (It's the same as: $foo->set("test", "text");)
    Another example:
    $foo->set("value1", 1, "value2", 4);

    Differences if register_globals = on/off:
    Example:
    $foo = 1;
    $bar->set("foo", 2);
    echo "foo = " . $foo;

    register_globals = on:
    Ausgabe: foo = 2;
    register_globals = off:
    Ausgabe: foo = 1;

    $foo->get(["_ARRAY"]); <-- Get a variable from a Session.
    If the string "_ARRAY" is passed, the method will return an Array with all session
    variables.
    Example:
    You want $x to have the value of the session-var $bar:
    var = objekt->get(SessionVarName);
    $x = $foo->get("bar");
    */

    class my_session{
    var $rg;
    var $old;
    var $s_id;
    var $errors;

    function my_session($s = false){
    if(ini_get("register_globals")) $this->rg = true;
    else $this->rg = false;

    if(phpversion() <= "4.1.0") $this->old = true;
    else $this->old = false;

    if($s == true) $this->start();
    }

    function start(){
    session_start();
    $this->s_id = session_id();
    }

    function sessionid($new_id = false){
    if(!$new_id) return $this->s_id;
    else session_id($new_id);
    }

    function clear(){ session_destroy(); }

    function set(){
    $v = func_get_args();

    if(is_array($v[0])){
    foreach($v[0] as $k => $value){
    if($this->check_var_syntax($k)) $this->reg_vars($k, $value);
    }
    } else {
    if(count($v) % 2){
    $this->errors[] = "Variable '" . $v[count($v) - 1] . "' ignored";
    array_pop($v);
    }

    for($i = 0; $i < count($v); $i += 2){
    if($this->check_var_syntax($v[$i])) $this->reg_vars($v[$i], $v[$i+1]);
    }
    }
    }

    function check_var_syntax($buff){
    if(preg_match("!^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$!i", $buff)) return true;
    else {
    $this->errors[] = "Variable '<i>" . $buff . "</i>' ignored,<br>wrong syntax";
    return false;
    }
    }

    function reg_vars($n, $v){
    if($this->rg){
    $GLOBALS[$n] = $v;
    session_register("$n");
    } else {
    session_register("$n");
    if($this->old) $GLOBALS['HTTP_SESSION_VARS'][$n] = $v;
    else $GLOBALS['_SESSION'][$n] = $v;
    }
    }

    function get($buff){
    if($buff == "_ARRAY"){
    if($this->old) return $GLOBALS['HTTP_SESSION_VARS'];
    else return $GLOBALS['_SESSION'];
    } if($this->old) return $GLOBALS['HTTP_SESSION_VARS'][$buff];
    else return $GLOBALS['_SESSION'][$buff];
    }

    function test_cookies(){
    if($this->old) $url = $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'] . "?" . $GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING'];
    else $url = $GLOBALS['_SERVER']['PHP_SELF'] . "?" . $GLOBALS['_SERVER']['QUERY_STRING'];

    $start = $this->get("start");

    if($start > 0){
    switch($start){
    case 1:
    setcookie("cookie", "1");
    $this->set("start", 2);
    header("Location: $url");
    break;
    case 2:
    if($this->old) ($GLOBALS['HTTP_COOKIE_VARS']['cookie']) ? ($cookies = true) : ($cookies = false);
    else ($GLOBALS['_COOKIE']['cookie']) ? ($cookies = true) : ($cookies = false);
    break;
    }
    } else {
    $this->set("start", 1);
    header("Location: $url&PHPSESSID=" . session_id());
    }
    return $cookies;
    }

    function stats(){
    $stats = array();
    if($this->rg) $stats['register_globals'] = 1;
    else $stats['register_globals'] = 0;

    $stats['PHP-Version'] = phpversion();

    $this->display("Description", "Value", $stats);
    }

    function show(){
    if($this->old) $session_values = $GLOBALS['HTTP_SESSION_VARS'];
    else $session_values = $GLOBALS['_SESSION'];

    if(!is_array($session_values)) $session_values = array("No session-vars" => "");

    $this->display("Name of the variable", "Value", $session_values);
    }

    function show_errors(){
    if(!is_array($this->errors)) $this->errors[] = "No error";

    $this->display("Description", "", $this->errors, "left");
    }

    function clear_errors(){ $this->errors = ""; }

    function display($head_1, $head_2, $content, $align = "center"){
    echo '<table width="300" border="0" cellspacing="0" cellpadding="3" align="center" style="font-family: Arial, Helvetica, Verdana, sans-serif; font-size: 10pt;">
    <tr>
    <td bgcolor="#cccccc"><b>' . $head_1 . '</b></td>
    <td bgcolor="#cccccc">&nbsp;</td>
    <td bgcolor="#cccccc" align="' . $align . '"><b>' . $head_2 . '</b></td>
    </tr>';

    foreach($content as $k => $v){
    ($i++ % 2) ? ($c = "#e0e0e0") : ($c = "#eeeeee");
    echo '<tr>
    <td bgcolor="' . $c . '">' . $k . '</td>
    <td bgcolor="' . $c . '">&nbsp;</td>
    <td bgcolor="' . $c . '" align="' . $align . '">' . $v . '</td>
    </tr>';
    }
    echo '</table>';
    }
    }
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Addressing software-as-a-service challenges using Tivoli security and WebSphere solutions

    Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base.
    FREE! Go There Now!


    NEW! Evaluate IBM Lotus Sametime Standard V8.0

    Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration.
    FREE! Go There Now!


    NEW! Evaluate IBM Rational Developer for System i V7.1

    Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications.
    FREE! Go There Now!


    NEW! IBM Rational AppScan Standard Edition V7.7

    Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7.
    FREE! Go There Now!


    NEW! Integrating XML into Your Enterprise Using Data Federation

    XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats.
    FREE! Go There Now!


    NEW! Rational Build Forge Express eKit

    Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast.
    FREE! Go There Now!


    NEW! Rational Modeling Extension for Microsoft.Net

    Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms.
    FREE! Go There Now!


    NEW! Rational Talks to You: Scott Ambler on being agile in a global development environment

    Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered!
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Functional Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications.
    FREE! Go There Now!


    NEW! Webcast: Introducing the new Information Server and Solutions community: LeverageInformation

    User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    MISCELLANEOUS CODE ARTICLES

    - upload image to database sql
    - Random Password Generator
    - BCroot, get the root of a number with BC fun...
    - Find pi in a high precision
    - [PHP5] FORMCHECKER : data validation
    - SPL and ITERATOR : examples
    - Xml with Rss Feeds
    - [PHP5] NOTIMEOUT PACKAGE
    - Class to return variables to a Flash movie.
    - BCGCD Greatest Common Denominator (Large Num...
    - HMAC
    - Binary to Decimal
    - Decimal to Binary using logs
    - web.framework v1.0.0
    - Shopping Cart Class





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT