0

I am new to PHP and having trouble using Global variables. I have a global array called profile. However, when I declare access the contents of a specific index of profile, 'authorized', the value does not change outside of the function is was set in. I fear that it might be due to the fact that the 'authorized' index was not declared in the array when the array was declared. However, there are instances of this occurring with other indexes so I'm not really sure. Any help would be really helpful!

Declaration of profile:

/**
 * User profile
 * @name $profile
 * @global array $GLOBALS['profile']
 */
$GLOBALS['profile'] = array(
    # Basic Config - Required
'username' => 'tom',
'x' => 'filler',
'y' => 'filler',
'salt' => 'filler',

    # Optional Config - Please see README before setting these
#   'microid'   =>  array('mailto:user@site', 'http://delegator'),
#   'pavatar'   =>  'http://your.site.com/path/pavatar.img',

    # Advanced Config - Please see README before setting these
    'allow_gmp' =>  true,
#   'allow_test'    =>  false,
#   'allow_suhosin' =>  false,
#   'auth_realm'    =>  'phpMyID',
#   'force_bigmath' =>  false,
#   'idp_url'   =>  'http://192.168.1.5/MyID.config.php',
#   'lifetime'  =>  1440,
#   'paranoid'  =>  false, # EXPERIMENTAL

    # Debug Config - Please see README before setting these
#   'debug'     =>  false,
#   'logfile'   =>  '/tmp/phpMyID.debug.log',
);

Function where profile['authorized'] is set:

/**
 *Handles the validation of the signature from the user
 */
function validate_mode()
{
global $profile;

user_session();


$qString = strtoupper('abc');
$pString = strtoupper('abc');
$gString = '10';
$aString = strtoupper($_POST['a']);
$zString = strtoupper($_POST['z']);
$yString = strtoupper($_POST['y']);
$cString = $_POST['c'];
$c = gmp_init($cString, 16);
//echo$cString;

$q1 = gmp_init($qString, 16);
$p1 = gmp_init($pString, 16);
$g1 = gmp_init($gString, 16);
$a = gmp_init($aString, 16);
$z = gmp_init($zString, 16);
//$hex_y = base64_decode(yString);
$y = gmp_init($yString, 16);
//echo $yString;

$hash = sha1($pString,false);

$hash = $hash . sha1($qString,false);
$hash = $hash . sha1($gString,false);
$hash = $hash . sha1($yString,false);
$hash = $hash . sha1($cString,false);
$hash = $hash . sha1($aString,false);
$full_hash =sha1($hash,false);

//echo $full_hash;
if (gmp_cmp($z, $q1) < 0)
    {
        $temp_ans = gmp_powm($a, $q1, $p1);
        if (gmp_cmp($temp_ans, 1) == 0)
        {

                $this_hash = gmp_init($full_hash, 16);
                $temp_pow = gmp_powm($g1,$z,$p1);
                $temp_inner =gmp_powm($y,$this_hash, $p1);
                $temp_mid = gmp_mul($a, $temp_inner);
                $temp_pow2 = gmp_mod($temp_mid, $p1);

                if (gmp_cmp($temp_pow, $temp_pow2) == 0)
                {   
                    $compare = strcmp($cString,$_SESSION['challenge']);
                    if ($compare == 0)
                    {
                        $_SESSION['auth_url'] = $profile['idp_url'];

                        $profile['authorized'] = true;
                        // return to the refresh url if they get in
                        wrap_redirect($profile['idp_url']."?openid.mode=id_res");
                    }//ends the $compare == 0;
                    else 
                        echo "The session stored challenge value does not match the one supplied"."\n";
                }//ends the gmp_cmp if statement.
                else
                    echo"g^z mod p doesn't equal a*y^c mod p"."\n";
        }//ends the if($temp_ans ==1)
        else
            echo"a^q  mod p does not equal 1 so we exited"."\n";
    }//ends the if comparing of z and q
    else
        echo"Z is greater than Q so it is exiting"."<br>";
}//ends the validate mode

The function where profile['authorized'] is accessed and the value has not changed:

function id_res_mode () {
    global $profile;
echo "authorization".$profile['authorized'];
    user_session();

    if ($profile['authorized'])
        wrap_html('You are logged in as ' . $_SESSION['username']);

    wrap_html('You are not logged in');
}
3
  • I thought you could access the global variables in php in two ways $GLOBALS["profile"] and if you put the keyword global $profile in the beginning of the function. At least thats what in the PHP Manual here: php.net/manual/en/language.variables.scope.php Commented May 13, 2012 at 22:20
  • That comment was in response to someone else's who I guess deleted theirs. Commented May 13, 2012 at 22:21
  • have you included the declaration file in each of these code? Commented May 13, 2012 at 22:29

1 Answer 1

1

You didn't declare authorized in the array. Just FYI, for next time you're debugging, you would have gotten an "undefined index" warning if you had error reporting for warnings turned on.

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.