2015-04-23 38 views
1

我有以下代码确保用户登录。但是我想更改为代码来检查特定用户标识。谁能帮我这个?检查特定用户

function protect_page() { 
    if (logged_in() === false) { 
     header('Location: protected.php'); 
     exit(); 
    } 
} 
+0

我们需要看到函数'logged_in'能够帮助您;) – Marc

+0

或者您可以发回(使用return;)该“logged_in()”函数中的用户ID并检查user_id ==你想要什么 –

+0

是否要将特定用户ID传递给函数? – Marc

回答

0

您可以修改你的函数logged_in并通过特定的用户ID的功能:

function logged_in($id) { 
    //this function checks if the user is logged in and has a specific id 
    return (isset($_SESSION['user_id']) && $_SESSION['user_id'] === $id) ? true : false; 
} 

你必须改变你的protect_page功能,以适应新logged_in功能:

function protect_page() { 
    if (logged_in(7) === false){ 
     header('Location: protected.php'); 
     exit(); 
    } 
} 
2

您可以使用额外的可选变量更新您的登录功能。 如果你没有指定$ user_id变量,它将取值为0,它只会检查用户是否登录。如果你确实指定了某个$ user_id,那么如果用户登录,该函数将返回true; $ user_id与存储在会话中的ID相匹配。

function logged_in($user_id = 0) 
{ 
    return (isset($_SESSION['user_id']) && (($user_id == 0) || ($_SESSION['user_id'] == $user_id))) ? true : false; //this function checks if the user is logged in and matches the given user identifier. 
} 
相关问题