一個變量我有檢查,如果用戶有一個劇目中的權利的特權,如果用戶確實,我想它設定新的變量的函數:getprivileges = true
使用條件設置爲True
<?php
$directory = $_SESSION['cwd']; // current directory
$user = $_SESSION['simple_auth']['username']; // get username
$repository = gatorconf::get('repository'); // get base repertory of the file manager
$userdir = $repository.DS.'user'.DS.$user; // user's repertory
function scanDirectory($userdir = '', $directory){
$folders = glob($userdir . '/*' , GLOB_ONLYDIR);
foreach($folders as $folder){
if (($folder == $directory && gator::checkPermissions('r')) || (gator::checkPermissions('ru')) || ($userdir == $directory && gator::checkPermissions('r'))) {
$getprivileges = true;
break;
}
scanDirectory($folder, $directory);
}
}
scanDirectory($userdir, $directory);
?>
<?php if ($getprivileges == true): ?>
<p>Permissions granted.<p>
<?php endif; ?>
我相信功能return
是我正在尋找,但我真的不知道如何使用它。 代碼的最後部分僅僅是我打算對變量做的一個例子。
我添加了,到目前爲止我的變量$ getprivileges沒有在我的代碼中的其他地方定義,所以也許我必須先給它一個默認值?
有人可以給我一些解釋我如何做到這一點?
非常感謝
解決方案更新從註釋:
<?php
$directory = $_SESSION['cwd']; // current directory
$user = $_SESSION['simple_auth']['username']; // get username
$repository = gatorconf::get('repository'); // get base repertory of the file manager
$userdir = $repository.DS.'user'.DS.$user; // user's repertory
$getprivileges = scanDirectory($userdir, $directory);
function scanDirectory($userdir = '', $directory){
$folders = glob($userdir . '/*' , GLOB_ONLYDIR);
foreach($folders as $folder){
if (($folder == $directory && gator::checkPermissions('r')) || (gator::checkPermissions('ru')) || ($userdir == $directory && gator::checkPermissions('r'))) {
return true;
break;
}
scanDirectory($folder, $directory);
}
}
scanDirectory($userdir, $directory);
?>
<?php if ($getprivileges == true): ?>
<p>Permissions granted.<p>
<?php endif; ?>
開始,'$ getprivileges'超出範圍。 # – ScarletMerlin
'return true;'或在最後'返回false;'然後調用'$ getprivileges = scanDirectory($ userdir,$ directory);' – AbraCadaver
將'scanDirectory($ userdir,$ directory);'設置爲一個變量。然後讓函數返回true或false。然後檢查該變量(或者只是函數調用)。也應該看看http://php.net/manual/en/language.variables.scope.php。注意本地和全局範圍。 – chris85