Re: [PHP] Postgresql session handling

2001-02-26 Thread Bolt Thrower

I [EMAIL PROTECTED] wrote:
 Can someone give me a simple example script that uses postgres session
 handling, that works with register_globals "off"?  


As a followup again, it seems what I've been running into is a bug
in PHP:
http://bugs.php.net/bugs.php?id=8772
http://bugs.php.net/bugs.php?id=9002

So, hopefully, someday, it will be fixed. 
-- 
Steve [EMAIL PROTECTED]
Now playing: Moon and Sun Part II: North's Son
(Amorphis - "Black Winter Day")

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Postgresql session handling

2001-02-22 Thread Bolt Thrower

I [EMAIL PROTECTED] wrote:

 It seems the pgsql_session_write() function is not even being invoked.
 Here it is:

As a followup, it seems that turning register_globals "on" allows the
pgsql_session_write() function to be called, and my test script works
if I replace $HTTP_SESSION_VARS["count"] with $count.  However, my
environment is such that I have register_globals "off", which seems
to conflict with sessions.  In fact, I even get a
Undefined variable:  HTTP_SESSION_VARS
error for the line 
$HTTP_SESSION_VARS["count"]++;

Can someone give me a simple example script that uses postgres session
handling, that works with register_globals "off"?  

Thanks,

-- 
Steve [EMAIL PROTECTED]
"And when you walk in golden halls, you get to keep the gold that falls"
-- Black Sabbath, "Heaven and Hell"

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Postgresql session handling

2001-02-20 Thread Bolt Thrower

PHP 4.04pl1, PostgresQL 7.0.3 on a RedHat 6.2 system.

I'm trying to get session data to be stored in a
postgres table.  I'm using the pgsql session handler from
http://www.csh.rit.edu/~jon/projects/php/pgsql_session_handler/, and I
think I've got it set up correctly, but I thought I'd ask here first.
With the following test script

+-
| ? include("pgsql_session_handler.inc"); ?
| ?
| session_start();
| session_register("count");
| $HTTP_SESSION_VARS[count]++;
| ?
| html
| head
| titleTest page/title
| /head
| body
| Hello!  You've been here ?= $HTTP_SESSION_VARS[count]; ? times!br
| 
| ?  print "To continue, A HREF=\"test-session.php\"click here/a"; ?
| 
| /body
| /html
+-

I can see php querying the database for session data (in the postgres
logs), but never writing session data to the database.  Thus, the
counter is always '1'.

Any ideas where I'm going wrong?  Why wouldn't session data get
written to the database?

Thanks,
-- 
Steve [EMAIL PROTECTED]
Now playing: Five Magics
(Megadeth - "Rust In Peace")

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Postgresql session handling

2001-02-20 Thread Richard Lynch

The writing of the session data occurs *after* the server-browser HTTP
connection is cut.

If you have any error-reporting happening in your session_write function,
you won't see it.

Alter that function to log errors to a file or something.

