problem managing session with cookies and session_set_save_handler()

problem managing session with cookies and session_set_save_handler()

am 24.11.2007 21:08:25 von ddolgoff

Hello to everyone,

I have a problem of managing a session with cookies and
"session_set_save_handler()". I want to use php's built-in session
management mechanism with user-level session storage functions to
store session data on the client in cookies.

The simple implementation that I have works well under PHP 5.2.0 ,
Apache 2.0.54 and WinXP.

When I put it on Linux RHEL3 (Apache/2.0.46 (Red Hat) PHP/5.0.4) I get
the following error:

"Warning: Cannot modify header information - headers already sent
in /......./ct.php on line 37". It seems like a simple error but
setting a cookie with "session_set_save_handler()" becomes kind of
tricky.

Looks like there is something wrong with my "ob_buffering" settings.

I will appreciate if anyone has any ideas or solutions to the problem.

Here is my script:


function open($save_path, $session_name)
{
return true;
}

function close()
{
return true;
}

function read($id)
{
$sess_name = 'sess_'.$id;
if(isset($_COOKIE[$sess_name]))
{
return base64_decode($_COOKIE[$sess_name]);
}
else
{
return '';
}
}

function write($id, $sess_data)
{
$sess_name = 'sess_'.$id;
if($sess_data) setcookie($sess_name, base64_encode($sess_data), 0,
'/');
ob_end_flush();
return true;
}

function destroy($id)
{
$sess_name = 'sess_'.$id;
setcookie ($sess_name, '', time() - 3600);
return true;
}

function gc($maxlifetime)
{
return true;
}

ob_start();
session_set_save_handler('open', 'close', 'read', 'write', 'destroy',
'gc');
register_shutdown_function('session_write_close');
session_start();


$_SESSION['probe'] = 'session data goes here';

header('Content-Type: text/html; charset=win1252');

print_r($_SESSION);

?>

Thanks to everyone,
Dmitry

Re: problem managing session with cookies and session_set_save_handler()

am 24.11.2007 21:15:17 von Jerry Stuckle

ddolgoff@gmail.com wrote:
> Hello to everyone,
>
> I have a problem of managing a session with cookies and
> "session_set_save_handler()". I want to use php's built-in session
> management mechanism with user-level session storage functions to
> store session data on the client in cookies.
>
> The simple implementation that I have works well under PHP 5.2.0 ,
> Apache 2.0.54 and WinXP.
>
> When I put it on Linux RHEL3 (Apache/2.0.46 (Red Hat) PHP/5.0.4) I get
> the following error:
>
> "Warning: Cannot modify header information - headers already sent
> in /......./ct.php on line 37". It seems like a simple error but
> setting a cookie with "session_set_save_handler()" becomes kind of
> tricky.
>
> Looks like there is something wrong with my "ob_buffering" settings.
>
> I will appreciate if anyone has any ideas or solutions to the problem.
>
> Here is my script:
>
> >
> function open($save_path, $session_name)
> {
> return true;
> }
>
> function close()
> {
> return true;
> }
>
> function read($id)
> {
> $sess_name = 'sess_'.$id;
> if(isset($_COOKIE[$sess_name]))
> {
> return base64_decode($_COOKIE[$sess_name]);
> }
> else
> {
> return '';
> }
> }
>
> function write($id, $sess_data)
> {
> $sess_name = 'sess_'.$id;
> if($sess_data) setcookie($sess_name, base64_encode($sess_data), 0,
> '/');
> ob_end_flush();
> return true;
> }
>
> function destroy($id)
> {
> $sess_name = 'sess_'.$id;
> setcookie ($sess_name, '', time() - 3600);
> return true;
> }
>
> function gc($maxlifetime)
> {
> return true;
> }
>
> ob_start();
> session_set_save_handler('open', 'close', 'read', 'write', 'destroy',
> 'gc');
> register_shutdown_function('session_write_close');
> session_start();
>
>
> $_SESSION['probe'] = 'session data goes here';
>
> header('Content-Type: text/html; charset=win1252');
>
> print_r($_SESSION);
>
> ?>
>
> Thanks to everyone,
> Dmitry
>

First of all, don't use obstart(). Fix the problem which is causing
output to be sent. As the message says - check line 37 of ct.ext.

Once you get the real problem fixed, you don't need such crude bypasses
as obstart().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: problem managing session with cookies and session_set_save_handler()

