English 中文(简体)
测试 CodeIgniter 会话变量的适当方式是什么?
原标题:What is the proper way to test CodeIgniter session variable?

选择下面的代码片断。 什么样的最佳测试方法才能确保会话变量不是空的?

<?php if ($this->session->userdata( userID )) {
   $loggedIn = 1;
}
else {
   $loggedIn = 0;
} ?>

如果我的脚本稍晚一些时,我打给以下的首个指纹,这是正确的,但第二我收到Message:未定义变量:登录到

<?php echo $this->session->userdata( userID ));
      echo $loggedIn; ?>

我尝试过使用 ! noty isset , 但都失败了。 我还尝试使用 if (! ($This- gt;session- & gt;userdata (userID)) (userdiceID)) , 但没有找到。 有什么想法吗?

最佳回答

尝试以下列方式代替:

<?php 
$loggedIn = 0;
if ($this->session->userdata( userID ) !== FALSE) {
   $loggedIn = 1;
}
?>

如果错误继续, 您需要发布更多代码, 以防您在另一个范围重新调用该变量 。

问题回答

如果您的目标是要查看会话变量 userID 是否设置, 那么以下内容应该有效 :

$this->session->userdata( userID ) !== false

您为什么不在您的会话中创建一个名为_ logged_ in 的布林域, 然后检查一下 :

if(false !== $this->session->userdata( is_logged_in ))
if($this->session->userdata( is_logged_in )) {
    //then condition
}

这是测试的正确方法!





相关问题
why the session in iis automatically log out?

I used iis6, and when i called a function Directory.delete(), the all the session relate this website will be logged out. and i debugged the website, i found no exception. any one have ideas on this ? ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Can I get the size of a Session object in bytes in c#?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it? I want to get the size of a particular Session object, such as Session["table1"], ...

提供严格分类的出席会议物体

提供严格分类的与会机会的最佳方式是什么? 我正计划转而选择矩阵,这正在促使汇编者抱怨我的幻觉方案拟订方法......

PHP Session is not destroying after user logout

I m trying to create an authentication mechanism for my PHP Application and I m having difficulty destroying the session. I ve tried unsetting the authentication token which was previously set within ...

热门标签