1

My php code looks like this:

$jsonIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode(file_get_contents("data.config"), TRUE)), RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) 
{

    if($key == "user")
        $user = $val;
    else if($key == "pass")
        $pass = $val;
    else if($key == "server")
        $server = $val;
    else if($key == "data")
        $data = $val;
}

if(!empty($_GET) && isset($_GET['function'])) call_user_func($_GET['function']);
if(!empty($_POST) && isset($_POST['function'])) call_user_func($_POST['function']);

function dbcon()
{
    return new mysqli($server,$user,$pass,$data);
}

But when I call dbcon(), I get undefined variable: user and so forth for the other 3.
Why is this happening and how can I fix it?

Sparky
  • 98,165
  • 25
  • 199
  • 285
dsynkd
  • 1,999
  • 4
  • 26
  • 40
  • Please comment stating the reason for the downvote so I know what I did wrong and will try to not repeat it again. – dsynkd Apr 07 '14 at 16:41
  • 1
    because they not `global` – vp_arth Apr 07 '14 at 16:41
  • Are you including the file that declares and defines your variables? – Brian Driscoll Apr 07 '14 at 16:41
  • I did try global but no use... – dsynkd Apr 07 '14 at 16:41
  • 1
    Try `global $user` inside `dbcon()` function. Also make sure that `$user` is set to some value – Rahil Wazir Apr 07 '14 at 16:42
  • I want to see your `data.config` just for fun... Need it really recursive parsing? – vp_arth Apr 07 '14 at 16:44
  • 1
    If someone ask a question, why downvote him because he forgot something in his code? Is stackoverflow not the place to ask questions, resolve problems and/or correct incorrect code? – Danny Apr 07 '14 at 16:52
  • 1
    @Danny, yes. But SO is _also_ an archive of high quality questions/answers maintained for the benefit of future readers. Down-votes indicate a variety of issues and should never be taken personally. See http://stackoverflow.com/help/why-vote – Sparky Apr 07 '14 at 16:58
  • @Sparky i know, but sometimes ... :-) – Danny Apr 07 '14 at 17:01

1 Answers1

3

You should use global keyword for this:

function dbcon()
{
    global $server;
    global $user; ...
    return new mysqli($server,$user,$pass,$data);
}

But you should not to use global variables
Simple always inject all your dependencies

function dbcon($server,$user,$pass,$data);

for example, or better: function dbcon($options);

Or the best way is the OOP learn and write separate class for incapsulate all of this....

Some offtop.
Replace this:

if(!empty($_GET) && isset($_GET['function'])) call_user_func($_GET['function']);
if(!empty($_POST) && isset($_POST['function'])) call_user_func($_POST['function']);

with followed:

$allowed_foos = array('allowed_foo1', 'allowed_foo2');
if(
   !empty($_REQUEST) && 
   isset($_REQUEST['function']) && 
   in_array($_REQUEST['function'], $allowed_foos)
) 
  call_user_func($_REQUEST['function']);
vp_arth
  • 14,461
  • 4
  • 37
  • 66
  • Bro u gucci! tnx. My problem was that I used global outside of the function instead of inside. – dsynkd Apr 07 '14 at 16:55
  • also, I skip this: `call_user_func($_GET['function'])`; Remove this immediately... If this is really needed, you should create "whitelist" for be sure that params, you get is params you wait... – vp_arth Apr 07 '14 at 16:57
  • How can I make it only call functions that are defined in functions.php? – dsynkd Apr 07 '14 at 16:59
  • I write "whitelist" example – vp_arth Apr 07 '14 at 17:01
  • But that will require me to repeat the names of all the functions... Only if I could write a function that will make the whitelist for me....? – dsynkd Apr 07 '14 at 17:02
  • You should to understand, that you allow to run any php code without it – vp_arth Apr 07 '14 at 17:06