473,498 Members | 310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 24 '07 #1
4 2640
dd******@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.
js*******@attglobal.net
==================

Nov 24 '07 #2
On Nov 24, 3:15 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
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
Nov 24 '07 #3
dd******@gmail.com wrote:
On Nov 24, 3:15 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>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.
js*******@attglobal.net
==================

Nov 24 '07 #4
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
Nov 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5277
by: Brett Porter | last post by:
Howdy, For a challenge I took on a very large project and have decided to develop it on Red Hat, using PHP and MySQL. I am pleased to say that using Red Hat, PHP and MySQL has been extremely...
4
3098
by: Steve | last post by:
Hi, I need to be able to open another browser from within an already opened browser with a completely new session (using the same session variables). How can I do this? In essence, I want to...
8
3956
by: ndsoumah | last post by:
hello guys I'm trying to get access to variables I put in a session variable from another page and it fails... here's the exact situation main file page1.php
3
3531
by: PM | last post by:
I'm trying to make a kind of search history containing the 3 last searched words. So I'm using 3 Session Variables: Word1 / Word2 / Word3. In order to get this history working, I need to put the...
1
2177
by: Bruce W.1 | last post by:
BACKGROUND My ASP.NET app has been running (or rather available) on a server for about 4 months now. The server is at http://www.webhost4life.com/ They don't add cookies to any of the sites...
1
1588
by: .NET Developer | last post by:
I'm having an issue that hopefully someone here can help me out with. First a quick explanation: I'm managing users of my site in a fairly custom way. (in other words I'm not using asp.net's...
0
3202
by: joseph conrad | last post by:
Hi, I tried to implement my own session handler in order to keep control on the process the drawback I foun it is not creating and storing in my cookie the PHPSESSID variable anymore. reading te...
3
3018
by: Jim Carlock | last post by:
I have a problem where session cookies get left inside the temporary folder. Is this a common problem or is there perhaps something I've over looked - there a way to make sure the session...
5
2621
by: PaowZ | last post by:
Hello There! I'm trying to handle sessions using php5, but having some troubles to design it.. Ok, I have to use "session_set_save_handler()" to override session management but I don't really...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.