2

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.

4
  • Do not use the mysql_* functions anymore. They are deprecated. Switch to mysqli_* functions. Commented Jul 28, 2013 at 12:52
  • @TobiasKun Thanks, I'm no expert, should i just replace mysql with mysqli in the whole code? Commented Jul 28, 2013 at 12:53
  • Have a look at the documentation and if there are questions afterwards, ask them here :) Commented Jul 28, 2013 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. Commented Jul 28, 2013 at 12:58

2 Answers 2

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";
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried that before, same thing as defining globals inside the function
I mean also at the definition: $GLOBALS['available'] = "false"; and $GLOBALS['available'] = "true";
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.