[英]Undefined global variable in CodeIgniter
I want to use a global variable in a CodeIgniter Controller, I tried to use the solution from this post Codeigniter global variable within controller but it doesn't work for me. 我想在CodeIgniter控制器中使用全局变量,我试图在控制器中使用本帖子Codeigniter全局变量中的解决方案,但它对我不起作用。 I think because first I call the session
function, and after the user's authentication the user
function, but the web page has been refreshed and the value is lost. 我认为是因为首先调用session
函数,然后在用户身份验证后调用user
函数,但是网页已刷新并且值丢失了。 Is there any to store the variable somewhere after the first's function call and retrieve it later from the second function? 在第一个函数调用之后是否有任何变量可以存储在某个地方,以后再从第二个函数中检索出来?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auth extends CI_Controller
{
//Constructor
function __construct() {
parent::__construct();
}
public $user;
public function session()
{
require 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => '***********',
'secret' => '*****************',
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
$uid = $facebook->getUser();
} else {
// proceed knowing you require user login and/or authentication
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
}
public function user()
{
echo ($user);
}
}
You Can Define Global Constant in constant.php file under the config folder and you can acsees it any where by using that constant 您可以在config文件夹下的constant.php文件中定义全局常量,并且可以使用该常量在任何位置查看它
Like 喜欢
define('MY_VAR','MY_value'); define('MY_VAR','MY_value');
and Now If u want to use it just do LIKE echo MY_VAR; 现在,如果您想使用它,只需执行echo MY_VAR;
当您直接调用函数user()时,PHP将在定义后分配变量,因为该变量未定义,因此该变量不存在。为了引用全局变量,还应使用$ this-> user 。在这种情况下,变量$ user应该是一个会话变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.