2014-04-01 36 views
0

我試圖保護對我的div class =「posts」的訪問。使用未定義的常量getUser - 假定'getUser'錯誤

我只想看看用戶是否有權限,如果函數getUser存在。有

但I'm這錯誤,我不明白問題出在哪裏:

Notice: Use of undefined constant getUser - assumed 'getUser' in if(function_exists(getUser)) 

我的代碼:

if(function_exists(getUser)) 
     { 

     if(!getUser($_SESSION['user']['id'], '1')) 
     { 
      echo 'Sorry, but you don't have permission to enter here'; 
     } 
     else 
     { 

    ?> 
    <!-- I show this div id he user have permission --> 
    <div class="pages" style="display:block"> 
    .... 
    <?php 
     } 
    } 
    //if the function does not exist 
    else 
    { 
     header('Location: ../index2.php'); 
    } 
    ?> 
    </div> 

我的getUser功能,是一款功能看到用戶的水平:

function getUser($userId, $nivel=NULL){} 

該函數有點誇張,但如果用戶級別有效,它將返回true,如果不可能,則返回false,以便使用userId讀取用戶。

回答

2

你需要用引號括傳遞給function_exists()函數的名稱(單人或雙人):

if(function_exists('getUser')) 

參見手冊中的例子:

<?php 
if (function_exists('imap_open')) { 
    echo "IMAP functions are available.<br />\n"; 
} else { 
    echo "IMAP functions are not available.<br />\n"; 
} 
?> 
+0

謝謝,我是看到其他網站,而不是PHP手冊,他們使用沒有引號。但現在它起作用了!謝謝:) – John23

+0

如果他們抑制錯誤,他們永遠不會看到你得到的警告 –

相關問題