PHP 5.5 and passing session variables to a configuration file.

Hello folks,

   Here is the flow - when a user is validated, I extract their database name from a master database and stuff it into a session variable. Each user has a different database for their own use.
     I then need to pass the session variable to two different configuration files -1) to define variables for the database connection and 2) to define  the database information that the grid will use for creating a datagrid ( I am using phpgrid_lite from
    I have displayed,  in both of the configuration files, the value of the session variable and it is correct. And still in the configuration file for the phpgrid, it tells me it is an undefined session variable.  the database name is to be enclosed by single quotes in both files when being defined.
No matter what I try, I receive the same error message that it is not defined. I do not know what I am doing wrong.
I have posted both configuration files and the screen capture of the error.  I have displayed the value of the session variable in both files and the value is correct. I don't know if it is because of my including session.php or what. But I do know I need to have my session variables  available to every page. which I what I thought I was doing by including the session.php.
I have naturally, filled password and username with * for display purposes and they may not show the same as to length.
Could someone be as so kind to help me?
This is my database configuration file:
// my dbconfig.php
$dbname = $_SESSION['dbname'];
//echo  "\n" . "Am at dbconfig for mysql db" ."\n" ;
//echo "\n";
    define('DB_USER', '*******');
    define('DB_PASSWORD', '*******');
    define('DB_HOST', 'localhost');

Open in new window

This is the phpgrid configuration file:
$dbname = $_SESSION['dbname'];
//echo "\n" . "Am at config for php grid" ."\n";

define('PHPGRID_DB_HOSTNAME', 'localhost'); // database host name
define('PHPGRID_DB_USERNAME', '******');     // database user name
define('PHPGRID_DB_PASSWORD', '*******'); // database password
define('PHPGRID_DB_NAME',$dbname);  // database name
define('PHPGRID_DB_TYPE', 'mysql');  // database type
define('PHPGRID_DB_CHARSET','utf8'); // ex: utf8(for mysql),AL32UTF8 (for oracle), leave blank to use the default charset

// *** You should define SERVER_ROOT manually when use Apache alias directive or IIS virtual directory ***
define('SERVER_ROOT', str_replace(str_replace('\\', '/', realpath($_SERVER['DOCUMENT_ROOT'])),'', str_replace('\\', '/',dirname(__FILE__))));

/******** DO NOT MODIFY ***********/

Open in new window

This is the beginning of the page that is to display the grid:
    require_once ("Includes/dbconfig.php");
   // Create database connection
    $databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if ($databaseConnection->connect_error)
        die("Database selection failed: " . $databaseConnection->connect_error);
    $baderr= "";

// user has click on main menu button
if (isset($_POST['main']))
       header ("Location:suppliermenu.php");


Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

From the errors visible in the JPG it seams your script can not find the location of the include files...Check your file mapping. Aside from that the session variables may not be getting passed correctly. Check that they are being created as you expect them to be passed and also check that they are being passed and available to each function that needs them based on the scope the session variables have.
OverthereAuthor Commented:
Good point. And I agree about the location may a problem. After posting this question, I decided to display the value of the phpgrid database variable and it was correct. I displayed each defined phpgrid variable and they were all correct.
 On each page, it displays the correct value. I noticed too, that the phpgrid config file calls another file, which contains a statements and one in particular, that I think it the problem.

require_once(dirname(__FILE__) .'/conf.php');  

Open in new window

  So, its entirely possible that the "location" is the problem but I do not know how to fix it. I am using IIS 8 Express with WebMatrix 3 and have no virtual directory etc.
Any suggestions?
in your phpgrid configuration file where you declare the session.php include, add "../" at the beginning before "Inlcudes/session.php" in the file path and then run it and post a picture of the result. This should allow the programming to search the location of session.php relative to the location of phpgrid rather than using the absolute pah for inluced which is sending it to "c:\php\inlcudes\" which is a global directory fro includes and I'm guessing not where you have session.php.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Dave BaldwinFixer of ProblemsCommented:
I don't see 'session_start()' in any of your code.  It must come first before you try to access any $_SESSION variables because that is how they are identified.

Also... you appear to have 'mysql' in one place and 'mysqli' in another.  They do not work together, they are completely separate drivers.
Good catch on the MySQL and mysqli Dave. I believe one is from his code and the other is part of phpgrid. As for the session_start() it appears to be getting started in one of the includes at the beginning since he is getting a session already started error, session_start() will be ignored message. See top of JPG.
Dave BaldwinFixer of ProblemsCommented:
Except the error message says that "Includes/session.php" isn't getting loaded.  I can't figure out what the main page is.
Ray PaseurCommented:
A "standard" model for PHP client authentication:

How to use the PHP session:

Database conversions, mapping MySQL, MySQLi and PDO:

I think the first thing I would want to do is make sure that the file and directory paths are correct.  You can do this with simple "hello world" scripts (I like to use phpinfo() for this).  Once you know where everything is located, you will be able to add the other scripts to the application.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OverthereAuthor Commented:
Thank you all for responding, but I see I need to clarify some items, and understand, no offense meant nor given, but some clarification is needed. If it were me, I would want someone to point these things out to me...
And you're right TEKDesign, one statement is from the phpgrid config and the other, my db config.
They are not related.
David, you are as always, willing to help.
And Ray, your articles are enlightening, I am still trying to digest them.

In my pages, I have a include for "sessions.php "- that contains my session_start() command. Exception being my database configuration file.

The  "mysql" in the phpgrid configuration file, refers to the "type" of sql database that I am using.
For each different "type" of sql database, (Oracle, MSSql, MySQL, etc) they have a pre-defined keyword that you must use to represent the "type" of database you are using for the datagrid. Since I am using 'mysql', the keyword that must be used is  'mysql'. Hence,

define('PHPGRID_DB_TYPE', 'mysql');  // database type

Open in new window

The following statement refers to my database connection and has no relation to the phpgrid configuration file...I am simply naming a new database connection.

 $databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

Open in new window

All of that being said, I removed the includes for session.php and replaced it with statement:
session_start() and received better results, It extracted my data, but would not display it in the grid.
boo boo.
I have attached a screen of my latest attempts!
I think this is something that I will have to contact about.
That being said, I am going to keep this question open for a day or two in case you fellas think of something else that would help me. I am a stumped chump...
I certainly do appreciate all of your responses, everyone tries so hard and its appreciated... back to coding...sigh
OverthereAuthor Commented:
Okay fellas, I decided that it was really an issue phpgrid should review etc.
 I am splitting points, I thank all of you for responding. It has been appreciated. :}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.