--
Visit the Zend Store at http://www.zend.com/store/
Wanna help me out?  Like Music?  Buy a CD: http://l-i-e.com/artists.htm
Volunteer a little time: http://chatmusic.com/volunteer.htm
- Original Message -
From: Bolt Thrower [EMAIL PROTECTED]
Newsgroups: php.general
Sent: Tuesday, February 20, 2001 12:45 PM
Subject: [PHP] Postgresql session handling


 PHP 4.04pl1, PostgresQL 7.0.3 on a RedHat 6.2 system.

 I'm trying to get session data to be stored in a
 postgres table.  I'm using the pgsql session handler from
 http://www.csh.rit.edu/~jon/projects/php/pgsql_session_handler/, and I
 think I've got it set up correctly, but I thought I'd ask here first.
 With the following test script

 +-
 | ? include("pgsql_session_handler.inc"); ?
 | ?
 | session_start();
 | session_register("count");
 | $HTTP_SESSION_VARS[count]++;
 | ?
 | html
 | head
 | titleTest page/title
 | /head
 | body
 | Hello!  You've been here ?= $HTTP_SESSION_VARS[count]; ? times!br
 |
 | ?  print "To continue, A HREF=\"test-session.php\"click here/a"; ?
 |
 | /body
 | /html
 +-

 I can see php querying the database for session data (in the postgres
 logs), but never writing session data to the database.  Thus, the
 counter is always '1'.

 Any ideas where I'm going wrong?  Why wouldn't session data get
 written to the database?

 Thanks,
 --
 Steve [EMAIL PROTECTED]
 Now playing: Five Magics
 (Megadeth - "Rust In Peace")

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 To contact the list administrators, e-mail: [EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Postgresql session handling

2001-02-20 Thread Bolt Thrower

"Richard Lynch" [EMAIL PROTECTED] wrote:
 The writing of the session data occurs *after* the server-browser HTTP
 connection is cut.

 If you have any error-reporting happening in your session_write function,
 you won't see it.

 Alter that function to log errors to a file or something.

It seems the pgsql_session_write() function is not even being invoked.
Here it is:

+-
| function pgsql_session_write($key, $val)
| {
|   /* debugging */
|   $fp = fopen("/tmp/phpdebugwrite","w") or die ("can't open file");
| fwrite($fp, "HERE");
| fclose($fp);
|   /* end debug */
| 
| global $pgsql_session_handle, $pgsql_session_table;
| 
| $key = addslashes($key);
| $val = addslashes($val);
| $now = time();
| 
| 
| 
| /*
|  * Delete any existing data for this session and then insert a new row
|  * containing the current session data, all in a single transaction.
|  * This should prevent collisions between multiple session instances.
|  *
|  * Thanks to "Will Fitzgerald" [EMAIL PROTECTED].
|  */
| $query = 'begin; ';
| $query .= "delete from $pgsql_session_table where session_id = '$key'; ";
| $query .= "insert into $pgsql_session_table values('$key', $now, '$val'); ";
| $query .= 'commit;';
| $result = @pg_exec($pgsql_session_handle, $query);
| 
| $ret = (pg_cmdtuples($result) == 0);
| pg_freeresult($result);
| 
| return ($ret);
| }
+-

I've added the debugging statements at the top.  Can you see anything
wrong with this function?  At the end of the include file in which
this appears, is the session_set_save_handler() call:

session_set_save_handler(
'pgsql_session_open',
'pgsql_session_close',
'pgsql_session_read',
'pgsql_session_write',
'pgsql_session_destroy',
'pgsql_session_gc'
);


Thanks,
-- 
Steve [EMAIL PROTECTED]
"And when you walk in golden halls, you get to keep the gold that falls"
-- Black Sabbath, "Heaven and Hell"

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Postgresql session handling

2001-02-20 Thread Yasuo Ohgaki

"Richard Lynch" [EMAIL PROTECTED] wrote:
  The writing of the session data occurs *after* the server-browser HTTP
  connection is cut.

  If you have any error-reporting happening in your session_write 
function,
  you won't see it.

  Alter that function to log errors to a file or something.

It seems the pgsql_session_write() function is not even being invoked.
Here it is:

+-
| function pgsql_session_write($key, $val)
| {
|  /* debugging */
|  $fp = fopen("/tmp/phpdebugwrite","w") or die ("can't open file");
| fwrite($fp, "HERE");
| fclose($fp);
|  /* end debug */

Why don't you just use error_log() function and write errors to a file?

|
| global $pgsql_session_handle, $pgsql_session_table;
|
| $key = addslashes($key);
| $val = addslashes($val);
| $now = time();
|
|
|
| /*
|  * Delete any existing data for this session and then insert a new 
row
|  * containing the current session data, all in a single transaction.
|  * This should prevent collisions between multiple session instances.
|  *
|  * Thanks to "Will Fitzgerald" [EMAIL PROTECTED].
|  */
| $query = 'begin; ';
| $query .= "delete from $pgsql_session_table where session_id = 
'$key'; ";
| $query .= "insert into $pgsql_session_table values('$key', $now, 
'$val'); ";
| $query .= 'commit;';
| $result = @pg_exec($pgsql_session_handle, $query);

I've seen this kind of implmentatin

delete 
insert 

IMHO, 2 sql operation that requires locks for session handling is just a 
waste
of resources. (delete requires higher lock level than select, transaction 
needs more resources, for each delete, insert requires fsync() and requires 
more disk head movements, etc, etc)
Unless you would like to implement some kind of session key validation to 
session handlers, simple

select 
insert or update ...

may give better performance with many web servers and users. (may not make 
any significant improvement, though. Disabling PostgreSQL's fsync() for 
session db would much better result. Just FYI)

|
| $ret = (pg_cmdtuples($result) == 0);
| pg_freeresult($result);
|
| return ($ret);
| }
+-

