This is a discussion on problem managing session with cookies and session_set_save_handler() within the PHP Language 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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 ================== |
|
|||
|
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 |
|
|||
|
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 ================== |
|
|||
|
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 |