problem managing session with cookies and session_set_save_handler()

This is a discussion on problem managing session with cookies and session_set_save_handler() within the PHP General forums, part of the PHP Programming Forums category; Hello to everyone, I have a problem of managing a session with cookies and "session_set_save_handler()". I want to ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-24-2007
ddolgoff@gmail.com
 
Posts: n/a
Default problem managing session with cookies and session_set_save_handler()

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:

<?php

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
Reply With Quote
  #2 (permalink)  
Old 11-24-2007
Jerry Stuckle
 
Posts: n/a
Default Re: problem managing session with cookies and session_set_save_handler()

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:
>
> <?php
>
> 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
==================

Reply With Quote
  #3 (permalink)  
Old 11-24-2007
ddolgoff@gmail.com
 
Posts: n/a
Default Re: problem managing session with cookies and session_set_save_handler()

On Nov 24, 3:15 pm, Jerry Stuckle <jstuck...@attglobal.net> 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:

>
> > <?php

>
> > 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
Reply With Quote
  #4 (permalink)  
Old 11-24-2007
Jerry Stuckle
 
Posts: n/a
Default Re: problem managing session with cookies and session_set_save_handler()

ddolgoff@gmail.com wrote:
> On Nov 24, 3:15 pm, Jerry Stuckle <jstuck...@attglobal.net> 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:
>>> <?php
>>> 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
==================

Reply With Quote
  #5 (permalink)  
Old 11-26-2007
Kailash Nadh
 
Posts: n/a
Default Re: problem managing session with cookies and session_set_save_handler()

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:
>
> <?php
>
> 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 <?php could be the problem)

Kailash Nadh | http://kailashnadh.name
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:18 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0