Miscellaneous Code

  Home arrow Miscellaneous Code arrow Session class
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:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


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