Page 1 of 1

Creating customized session management...

Posted: Wed Aug 21, 2002 2:06 pm
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:

Posted: Wed Aug 21, 2002 2:14 pm
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.

Posted: Wed Aug 21, 2002 2:30 pm
by Takuma
Thanks but any luck with the session management script? It's quite urgent. Thanks. :wink:

Posted: Wed Aug 21, 2002 2:43 pm
by JPlush76
are you protecting national security? :lol:

Posted: Wed Aug 21, 2002 2:48 pm
by nielsene
I sometimes am :)

Ahhhh

Posted: Wed Aug 21, 2002 4:02 pm
by Takuma
Ahhh could actually tell me why please, cos I'm pretty desperate about session management not workin!

Posted: Wed Aug 21, 2002 4:05 pm
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?

Posted: Wed Aug 21, 2002 4:48 pm
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;

?>

Posted: Wed Aug 21, 2002 5:04 pm
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.

Posted: Thu Aug 22, 2002 12:57 am
by Takuma
That might be it nielsene... I'l try

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