|
|
Subject:
PHP Sessions - how to reset?
Category: Computers > Programming Asked by: g8z-ga List Price: $10.00 |
Posted:
10 Jan 2003 20:37 PST
Expires: 09 Feb 2003 20:37 PST Question ID: 141512 |
Hello, When I send output to the browser in PHP, e.g. print "hello world"; This basically starts a session in PHP. If, after printing Hello World, I have something like this: session_start(); Then I get the following error message: Warning: Cannot send session cache limiter - headers already sent... I would like to know if there is some way to "reset" the session somehow so that I can still use PHP's session handling functions even after sending output to the browser. I've been using PHP for about 4 years, and have used sessions for about 2 years, although in the past I have always been able to get around this problem by putting session handling functions before any browser output. At the moment, however, I'm faced with a scenario where I cannot do this. Thanks, Darren |
|
Subject:
Re: PHP Sessions - how to reset?
Answered By: webadept-ga on 11 Jan 2003 00:22 PST Rated: |
Hi, What the commentor suggest below will work, but there is a slightly better way, and one I use quite a bit. The ob_start() and ob_end_flush() http://www.php.net/manual/en/function.ob-start.php http://www.php.net/manual/en/function.ob-end-flush.php example: <?PHP ob_start(); // Stop Header Action // if($ad=="google"){Redirect("add_page.html",0); exit;} print "hello world"; ob_end_flush(); // Let Header Happen // ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html dir="LTR"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> ... and what ever else you want to do here with the page. You can build all of your needed output variables inside those two, and when you are done, the flush sends the output to the browser. Thanks, webadept-ga | |
| |
|
g8z-ga
rated this answer:
Excellent answer + followup. Thanks for the sample code. |
|
Subject:
Re: PHP Sessions - how to reset?
From: dewolfe001-ga on 10 Jan 2003 22:49 PST |
You are limited to sending cookie information BEFORE you send a header. But, your HTML output can call other pages and images. Those can start a session. They can also look for a marker so that they don't restart a session. This is an example of an image call an a start of a session: <img src="gglimg.php?img=mug-b.jpg&hdr=jpg"> with the PHP script looking like this: <? session_start(); header("Content-type: image/".$hdr); include($img); $name="Hello There!"; session_register("name"); ?> This will set a session tied to the client. Only scripts that call session_start will be able to read what goes into these session variables, so that can't help a script that needs to write the session_start AFTER the header is written. In addition, client side means like JavaScript and Flash can set cookies. If you can read and pass the session name to the client, JS and Flash _could_ look back to the server to read the cookie contents. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |