wraping code that inludes SESSIONS in a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

wraping code that inludes SESSIONS in a function

Post by deejay »

Hi

I have written code that works fine that I would now like to wrap into a function. Inside the function i have added the line
global $HTTP_SESSION_VARS;
the whole function now looks like this

Code: Select all

<?php
function adDisplay()
{
      global $HTTP_SESSION_VARS;

      $business_group = $_SESSION['business_group'];

       if($business_group == "retail"){   //open local
       echo '<center><img src="../images/indexPageAds/retailStoreMemoCam.gif" width="135" height="178"></center> ';
             }     //close local

       elseif($business_group == "office"){     //open local
       echo '<center><img src="../../images/indexPageAds/officeMemoCam.gif"></center>';
             }      //close local
     elseif($business_group == "warehouse_factory"){ //open local
     echo '<center><img src="../../images/indexPageAds/warehouseFactory.gif"></center>';
             }   //close local
     elseif($business_group == "school"){   //open local
      echo '<center><img src="../../images/indexPageAds/schoolsMemoCam.gif"></center>';
              }    //close local
     elseif($business_group == "hospital"){    //open local
     echo '<center><img src="../../images/indexPageAds/hospitalsMemoCam.gif"></center>';
             }    //close local

     elseif($business_group == "night_club"){  //open local
     echo '<center><img src="../../images/indexPageAds/nightclubHHMDs.gif"></center>';
             }     //close local
     elseif($business_group == "taxis_delivery_vehicles"){  //open local
      echo '<center><img src="../../images/indexPageAds/deliveryVehicles.gif"></center>';
             }    //close local
     elseif($business_group == "vending_machines"){   //open local
      echo '<center><img src="../../images/indexPageAds/vendingMachinesMemoCam.gif"></center>';
            }     // close local
     else  //open local
     echo '';
            }
?>
Can anyone see what I am missing. The fact it works outside of the function I take it must be something simple I've missed. Thanks for any help you may be able to give.

Deej
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You don't need this line:

Code: Select all

global $HTTP_SESSION_VARS;
and have you got a call to session_start() at the top of the page in which you would like to include this function?

Mac
User avatar
Ebula
Forum Newbie
Posts: 15
Joined: Wed Mar 19, 2003 7:21 am
Location: Hamburg, Germany

Post by Ebula »

Just a quick not: In view of futur PHP versions I would use $_SESSION instead of $HTTP_SESSION_VARS. Check the phpmanual about Predefined Variables.
Maybe this also solves the prob....
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Ebula wrote:Just a quick not: In view of futur PHP versions I would use $_SESSION instead of $HTTP_SESSION_VARS.
But he is using $_SESSION - he just declares $HTTP_SESSION_VARS global which is why the line is not necessary.

Mac
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Post by deejay »

Hi

yeah i had originally checked it without that line, but i read in a post that pre 4.1 (im on 4.0) should set SESSIONS in a global.

at the top of the page i run the check

Code: Select all

<?php
echo '$_SESSION[business_group] equals '.$_SESSION['business_group']; 
?>
which works fine. and because this code does what it should on the page outside of the function -

Code: Select all

<?php

$business_group == $_SESSION['business_group'];

if($business_group == "retail"){   //open local
echo '<center><img src="../images/indexPageAds/retailStoreMemoCam.gif" width="135" height="178"></center> ';
}     //close local

     elseif($business_group == "office"){     //open local
echo '<center><img src="../../images/indexPageAds/officeMemoCam.gif"></center>';
}      //close local
     elseif($business_group == "warehouse_factory"){ //open local
echo '<center><img src="../../images/indexPageAds/warehouseFactory.gif"></center>';
}   //close local
     elseif($business_group == "school"){   //open local
echo '<center><img src="../../images/indexPageAds/schoolsMemoCam.gif"></center>';
}    //close local
     elseif($business_group == "hospital"){    //open local
echo '<center><img src="../../images/indexPageAds/hospitalsMemoCam.gif"></center>';
}    //close local

     elseif($business_group == "night_club"){  //open local
echo '<center><img src="../../images/indexPageAds/nightclubHHMDs.gif"></center>';
}     //close local
     elseif($business_group == "taxis_delivery_vehicles"){  //open local
echo '<center><img src="../../images/indexPageAds/deliveryVehicles.gif"></center>';
}    //close local
     elseif($business_group == "vending_machines"){   //open local
echo '<center><img src="../../images/indexPageAds/vendingMachinesMemoCam.gif"></center>';
}     // close local
     else  //open local
echo '';

?>
I guess it must be because the value of $_SESSION[business_group] has been lost somewhere when putting the code inside a function.

:?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What do you get if you put:

Code: Select all

echo '<pre>';
print_r($_SESSION);
echo '</pre>';
in the function?

Mac
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Post by deejay »

it printed
Array
(
[username] => deej5
[business_group] => hospital
)
and now the rest of the function works! so i took away the code and the function still works :? why i didn't before I have no idea.

but i have it working now so thanks for that :)
Post Reply