CodeIgniter Forums
Differences in session variable declaration - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Differences in session variable declaration (/showthread.php?tid=71679)

Pages: 1 2


Differences in session variable declaration - n2fole00 - 09-12-2018

Hi, I have the following hook

PHP Code:
class LanguageLoader
{
 
 function initialize() {
 
   $ci =& get_instance();
 
   $ci->load->helper('language');

 
   $site_lang $_SESSION['site_lang']; 
 
   //$site_lang = $ci->session->site_lang;
 
   if ($site_lang) {
 
       $ci->lang->load('general'$site_lang);
 
   } else {
 
       $ci->lang->load('general''english');
 
   }
 
 }



With the line
PHP Code:
$site_lang $_SESSION['site_lang']; 
I get the following error

PHP Code:
A PHP Error was encountered
Severity
Notice

Message
Undefined indexsite_lang

Filename
hooks/LanguageLoader.php

Line Number
12

Backtrace
:

FileC:\xampp\htdocs\availability-calendar\application\hooks\LanguageLoader.php
Line
12
Function: _error_handler

File
C:\xampp\htdocs\availability-calendar\index.php
Line
315
Function: require_once 

but replacing it with
PHP Code:
$site_lang $ci->session->site_lang
resolves the problem. I'm curious to know what the issue was though. Thanks.


RE: Differences in session variable declaration - InsiteFX - 09-12-2018

When using:
PHP Code:
$CI =& get_instance(); 

Your getting CodeIgniter's Controller Class.

Are you loading the Session library in the Controller or are you Autoloading it?


RE: Differences in session variable declaration - php_rocs - 09-12-2018

@n2fole00

It could be what value is returned between the two. You could do a var_dump on both values and see what is returned that should give you the answer that you seek.


RE: Differences in session variable declaration - n2fole00 - 09-12-2018

(09-12-2018, 02:20 PM)InsiteFX Wrote: When using:
PHP Code:
$CI =& get_instance(); 

Your getting CodeIgniter's Controller Class.

Are you loading the Session library in the Controller or are you Autoloading it?

I'm autoloading it.


RE: Differences in session variable declaration - n2fole00 - 09-12-2018

(09-12-2018, 02:22 PM)php_rocs Wrote: @n2fole00

It could be what value is returned between the two.  You could do a var_dump on both values and see what is returned that should give you the answer that you seek.

They both return the same thing. The error occurs when the value is NULL.


RE: Differences in session variable declaration - InsiteFX - 09-12-2018

Then use isset method on it to see if it contains a value or not.


RE: Differences in session variable declaration - php_rocs - 09-13-2018

@n2fole00,

They both are null but one causes an errors and the other doesn't? That can't be right. There is a difference. That is the only reason why one would work and the other not work.

InsiteFX made a excellent suggestion as well.


RE: Differences in session variable declaration - n2fole00 - 09-13-2018

(09-13-2018, 07:51 AM)php_rocs Wrote: @n2fole00,

They both are null but one causes an errors and the other doesn't?  That can't be right.  There is a difference.  That is the only reason why one would work and the other not work.

InsiteFX made a excellent suggestion as well.

This is what I was thinking, but what could be the difference? var_dump should give me the output isset would test for.


RE: Differences in session variable declaration - php_rocs - 09-13-2018

@n2fole00,

What was the exact var_dump for each variable?


RE: Differences in session variable declaration - n2fole00 - 09-13-2018

(09-13-2018, 02:08 PM)php_rocs Wrote: @n2fole00,

What was the exact var_dump for each variable?

PHP Code:
$site_lang2 $_SESSION['site_lang'];
$site_lang $ci->session->site_lang;

echo 
"site_lang: ";
var_dump($site_lang);
echo 
"site_lang2: ";
var_dump($site_lang2);
echo 
"isset: ";
var_dump(isset($site_lang2)); 

Code:
site_lang: NULL site_lang2: NULL isset: bool(false)