am 24.11.2007 21:27:35 von ddolgoff

On Nov 24, 3:15 pm, Jerry Stuckle wrote:
> ddolg...@gmail.com wrote:
> > Hello to everyone,
>
> > I have a problem of managing a session with cookies and
> > "session_set_save_handler()". I want to use php's built-in session
> > management mechanism with user-level session storage functions to
> > store session data on the client in cookies.
>
> > The simple implementation that I have works well under PHP 5.2.0 ,
> > Apache 2.0.54 and WinXP.
>
> > When I put it on Linux RHEL3 (Apache/2.0.46 (Red Hat) PHP/5.0.4) I get
> > the following error:
>
> > "Warning: Cannot modify header information - headers already sent
> > in /......./ct.php on line 37". It seems like a simple error but
> > setting a cookie with "session_set_save_handler()" becomes kind of
> > tricky.
>
> > Looks like there is something wrong with my "ob_buffering" settings.
>
> > I will appreciate if anyone has any ideas or solutions to the problem.
>
> > Here is my script:
>
> > >
> > function open($save_path, $session_name)
> > {
> > return true;
> > }
>
> > function close()
> > {
> > return true;
> > }
>
> > function read($id)
> > {
> > $sess_name = 'sess_'.$id;
> > if(isset($_COOKIE[$sess_name]))
> > {
> > return base64_decode($_COOKIE[$sess_name]);
> > }
> > else
> > {
> > return '';
> > }
> > }
>
> > function write($id, $sess_data)
> > {
> > $sess_name = 'sess_'.$id;
> > if($sess_data) setcookie($sess_name, base64_encode($sess_data), 0,
> > '/');
> > ob_end_flush();
> > return true;
> > }
>
> > function destroy($id)
> > {
> > $sess_name = 'sess_'.$id;
> > setcookie ($sess_name, '', time() - 3600);
> > return true;
> > }
>
> > function gc($maxlifetime)
> > {
> > return true;
> > }
>
> > ob_start();
> > session_set_save_handler('open', 'close', 'read', 'write', 'destroy',
> > 'gc');
> > register_shutdown_function('session_write_close');
> > session_start();
>
> > $_SESSION['probe'] = 'session data goes here';
>
> > header('Content-Type: text/html; charset=win1252');
>
> > print_r($_SESSION);
>
> > ?>
>
> > Thanks to everyone,
> > Dmitry
>
> First of all, don't use obstart(). Fix the problem which is causing
> output to be sent. As the message says - check line 37 of ct.ext.
>
> Once you get the real problem fixed, you don't need such crude bypasses
> as obstart().
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Jerry,

Thank you for the response. I am sorry, I did not make it clear. Line
37 is the line where setcookie() function gets called. It is inside
write() function :
if($sess_data) setcookie($sess_name, base64_encode($sess_data), 0,
'/');

That is why I cannot understand why it causes the problem.

Best regards,
Dmitry

Re: problem managing session with cookies and session_set_save_handler()

am 24.11.2007 21:55:03 von Jerry Stuckle

