views:

43

answers:

1

Hi. I can't understand why this don't work. First i have a function like this where the user name shows up:

if(isset($_POST['Commit'])){

if(empty($_POST['crime'])){
echo "You didn't select the type of crime you wish to do.";
}else{

   (...lots of code)

$name = $_SESSION['username'];`

Then I have another function where the username shows up blank:

if(isset($_POST['action'])){

    (...)

if(empty($_POST['car'])){
echo "You didn't select a car.";
}else{


if($row['owner'] != $_SESSION['username']){
echo "This isn't your car.";
}else{

There is allot of code that i didn't post, but you guys get the idea. Why does this happen? i thought $_SESSION was global and always available

My main php file look something like this:

require("php functions\page_functions.php"); 
require("php functions\gta_functions.php");

session_start();

class gtapage extends Page
{

        public function display()
        {

        displayGta();
        }
}

where displayGta(); is the function from my first post

+4  A: 

Has session_start() been called in the script that's using the second function? $_SESSION is only available on pages in which a session has been opened.

ceejayoz
Updated my first post now. The main page has a session_start(); then it calls the displayGta function that has both of the functions that calls the $_SESSION['username'], but it only work in one of them...
ganjan
What does `print_r($_SESSION)` display in the second function?
ceejayoz
nothing, but I do get this warning:Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampplite\htdocs\Drug Empire\php functions\gta_functions.php:346) in C:\xampplite\htdocs\Drug Empire\gta.php on line 12
ganjan
`print_r($_SESSION)` can't generate that warning unless it's executing before `session_start()`. Try moving `session_start()` above your includes.
ceejayoz
IT WORKS! Thanks ceejayoz :D
ganjan