-1

I made function which will check user's status(logged in, banned, deleted account etc...). In that function system will load some info from MySQL base(name, skin id etc...). Problem is because loaded info isn't available in rest part of code.

NOTICE: UNDEFINED VARIABLE: _HSYNC_SKIN IN C:\PROGRAM FILES\WAMP\WWW\HSYNC\INDEX.PHP ON LINE 79 CALL STACK #TIMEMEMORYFUNCTIONLOCATION 10.0013255112{MAIN}( )..\INDEX.PHP:0 .PNG" ALT="SKIN" CLASS="IMG-THUMBNAIL _HSYNC_SKIN_IMG_COVER">

Below is short code of my code(index.php).

<?php
require('_hsync_scripts/_hsync_pristup.php');
$_hsync_pristup = _hsync_pristup(); // IN '_hsync_pristup()' I LOAD INFOS
// CHECKS (switch)
?>

// HTML CODE

<?php require('_hsync_templates/_hsync_ucp_nav.php'); ?> // HERE I'M USING LOADED INFOS

Function _hsync_pristup() (focus on vars $_hsync_ime and $_hsync_skin)

<?php

function _hsync_pristup()
{
    session_start();
    $_hsync_id = -1;

    if(isset($_COOKIE['_hsync_prijavljen'])) $_hsync_id = $_COOKIE['_hsync_prijavljen']; // PRIJAVLJEN
    else if(isset($_SESSION['_hsync_sess_prijavljen'])) $_hsync_id = $_SESSION['_hsync_sess_prijavljen']; // PRIJAVLJEN
    else return (0); // NIJE PRIJAVLJEN

    require('_hsync_scripts/_hsync_baza.php');

    $_hsync_statment = $_hsync_konekcija->prepare("SELECT Zakljucan, Ime, Skin FROM $_hsync_usr WHERE ID = ?");
    $_hsync_statment->bind_param("i", $_hsync_id);
    $_hsync_statment->execute();
    $_hsync_rezultat = $_hsync_statment->get_result();

    if($_hsync_rezultat->num_rows == 0) return (-1); // OBIRSAN RAČUN

    $_hsync_podatci = $_hsync_rezultat->fetch_assoc();
    if($_hsync_podatci["Zakljucan"] != 0) // ZAKLJUČAN RAČUN
    {
        setcookie("_hsync_zakljucan", $_hsync_id, time() + 8, "/");
        setcookie("_hsync_zakljucan_zap", rand(6, 16), time() + 8, "/");
        return (-2);
    }
    else
    {
        $_hsync_ime = $_hsync_podatci['Ime'];
        $_hsync_skin = $_hsync_podatci['Skin'];
    }

    $_hsync_statment = $_hsync_konekcija->prepare("SELECT ServerID FROM $_hsync_srv_online WHERE ID = ?");
    $_hsync_statment->bind_param("i", $_hsync_id);
    $_hsync_statment->execute();
    $_hsync_rezultat = $_hsync_statment->get_result();

    if($_hsync_rezultat->num_rows != 0) // PRIJAVLJEN NA GAME SERVERU
    {
        $_hsync_podatci = $_hsync_rezultat->fetch_assoc();
        $_hsync_serverid = $_hsync_podatci["ServerID"];

        setcookie("_hsync_online", $_hsync_id, time() + 8, "/");
        setcookie("_hsync_online_id", $_hsync_serverid, time() + 8, "/");
        return (-3);
    }

    if(isset($_COOKIE['_hsync_zabrana'])) return (-4); // IMA ZABRANU PRISTUPA
    else // NEMA ZABRANU
    {
        $_hsync_statment = $_hsync_konekcija->prepare("SELECT ID FROM $_hsync_srv_bnds WHERE ID = ? AND Aktivno = 1"); // TRAŽI DALI JE IMA
        $_hsync_statment->bind_param("i", $_hsync_id);
        $_hsync_statment->execute();
        $_hsync_rezultat = $_hsync_statment->get_result();

        if($_hsync_rezultat->num_rows > 0) // IMA ZABRANU
        {
            setcookie("_hsync_zabrana", $_hsync_id, time() + 31536000, "/");
            return (-4);
        }
    }

    $_hsync_datum = date("d. m. Y.");
    $_hsync_vrijeme = date("H:i:s");

    $_hsync_statment = $_hsync_konekcija->prepare("UPDATE $_hsync_usr SET DatumhSync = ?, VrijemehSync = ? WHERE ID = ?");
    $_hsync_statment->bind_param("ssi", $_hsync_datum, $_hsync_vrijeme, $_hsync_id);
    $_hsync_statment->execute();

    return (1);
}
?>

My question is, how to fix this errors? I Know what I need to do, but I don't understand. I included another file into one file. Every variable is in index.php, why PHP can't see those variables? Should I use $_SESSION to store infos? I checked folder on WAMP server, when system will delete expired sessions' files?

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
Pararera
  • 363
  • 3
  • 15
  • 4
    As much as I love secrets and admire those that can keep them; I think that it would be in the best interest of your question if you could release two secrets for me: The full error message along with the line number and please point out the line number in your code. K, thanx, bai :-) – MonkeyZeus May 27 '16 at 13:40
  • `NOTICE: UNDEFINED VARIABLE: _HSYNC_SKIN IN C:\PROGRAM FILES\WAMP\WWW\HSYNC\INDEX.PHP ON LINE 79 CALL STACK #TIMEMEMORYFUNCTIONLOCATION 10.0013255112{MAIN}( )..\INDEX.PHP:0 .PNG" ALT="SKIN" CLASS="IMG-THUMBNAIL _HSYNC_SKIN_IMG_COVER">` Code is `
    `
    – Pararera May 27 '16 at 13:41
  • 1
    Considering the error, it may be coming from your nav file, not _hsync_pristup. Can you post that as well? – aynber May 27 '16 at 13:44
  • The code you posted in your comment has no PHP code in it; this cannot possibly be the source of error. One more secret I'd like for you to share: edit your question and post **ALL** of the code for `C:\PROGRAM FILES\WAMP\WWW\HSYNC\INDEX.PHP` and point out line # 79. I apologize for not being psychic but if you want help then follow directions. `// HTML CODE` is lovely and all but it does not help you nor anyone trying to help. – MonkeyZeus May 27 '16 at 13:52

2 Answers2

1

The variables like $_hsync_skin are created inside the body of the _hsync_pristup() function. So they are only available inside this function.

Have a look at http://php.net/manual/en/language.variables.scope.php

If you want to access these variables outside of the function, you have two choices : declare them as global, or(better) instead of returning only the error code, return an array containing the error code AND the datas you need.

Something like

$result = array(
    'error_code' => 1,
    '_hsync_skin' => "foobar",
    // etc
);
Armage
  • 657
  • 8
  • 18
0

My guess is that it's a scope issue.

<?php
function foo()
{
    $bar = 'baz';
}
foo();
echo $bar;

This would result in an error. $bar is undefined.

Progrock
  • 7,373
  • 1
  • 19
  • 25