1

I try to define variable and then call it in my function, and then i call the function in another file. So, the variables outside the function work properly, but when i call them in function they don't work. Here's the code.

$info = new ServerInfo($server->GetServerData('IPAdress'));
$id = $server->GetServerData('ID');
$sshhost = $server->GetServerData('SSHHOST');
$sshport = $server->GetServerData('SSHPORT');
$sshuser = $server->GetServerData('SSHUSER');
$sshpw = base64_decode(base64_decode($server->GetServerData('SSHPW')));
$port = $server->GetServerData('PORT');

/* Start Server Function */
function start_server($sshhost, $sshport, $sshuser, $sshpw){

global $sshhost;
global $sshport;
global $sshuser;
global $sshpw;
global $id;
global $port;

if (!function_exists("ssh2_connect")) return "SSH2 PHP extenzija nije instalirana";

if(!($con = ssh2_connect($sshhost, $sshport))){
    return "Ne mogu se spojiti na server";
} else {

    if(!ssh2_auth_password($con, $sshuser, $sshpw)) {
        return "Netačni podatci za prijavu";
    } else {

        $stream = ssh2_shell($con, 'vt102', null, 80, 24, SSH2_TERM_UNIT_CHARS);
        fwrite( $stream, "cd /home/cs && screen -A -m -S srv".$id. " ./hlds_run -console -game cstrike +port " .$port. " +map de_dust2 +maxplayers 32 -pingboost 1".PHP_EOL);
        sleep(1);
        echo "Server Startovan";

        return TRUE;

    }
}   
}
2
  • 5
    You're passing them as arguments and declaring them global. Do one or the other. (Preferably, as arguments.) Commented Aug 29, 2018 at 16:35
  • 5
    Might have something to do with the fact that you have the function parameters named the same as the variables. Try not using Global variables if you can. Commented Aug 29, 2018 at 16:35

1 Answer 1

2

You can declare them using $GLOBALS['variablename'] then still use the $variablename in other function. ref: https://www.w3schools.com/php/php_superglobals.asp How to declare a global variable in php?

function start_server($sshhost, $sshport, $sshuser, $sshpw){

$GLOBALS['sshhost'] = $sshhost;
$GLOBALS['sshport'] = $sshport;
$GLOBALS['sshuser'] = $sshuser;
$GLOBALS['sshpw'] = $sshpw;

/* do more stuff */
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.