[PHP] session problems when calling within a class.

2001-08-08 Thread Mark Garcia

Greetings,

I'm having a puzzling time trying to figure out how to work around the issue
of the session_id not existing after making calls to session_start and
session_id to generate an session.  It seems that by set_si() calling
session_id and seeing if there is a value, doesn't not work.  Hence, a new
session_id is created but is not persistant.  If I reload the page
sessions.test.php3 it generates a new session_id and can not detect the
previous one set.

For example, I one it once, and it produces output to the browser as:

==
--84c666bde8c46803a10821e32aacff91
++84c666bde8c46803a10821e32aacff91

If I reload the page by chosing refresh it produces:

==
--8855c34496314baacd73d3de0bc59b49
++8855c34496314baacd73d3de0bc59b49

Is this a bug in PHP where you cannot call session function within a
function of a class and have it stick?  Any help would be greatly
appreciated.

-MAG

sessions.inc
class ccsession {
var $_cm_user_id = '';
var $_cm_group_id = '';
var $_cm_auth_id = '';
var $_username = '';
var $_first_name = '';
var $_last_name = '';
var $_session_id = '';

var $is_set_si = '';
var $is_get_si ='';
var $is_sd = '';
var $is_ss = '';
var $is_sn = '';
var $is_sr = '';
var $is_su = '';

function ccsession($userid = '1', $groupid = '1', $authid = '1',
$username = 'guest', $first_name = 'Guest', $last_name = 'Account')
{
$this-ss();
$this-_cm_user_id = $userid;
$this-_cm_group_id = $groupid;
$this-_cm_auth_id = $authid;
$this-_username = $username;
$this-_first_name = $first_name;
$this-_last_name = $last_name;
$this-_session_id = $this-set_si();

}

// Shortcuts...
function set_si()
{
//if (0) {
//$c_session_id = $this-get_si();
print ==.session_id().br;
if (session_id()) {
$this-_session_id = $c_session_id;
} else {
srand((double)microtime()*1132590); // make 'rand' function
truly random
$this-_session_id = md5(rand(0,999));
}
$this-is_set_si = 1;
session_id($this-_session_id);
print --.session_id().br;
return $this-_session_id;
//}
}
}

sessions.test.php3
?php

include 'class/sessions.inc';

$n_sess = new ccsession();
print ++.session_id().br;

?

PHP Version 4.0.3pl1
System Linux skritz 2.2.5 #4 SMP Tue May 2 11:26:16 PDT 2000 i686 unknown
Build Date May 15 2001
Configure Command './configure' '--with-apache=../apache_1.3.12' '--with-db'
'--with-mcal=../libmcal'
Server API Apache
Apache Version Apache/1.3.12
Apache Release 10312100
Apache API Version 19990320
Hostname:Port my.domain.com:80
User/Group www(2001)/100
Max Requests Per Child: 0
Keep Alive: on
Max Per Connection: 100
Timeouts Connection: 300
Keep-Alive: 15
Server Root /usr/local/apache
Loaded Modules mod_php4, mod_setenvif, mod_auth, mod_access, mod_alias,
mod_userdir, mod_actions, mod_imap, mod_asis, mod_cgi, mod_dir,
mod_autoindex, mod_include, mod_status, mod_negotiation, mod_mime,
mod_log_config, mod_env, http_core

-- 
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] session problems when calling within a class.

2001-08-08 Thread Sean C. McCarthy

Hi,

I got the same problem but i left it (had no more time to fumble around
with it). But try this, because it was what I was going to try:

function ccsession($userid = '1', $groupid = '1', $authid = '1',
$username = 'guest', $first_name = 'Guest', $last_name = 'Account')
{
global $HTTP_SESSION_VARS;

// Change all session variables below to
// $this-_cm_user_id = $HTTP_SESSION_VARS[userid]
// and so on..

$this-ss();// I guess this is not important for sessions
$this-_cm_user_id = $userid;
$this-_cm_group_id = $groupid;
$this-_cm_auth_id = $authid;
$this-_username = $username;
$this-_first_name = $first_name;
$this-_last_name = $last_name;
$this-_session_id = $this-set_si();

}


// Once you started the session you might be able to set it
// the way you are doing here, in the following code as long 
// as you don't send anything before this. If you do it you 
// know you will have already sent the old ID to the user.

function set_si()
{
//if (0) {
//$c_session_id = $this-get_si();
print ==.session_id().br;
if (session_id()) {
$this-_session_id = $c_session_id;
} else {
srand((double)microtime()*1132590); // make 'rand' function
truly random
$this-_session_id = md5(rand(0,999));
}
$this-is_set_si = 1;
session_id($this-_session_id);
print --.session_id().br;
return $this-_session_id;
//}
}
}



By doing this you should be able (and I say SHOULD, I haven't tested it)
to get the value from the session, but you will still have to start the
session outside the class.

Also don't forget to change the $HTTP_SESSION_VARS with the new session
information if you change it.

From my point of view the problem is with variable scope. Even with
register_globals and track_vars the code didn't work for me. If you find
out something please post it (or at least send me a message with your
findings) because actually I was doing that stuff with just functions
and not a class.

Hope it's useful for you.

Sean C. McCarthy
SCI, S.L. (www.sci-spain.com)

Mark Garcia wrote:
 
 Greetings,
 
 I'm having a puzzling time trying to figure out how to work around the issue
 of the session_id not existing after making calls to session_start and
 session_id to generate an session.  It seems that by set_si() calling
 session_id and seeing if there is a value, doesn't not work.  Hence, a new
 session_id is created but is not persistant.  If I reload the page
 sessions.test.php3 it generates a new session_id and can not detect the
 previous one set.
 
 For example, ...


-- 
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]