Variables From My Include File Are Being Ignored

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frazzle
    New Member
    • Dec 2006
    • 7

    Variables From My Include File Are Being Ignored

    I have been developing a system in PHP that uses MySQL. To that end, I have used a config file to hold high level fixed variables (database name, user id, password, etc) and I using "require_on ce" to get it into my code with no problems.

    The problem arises from my desire to integrate third party software, such as Wordpress, External Calendar, etc, into my site and have one central location for database configuration.

    I can successfully use "require_on ce" in WP's or X2's code but when I refer to variables set in my include file, they seem to be null, which is puzzling. This is a snippet from my include file:

    [php]
    <?

    $gs_database = "linwoods_onlin edems";
    $gs_username = "linwoods_admin ";
    $gs_password = "plush32331 ";
    ?>
    [/php]

    That's not all in the file but is the relevant part. I set about a dozen variables and I can successfully refer to them in my code. Here is an example from X2:

    [php]
    <?php
    require_once ('http://'.$_SERVER['HTTP_HOST'].'/lcs_config.php' );
    // ExtCalendar configuration file

    // DB configuration
    $CONFIG['dbsystem'] = "mysql"; // Your database system
    $CONFIG['dbserver'] = "localhost" ; // Your database server
    $CONFIG['dbuser'] = $gs_username; // Your db username
    $CONFIG['dbpass'] = $gs_password; // Your db password
    $CONFIG['dbname'] = $gs_database; // Your database name

    echo "|".$gs_usernam e."|";

    ?>

    [/php]

    It finds the file alright because if I change the file name, it breaks. The problem is that the variable I'm echoing is NULL. This is happening in three different external applications I'm trying to integrate so I'm stumped as to why this is happening. I've been on google for the last 2 hours with no success.

    Any ideas?

    Thanks!
    Paul
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Could be several reasons for this, one of them that these vars are not globally defined and are used in functions, from where they are not addressable.

    Have you considered using definitions, i.e. constants. You could then define your setup along the following lines:
    [php]
    define('SQL_SYS ', 'mysys');
    define('SQL_HOS T', 'myhost');
    define('SQL_USE R', 'myuser');
    define('SQL_PAS S', 'mypassw');
    define('SQL_DB' , 'mydb');[/php]
    and setup you CONFIG array as follows:
    [php]
    $CONFIG['dbsystem'] = SQL_SYS;
    $CONFIG['dbserver'] = SQL_HOST;
    $CONFIG['dbuser'] = SQL_USER;
    $CONFIG['dbpass'] = SQL_PASS;
    $CONFIG['dbname'] = SQL_DB;
    [/php]

    Ronald :cool:

    Comment

    • frazzle
      New Member
      • Dec 2006
      • 7

      #3
      I changed to definitions with no success. I found that you can't register definitions as globals so that didn't work and I went back to variables and defined them as global in my include file.

      That also didn't work.

      It's not a show stopper, but it's frustrating that I have to define my database in 4-5 places and once I install this application in multiple places, I'll have to have a checklist to configure the system.



      Originally posted by ronverdonk
      Could be several reasons for this, one of them that these vars are not globally defined and are used in functions, from where they are not addressable.

      Have you considered using definitions, i.e. constants. You could then define your setup along the following lines:
      [php]
      define('SQL_SYS ', 'mysys');
      define('SQL_HOS T', 'myhost');
      define('SQL_USE R', 'myuser');
      define('SQL_PAS S', 'mypassw');
      define('SQL_DB' , 'mydb');[/php]
      and setup you CONFIG array as follows:
      [php]
      $CONFIG['dbsystem'] = SQL_SYS;
      $CONFIG['dbserver'] = SQL_HOST;
      $CONFIG['dbuser'] = SQL_USER;
      $CONFIG['dbpass'] = SQL_PASS;
      $CONFIG['dbname'] = SQL_DB;
      [/php]

      Ronald :cool:

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        When the $_SESSION array is available in all scripts you could use that. At least they are global.
        But I still am, like you, also puzzled why the variables should be NULL.

        Ronald :cool:

        Comment

        • frazzle
          New Member
          • Dec 2006
          • 7

          #5
          Solved!

          After a few (more) hours of experimentation , and by a lucky chance, I found the solution to this problem.

          I was using [php]require_once('h ttp://'.$_SERVER['HTTP_HOST'].'/lcs_config.php' );[/php] in my script and it was pointing properly to the correct location. I knew this not only by displaying the value, but when I deliberately misspelled the filename it failed.

          What does work is:

          [php]require_once('. ./lcs_config.php' );[/php]

          This MUST be some sort of bug in PHP, but as long as it works and I don't spend anymore time I don't have on this, that's fine with me.

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            I am glad you found the solution to your problem yourself.

            Ronald :cool:

            Comment

            Working...