I've added the debugging statements at the top.  Can you see anything
wrong with this function?  At the end of the include file in which
this appears, is the session_set_save_handler() call:

You will not get output from die('.') ( or any print/echo) in session
handlers. Just use error_log() or implement user defined error handler
so that you can trigger error to display session handler errors.

my example pg_session_write() looks like:

function pg_session_write ($session_id, $session_data) {
global $db_session, $HTTP_SERVER_VARS, $HTTP_COOKIE_VARS;
if (!$db_session) {
error_log("session_write(): Cannot connect to database.",0);
}
if (strlen($session_data)  intval(SESS_DATA_MAX)) {
error_log('Session data too large. Unable to update session data for 
session '. $session_id, 0);
}

if (strlen($session_id) != 32) {
error_log("session_write(): Invalid Session ID",0);
return 0;
}
$session_id = addslashes($session_id);
$session_data = addslashes($session_data);
$uid = isset($HTTP_COOKIE_VARS['t_uid']) ? 
addslashes($HTTP_COOKIE_VARS['t_uid']) : '';

$query = 'SELECT session_id, i_session_counter FROM '. SESS_TABLE ." WHERE 
session_id = '$session_id'";
$result_id = pg_exec($db_session,$query);
$session_exist = pg_numrows($result_id);

if ($session_exist == 0) {
//  $query = 'INSERT INTO '. SESS_TABLE ." (session_id, i_time_created, 
i_last_active, t_remote_addr, t_session_data) VALUES ('$session_id', ". 
time() .", ". time() .", '". 
isset($HTTP_ENV_VARS['HTTP_X_FORWARDED_FOR'])?$HTTP_ENV_VARS['HTTP_X_FORWARDED_FOR']:$HTTP_SERVER_VARS['REMOTE_ADDR']
 
."', '$session_data')";
$query = 'INSERT INTO '. SESS_TABLE ." (session_id, i_time_created, 
i_last_active, t_uid, t_remote_addr, t_session_data) VALUES ('$session_id', 
". time() .", ". time() .", '$uid', '". $HTTP_SERVER_VARS['REMOTE_ADDR'] 
."', '$session_data')";
}
else {
$rec = pg_fetch_array($result_id,0,PGSQL_ASSOC);
$query = 'UPDATE '. SESS_TABLE ." SET t_session_data = 
'$session_data', 
i_last_active = ". time() .", t_uid = '$uid' , i_session_counter = ". 
intval(++$rec['i_session_counter']) ." WHERE session_id = '$session_id'";
}
$rows_affected = pg_cmdtuples(pg_exec($db_session,$query));
//error_log($query, 0);
if (!$rows_affected) {
error_log('session_write(): Failed to INSERT or UPDATE session.',0);
}
return $rows_affected;
}



session_set_save_handler(
 'pgsql_session_open',