Forum Moderators: coopster

Message Too Old, No Replies

Session not incrementing

I wonder why?

         

Habtom

7:17 am on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code below is meant to rotate a banner by picking it from a database. I have found out that the session variable gets a certain number and stucks there and refuses to increment. Any ideas. Thanks.

<?php
$countbanner = "SELECT count(*) FROM banners";
$counterquery = mysql_query($countbanner);
$displaycounter = mysql_fetch_array($counterquery);
$number_of_banners = $displaycounter[0];

if (empty($_SESSION['current_banner'])) {
$_SESSION['current_banner'] = '1'; }

$SQL = "SELECT image, link FROM banners WHERE id = '". $_SESSION['current_banner'] ."'";
$displayquery = mysql_query($SQL);
$displayresult = mysql_fetch_array($displayquery);

if (!empty($displayresult[0]))
{
echo '<a href="';
echo $displayresult[1];
echo '">';
echo '<img src = "images/banner_images/';
echo $displayresult[0];
echo '" border="0">';
echo '</a>';
}

$_SESSION['current_banner']++ ;

if (($_SESSION['current_banner']) > ($number_of_banners)) {
$_SESSION['current_banner'] = '1'; }

?>

Habtom

mcibor

9:52 am on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What number? Is it 1?

you should start your code with
<?php
session_start();
...

your code looks fine to me.
Michal Cibor

ergophobe

7:33 pm on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



set error_reporting to E_ALL - this will tell you if you have any undefined variables.

As Michal said, if you're not starting your session, that would be one case.

If your first query is getting a count of zero for $displaycounter, then you would also be getting set to 1 every time, so you might want to test

if (!empty($number_of_banners) && $_SESSION['current_banner'] > $number_of_banners) {
$_SESSION['current_banner'] = '1'; }

mattx17

8:08 pm on Jul 7, 2005 (gmt 0)

10+ Year Member



The problem is you can't set session variables after you've sent output to the browser. Put your session increment before your echo statements.

ergophobe

8:44 pm on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In fact, I often have an append that does session clean up after the full output.

You cannot *start* a session after output is sent, because starting a session involves at least an attempt to set or access a cookie, which involves sending HTTP headers, which must precede all output. You can, however, increment a session variable or otherwise manipulate it, as this does not involve any HTTP headers..

Habtom

8:10 am on Jul 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks!

vincevincevince

9:01 am on Jul 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot *start* a session after output is sent

Just stick ob_start(); on line 1