如何检查用户是否已登录,因此调用用户登录php函数时调用函数if(logged_on())并仅显示登录用户的内容:
<?php
if(logged_on()) {
// show profile, edit profile buttons
}
else { // show login button or form }
?>
我已将所有会话变量声明为$ username和$ password等,但我还没有声明login_on()函数。但我还会添加is_admin()函数,以便admin可以访问非用户内容并删除用户和页面,然后使用confirm_admin()函数,这样用户就无法访问delete-users.php等等。
答案 0 :(得分:1)
简单地说 if(isset($ _ SESSION)){...}
否则你可以使用&#34; session_status()&#34;方法。 http://php.net/manual/en/function.session-status.php
如果您想知道是否设置了特定变量: if(isset($ _ SESSION [&#34; isLogged&#34;]){...}
答案 1 :(得分:0)
做这样的事情:
<?php
session_start();
function is_logged() {
if (isset($_SESSION['user_id'])) return $_SESSION['user_id'];
else return false;
}
if (is_logged()) {
$user_id = is_logged();
do_something($user_id);
} else {
if (isset($_POST['submit'])) { //form submitted
//check login and password, if they are correct, do this:
$_SESSION['user_id'] = $user_id_from_database;
//if not correct
unset($_SESSION['user_id']);
header('Location: /'); //refresh page
} else {
//show login form with button named 'submit'
}
}
?>