1

I've searched for a day to find a solution for this,But no luck yet, I have a function called : online() this function is inside a file called client.php which is located in root/dir/includes/client.php:

In client.php:

// db connection
include "base.php";

// check if availabe
$available = "false";
$check = mysql_query("SELECT available FROM users ");
while ($row=mysql_fetch_array($check)) {
    if($row['available'] == "yes") {
        $available = "true";
    }
}
// get config
$fetch = mysql_query("SELECT * FROM config ");
$config = mysql_fetch_array($fetch);

// functions
function online() {
// globals
global $available,$config,$path;

// build box

    if($available == "true") {
        ?>
        <div id="online">

        <?
    } else {
            ?><div id="offline"> <?
        }
echo 'client.php is included';
}
}

?>

About client.php:

first it establishes a database connection, then checks if a user is available, if yes : $available = "true"; else: $available = "false";

Then I include client.php in index.php (located in root), So i Have:

in index.php:

$path = "dir/";
include $path . "includes/client.php";

So far so good, everything works,

The problem is...

I need to use this function in other pages in sub directories too, to be more specific, I'm trying to add this function to my wordpress website which is located in: root/wp in my wordpress header I include client.php :

include "../dir/includes/client.php";

And I get the output , So i'm sure it is included, But, none of my global variables work when I open my wordpress (root/wp) resulting in $available = null while it is expected to be "false" as it is defined in client.php

The confusing thing is that, when I echo $available inside my wordpress header I can get the value, but when I echo it inside client.php it is null again, so if I echo both in wordpress header and client.php , when I open my wordpress page, I can see the one I included in header, and the other one inside client.php is null.

Any help would be much appreciated.

Ramtin Gh
  • 1,035
  • 2
  • 11
  • 31
  • Do not use the mysql_* functions anymore. They are deprecated. Switch to mysqli_* functions. – Tobias Golbs Jul 28 '13 at 12:52
  • @TobiasKun Thanks, I'm no expert, should i just replace mysql with mysqli in the whole code? – Ramtin Gh Jul 28 '13 at 12:53
  • Have a look at the [documentation](http://php.net/manual/de/book.mysqli.php) and if there are questions afterwards, ask them here :) – Tobias Golbs Jul 28 '13 at 12:55
  • Was `$available` declared in the global scope? If your include script gets loaded in a function context, then it will reside in that functions context. And later attempts with `global $available` will naturally fail. – mario Jul 28 '13 at 12:58

2 Answers2

1

It may be an issue with a variable scope. Try to use

$GLOBALS['available']

instead of

$available

in your client.php. So it will be defined in global scope. At the definition section also:

$GLOBALS['available'] = "false"; and $GLOBALS['available'] = "true";
KIlya
  • 141
  • 3
0

Your problem is probably the link reference in your include file. You could try: include "/rootfolder/dir/includes/client.php"; instead of include "../dir/includes/client.php"; Also had a problem like this but this fixed it.

Daniel
  • 598
  • 1
  • 6
  • 23