Google Answers Logo
View Question
 
Q: PHP Sessions - how to reset? ( Answered 5 out of 5 stars,   1 Comment )
Question  
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
Answer  
Subject: Re: PHP Sessions - how to reset?
Answered By: webadept-ga on 11 Jan 2003 00:22 PST
Rated:5 out of 5 stars
 
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

Request for Answer Clarification by g8z-ga on 11 Jan 2003 00:56 PST
Can you give me an example using session functions? e.g.

<html>
<h1>My Title</h1>
Blah blah blah...

<? 
// start session & set some session variables here
?>

<h2>Subtitle</h2>
Blah blah blah...

Where would ob_start() and ob_end_flush() go in this example?

Clarification of Answer by webadept-ga on 11 Jan 2003 02:41 PST
Hi,

In this example I am a loading several environment variables from
different configuration files in the header and then displaying those
values in the HTML below.  This is a page that is actually working
inside of the code is good and hopefully it will give you a good idea
of how to use those two functions.  If your pages are going to rely
heavily on configuration files and changing database input, it is
generally a good idea to do most of your processing in the headers. 
The reason I prefer to do it this way is for maintenance later. 
Instead of having to go through the code inside the HTML areas, all of
the code and the variable settings are down the up in the top, so I
know where to find them later when things need to be changed.

Of course there's always more than one way to do something, some
programmers prefer the method shown below, had in the dynamic code
buried inside the HTML, I find that method easier to write, but a bit
harder to maintenance.  But again, it's a matter of preference, and
with PHP there's definitely room for preference.

<?PHP
ob_start();  // Stop Header Action //
// first level page
include ($DOCUMENT_ROOT . "config.php");
include $BOTTOM_MENU;
include $SEO_MAIN_MENU;



ob_end_flush(); // Let Header Happen // 
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Search Engine Optimization</title>
<META NAME="ROBOTS" CONTENT="INDEX,FOLLOW">



<!-- script type="text/javascript" language="javascript"
src="http://rue/web/actonvision/scripts/1k.js"></script -->

<script language="JavaScript">
<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if
((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight;
onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW ||
innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
// -->
</script>




<style type="text/css">
	@import " <?=$DOMAIN; ?>/style/style.css"; /* basic formatting */
        	

</style>

</head>

<body>


<div id="banner">
<img src="<?=$TOPLOGO; ?>">
</div>

<!-- Main Menu System Top Bar -->
<? print_menu($MAINDIR); ?>

<!-- end Top Bar Menu System -->





<div id="leftcontent">

<!-- left Side Menu area -->
<? include_once($SIDEBARS."/left/main_left.inc"); ?>


</div>
<!--  Center Content Area -->
<div id="centercontent">

<!-- Begin Story -->

<h1>Website Content</h1>
<center>
<h2> Page Relevance for Search Engine Optimization</h2>
</center>

<p>
Lots of wise words here
</p>
<p>
Read <a href="<?=$DOMAIN;?>/stories/seo_relevance.html">More Here</a>
</p>
<HR>


<!-- End Story -->

<!-- Begin Story -->

<h1>Search Engines</h1>
<center>
<h2> What Search Engines to pay attention too</h2>
</center>

<p>
Even more wise words here
</p>
<p>
Read <a href="<?=$DOMAIN;?>/stories/seo_attention.html">More Here</a>
</p>
<HR>


<!-- End Story -->



<!-- Begin Story -->

<h1>Website Design</h1>
<center>
<h2>Web Design Basics -- Clean and Clear </h2>
</center>

<p>
Yes yes, you got it.. wisdom overflows like rain in wooden barrles
under gargoyles.
</p>
<p>
Read <a href="<?=$DOMAIN;?>/stories/seo_basics.html">More Here</a>
</p>
<HR>


<!-- End Story -->



</div>
<!-- End the Center Content Area -->






<div id="rightcontent">
<? include_once($SIDEBARS."/right/affiliate_bar.inc"); ?>

</div>





<? print_bottom_menu($MAINDIR); ?>






</body>
</html>
g8z-ga rated this answer:5 out of 5 stars
Excellent answer + followup. Thanks for the sample code.

Comments  
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.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy