Creating customized session management...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Creating customized session management...

Post by Takuma »

Hi, I have made the following code but can't get it working.... :'( I tried hard but no luck, please help.

Code: Select all

<?php
  /*

    Session Library - Controls Sessions using MySQL

  */

  $mysql                    = array();
  $mysql&#1111;"open_connection"] = true;
  $mysql&#1111;"host"]            = "localhost";
  $mysql&#1111;"usr"]             = "root";
  $mysql&#1111;"pwd"]             = "";
  $mysql&#1111;"tbl"]             = "session";
  $mysql&#1111;"db"]              = "test";

  function sess_open($save_path, $sess_name) &#123;
    GLOBAL $mysql;

    if($mysql&#1111;"open_connection"])
    &#123;
      $link = mysql_pconnect($mysql&#1111;"host"],$mysql&#1111;"usr"],$mysql&#1111;"pwd"]) or die(mysql_error());
      mysql_select_db($mysql&#1111;"db"]);
    &#125;

    return(true);
  &#125;

  function sess_read($sess_id) &#123;
    GLOBAL $mysql;


    $temp = mysql_query("SELECT data FROM ".$mysql&#1111;"tbl"]." WHERE id = '$sess_id'") or die(mysql_error());

    if(mysql_num_rows($temp) == 0) &#123;
      return("");
    &#125;

    $result = mysql_fetch_array($result);
    mysql_free_result($temp);

    return($row&#1111;"data"]);
  &#125;

  function sess_write($sess_id, $val) &#123;
    GLOBAL $mysql;

    $temp = mysql_query("REPLACE INTO ".$mysql&#1111;"tbl"]." VALUES ('$sess_id','$val', null)") or die(mysql_error());

    return(true);
  &#125;

  function sess_destroy($sess_id) &#123;
    GLOBAL $mysql;

    $temp = mysql_query("DELETE FROM ".$mysql&#1111;"tbl"]." WHERE id = '$sess_id'") or die(mysql_error());

    return(true);
  &#125;

  function sess_gc($max_lifetime) &#123;
    GLOBAL $mysql;

    $old = time() - $max_lifetime;

    $temp = mysql_query("REPLACE INTO ".$mysql&#1111;"tbl"]." UNIX_TIMESTAMP(t_stamp) < $old") or die(mysql_error());

    return(true);
  &#125;

  session_set_save_handler("sess_open", "", "sess_read", "sess_write", "sess_destroy", "sess_gc")
?>
Also I made a MySQL class thing just for fun but this isn't working too well too...

Code: Select all

<?php
  /*

    MySQL Function Library - Contains Functions for MySQL

  */

  class lsbb_mysql &#123;
    $mysql         = array();
    $mysql&#1111;"usr"]  = "root";
    $mysql&#1111;"pwd"]  = "";
    $mysql&#1111;"host"] = "localhost";
    $mysql&#1111;"db"]   = "test"

    function connect() &#123;
      GLOBAL $mysql;

      $connect = @mysql_connect($mysql&#1111;"host"],$mysql&#1111;"usr"],$mysql&#1111;"pwd"]);
      if(!$connect) &#123;
        echo "Failed to establish connection with mySQL.";
        exit();
      &#125;

      $select = @mysql_select_db($mysql&#1111;"db"]);
      if(!$select) &#123;
        echo "Failed to select a database.";
      &#125;
    &#125;

    function close() &#123;
      mysql_close();
    &#125;

    function free($result_handle) &#123;
      mysql_free_result($result_handle);
    &#125;

    function execute($sql_query,$option) &#123;
      switch ($option) &#123;
        case 1:
          $temp = @mysql_query($sql_query);
          if(!$temp) &#123;
            echo "Failed to execute to query: ".$sql_query."<br>mySQL has returned the following error: ".mysql_error().".";
            mysql_close();
            exit();
          &#125;
          $result = mysql_fetch_array($temp);
          if(mysql_num_rows($temp) == 0) &#123;
            echo "Query: ".$sql_query." returned nothing from the database.";
            mysql_close();
            exit();
          &#125;
        break;
        case 2:
          $temp = @mysql_query($sql_query);
          if(!$temp) &#123;
            echo "Failed to execute to query: ".$sql_query."<br>mySQL has returned the following error: ".mysql_error().".";
            mysql_close();
            exit();
          &#125;
          $result = mysql_object_array($temp);
          if(mysql_num_rows($temp) == 0) &#123;
            echo "Query: ".$sql_query." returned nothing from the database.";
            mysql_close();
            exit();
          &#125;
        break;
        case 3:
          $temp = @mysql_query($sql_query);
          if(!$temp) &#123;
            echo "Failed to execute to query: ".$sql_query."<br>mySQL has returned the following error: ".mysql_error().".";
            mysql_close();
            exit();
          &#125;
          $result = mysql_num_rows($temp);
        break;
      &#125;
      return($result);
    &#125;
  &#125;
?>
This creates the following error on line 15 which is

Code: Select all

$mysql         = array();
Please help... :roll:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Regarding your class:

those variables need to be declared like

Code: Select all

class foo
&#123;
    var $bar;


    function foo() // constructor
    &#123;
        $this->bar = "Initial Value";
    &#125;
&#125;
Note the "var" before the class variable. PHP is also a little picky about initializiing class variables, that's why I prefer to do it in the constructor.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Thanks but any luck with the session management script? It's quite urgent. Thanks. :wink:
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

are you protecting national security? :lol:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I sometimes am :)
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Ahhhh

Post by Takuma »

Ahhh could actually tell me why please, cos I'm pretty desperate about session management not workin!
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Sorry, I don't know.. I've never written my own session handlers..

How is it not working? Is it giving runtime error messages? Is running, but not working, etc?
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

thats alot of code for someone just learning sessions, have you tried the very basic of session control?

here is a really simple session script I started with

Code: Select all

<?
session_start();


//***** INCLUDE GLOBAL MASTER FILE FOR DATABASE CONNECTIONS
include ("includes/main.php");
include ("includes/newfunc.php");


if ($userid && $password)
&#123;
  // if the user has just tried to log in

 
$result = query_db("select * from user where user_uname = '$userid' and user_pass = '$password'");
		   
 
$num_rows = mysql_numrows($result);

  if ($num_rows >0 )
  &#123;

    $valid_user = $userid;
	$id = $id;
    session_register("valid_user","id");
  &#125;

?>
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Have you checked your php.ini file? According to the entry on session_set_save_handler you have to set the session.save_handler variable to "user" if you want to use your own custon session handlers.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

That might be it nielsene... I'l try

P.S. How usefull is this PHP manual.... VERY! :D
Post Reply