Session variables and include files

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
Junk Guy
Forum Newbie
Posts: 9
Joined: Thu Jun 20, 2002 11:55 am

Session variables and include files

Post by Junk Guy »

I'm having a bit of a problem, and would greatly appreciate any help offered.

I have a php based architecture that utilizes session variables and a single include file. I have a session_start at the top of every one of my pages, and the include file defined a few variables I used on each page (DB Connection information)...so I also have a 'include "";' line on each page, as well.

The problem comes in when I try to include another file. This is a file that merely holds functions (I'm cleaning up the other pages and placing redundant functions into this new include file). Some of the functions rely on values retrieved from the already existing session variables. However, for some reason, these can't retrieve the values from the session variables.

My page header is laid out as follows:
session_start();
include('constants.inc');
include('functions.inc');

This layout doesn't work, and neither does placing the functions into the constants.inc file after the variables have been defined. I can't retrieve session variables defined by other pages, either.

I have tried modifying the functions.inc file with a similar header as defined above...no dice.

In summary, I can't retrieve session variables with the functions in the functions.inc file.

Any ideas on what's going on?

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

Post by twigletmac »

How do you access the session variables? Do you use the $_SESSION array?

Mac
Junk Guy
Forum Newbie
Posts: 9
Joined: Thu Jun 20, 2002 11:55 am

Post by Junk Guy »

No, I just access the variable itself...

fer instance,
$DBUSER="person";
session_register("DBUSER");
.
.
.
.
(elsewhere, in another file)
$conn = OCILogon($DBUSER...);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you are using PHP version 4.1 up assign the session variables like so:

Code: Select all

<?php
session_start();
// code ...
$_SESSION&#1111;'DBUSER'] = 'person';
// code ...
?>
Then in your functions you can just call $_SESSION['DBUSER'] because the $_SESSION array is autoglobal.

If you are using PHP version 4.0.x register the session variable as you did previously, then in those functions that call on session variables:

Code: Select all

<?php
function myfunc()
&#123;
    global $HTTP_SESSION_VARS;
    $DBUSER = $HTTP_SESSION_VARS&#1111;'DBUSER'];
    // code...
&#125;
?>
Mac
Junk Guy
Forum Newbie
Posts: 9
Joined: Thu Jun 20, 2002 11:55 am

Post by Junk Guy »

I have 4.1.2, so I'm replacing what I have with what you indicated in your first example. It's working, thusfar.

I have one other question, however. Are variables passed via querystring automagically inserted into the $_SESSION[] array, or do I still need to place them in there?

Thank you very much for you help. It is greatly appreciated.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Don't forget you need to put session_start() in any script that needs to access session variables.

As for passing variables through the query string you can access them in a similar way using the $_GET array - you don't need to put them into the $_SESSION array unless they're necessary as session variables.

Mac
Post Reply