2012-05-13 36 views
0

我是PHP新手,無法使用全局變量。我有一個名爲profile的全局數組。但是,當我聲明訪問特定的配置文件索引的內容時,「授權」的值不會在功能設置之外發生變化。我擔心這可能是由於「授權」索引爲在聲明數組時聲明不在數組中聲明。但是,其他索引也存在這種情況,所以我不太確定。任何幫助將非常有幫助!PHP中的全局變量沒有正確設置

宣言輪廓:

/** 
* 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:[email protected]', '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', 
); 

函數,其中簡檔[ '授權']被設定:

/** 
*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 

的函數,其中簡檔[ '授權']被訪問並且該值未改變:

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'); 
} 
+0

我以爲你可以通過兩種方式在$ php中訪問全局變量'$ GLOBALS [「profile」]',如果你在關鍵字'global $ profile'中放入函數的開頭。至少這就是PHP手冊中的內容:http://php.net/manual/en/language.variables.scope.php – tpar44

+0

該評論是爲了迴應我認爲刪除他人的其他人。 – tpar44

+0

你是否在這些代碼中包含了聲明文件? –

回答

1

您沒有申報授權在陣列中。僅供參考,下次您正在調試時,如果您在啓用警告時發生了錯誤報告,則會收到「未定義索引」警告。