简体   繁体   中英

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. 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. 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

Like

define('MY_VAR','MY_value');

and Now If u want to use it just do LIKE echo MY_VAR;

检查链接,我认为它对您有用http://ellislab.com/forums/viewthread/155141/

当您直接调用函数user()时,PHP将在定义后分配变量,因为该变量未定义,因此该变量不存在。为了引用全局变量,还应使用$ this-> user 。在这种情况下,变量$ user应该是一个会话变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM