什么是测试CodeIgniter会话变量的正确方法? [英] What is the proper way to test CodeIgniter session variable?

查看:120
本文介绍了什么是测试CodeIgniter会话变量的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取下面的代码片段。什么是测试以确保会话变量不为空的最好方法?

Take the following code snippet. What is the best way to test to make sure the session variable isn't empty?

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

如果稍后在我的脚本中,我调用以下命令,第一个打印正确,接收消息:未定义的变量:loggedIn

If later in my script, I call the following, the first prints properly, but on the second I receive Message: Undefined variable: loggedIn

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

我试过使用!empty isset ,但两者都不成功。我也尝试使用 if(!($ this-> session-> userdata('userID'))向后执行if / then语句,但没有骰子。

I've tried using !empty and isset, but both have been unsuccessful. I also tried doing the if/then statement backwards using if (!($this->session->userdata('userID')), but no dice. Any thoughts?

推荐答案

尝试执行以下操作:

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

继续,您需要发布更多代码,以便在另一个作用域调用该变量。

If the error continues, you'll need to post more code in case you're calling that variable in another scope.

这篇关于什么是测试CodeIgniter会话变量的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