ddolgoff@gmail.com wrote:
> On Nov 24, 3:15 pm, Jerry Stuckle wrote:
>> ddolg...@gmail.com wrote:
>>> Hello to everyone,
>>> I have a problem of managing a session with cookies and
>>> "session_set_save_handler()". I want to use php's built-in session
>>> management mechanism with user-level session storage functions to
>>> store session data on the client in cookies.
>>> The simple implementation that I have works well under PHP 5.2.0 ,
>>> Apache 2.0.54 and WinXP.
>>> When I put it on Linux RHEL3 (Apache/2.0.46 (Red Hat) PHP/5.0.4) I get
>>> the following error:
>>> "Warning: Cannot modify header information - headers already sent
>>> in /......./ct.php on line 37". It seems like a simple error but
>>> setting a cookie with "session_set_save_handler()" becomes kind of
>>> tricky.
>>> Looks like there is something wrong with my "ob_buffering" settings.
>>> I will appreciate if anyone has any ideas or solutions to the problem.
>>> Here is my script:
>>> >>> function open($save_path, $session_name)
>>> {
>>> return true;
>>> }
>>> function close()
>>> {
>>> return true;
>>> }
>>> function read($id)
>>> {
>>> $sess_name = 'sess_'.$id;
>>> if(isset($_COOKIE[$sess_name]))
>>> {
>>> return base64_decode($_COOKIE[$sess_name]);
>>> }
>>> else
>>> {
>>> return '';
>>> }
>>> }
>>> function write($id, $sess_data)
>>> {
>>> $sess_name = 'sess_'.$id;
>>> if($sess_data) setcookie($sess_name, base64_encode($sess_data), 0,
>>> '/');
>>> ob_end_flush();
>>> return true;
>>> }
>>> function destroy($id)
>>> {
>>> $sess_name = 'sess_'.$id;
>>> setcookie ($sess_name, '', time() - 3600);
>>> return true;
>>> }
>>> function gc($maxlifetime)
>>> {
>>> return true;
>>> }
>>> ob_start();
>>> session_set_save_handler('open', 'close', 'read', 'write', 'destroy',
>>> 'gc');
>>> register_shutdown_function('session_write_close');
>>> session_start();
>>> $_SESSION['probe'] = 'session data goes here';
>>> header('Content-Type: text/html; charset=win1252');
>>> print_r($_SESSION);
>>> ?>
>>> Thanks to everyone,
>>> Dmitry
>> First of all, don't use obstart(). Fix the problem which is causing
>> output to be sent. As the message says - check line 37 of ct.ext.
>>
>> Once you get the real problem fixed, you don't need such crude bypasses
>> as obstart().
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Jerry,
>
> Thank you for the response. I am sorry, I did not make it clear. Line
> 37 is the line where setcookie() function gets called. It is inside
> write() function :
> if($sess_data) setcookie($sess_name, base64_encode($sess_data), 0,
> '/');
>
> That is why I cannot understand why it causes the problem.
>
> Best regards,
> Dmitry
>

Why are you even trying to store session data in a cookie? That defeats
the whole purpose of sessions. Just store the data in your own cookie.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: problem managing session with cookies and session_set_save_handler()

am 27.11.2007 00:09:41 von Kailash Nadh

On Nov 24, 8:08 pm, ddolg...@gmail.com wrote:
> Hello to everyone,
>
> I have a problem of managing a session with cookies and
> "session_set_save_handler()". I want to use php's built-in session
> management mechanism with user-level session storage functions to
> store session data on the client in cookies.
>
> The simple implementation that I have works well under PHP 5.2.0 ,
> Apache 2.0.54 and WinXP.
>
> When I put it on Linux RHEL3 (Apache/2.0.46 (Red Hat) PHP/5.0.4) I get
> the following error:
>
> "Warning: Cannot modify header information - headers already sent
> in /......./ct.php on line 37". It seems like a simple error but
> setting a cookie with "session_set_save_handler()" becomes kind of
> tricky.
>
> Looks like there is something wrong with my "ob_buffering" settings.
>
> I will appreciate if anyone has any ideas or solutions to the problem.
>
> Here is my script:
>
> >
> function open($save_path, $session_name)
> {
> return true;
>
> }
>
> function close()
> {
> return true;
>
> }
>
> function read($id)
> {
> $sess_name = 'sess_'.$id;
> if(isset($_COOKIE[$sess_name]))
> {
> return base64_decode($_COOKIE[$sess_name]);
> }
> else
> {
> return '';
> }
>
> }
>
> function write($id, $sess_data)
> {
> $sess_name = 'sess_'.$id;
> if($sess_data) setcookie($sess_name, base64_encode($sess_data), 0,
> '/');
> ob_end_flush();
> return true;
>
> }
>
> function destroy($id)
> {
> $sess_name = 'sess_'.$id;
> setcookie ($sess_name, '', time() - 3600);
> return true;
>
> }
>
> function gc($maxlifetime)
> {
> return true;
>
> }
>
> ob_start();
> session_set_save_handler('open', 'close', 'read', 'write', 'destroy',
> 'gc');
> register_shutdown_function('session_write_close');
> session_start();
>
> $_SESSION['probe'] = 'session data goes here';
>
> header('Content-Type: text/html; charset=win1252');
>
> print_r($_SESSION);
>
> ?>
>
> Thanks to everyone,
> Dmitry

I tested your script out and it worked without any errors.
Can you make sure the script does not output anything before
session_start() ?
(Empty spaces or strat characters before
Kailash Nadh | http://kailashnadh.name