EllisLab text mark
Advanced Search
     
how to assign a value to session variable
Posted: 12 June 2012 02:16 AM   [ Ignore ]
Joined: 2012-06-07
3 posts

hi
i am new in codeigniter. i am trying to assign a value to a session variable but it didnot work.
i have a login page .i create a login page
function login()
{
 
  $data = array(
                  ‘head_title’ => ‘Login’,
    ‘head_css’  => ‘login.css’
      );
  $sub = array(
  ‘email2’  => $_POST[‘email2’],
  ‘password2’  => $_POST[‘password2’]
  );
   
          $this->session->set_userdata($sub);
  $this->layout->view(’/default/pages/login’,$data);

    }


when i run it it show the following error


A PHP Error was encountered

Severity: Notice

Message: Undefined index: email2

Filename: controllers/pages.php

Line Number: 155
A PHP Error was encountered

Severity: Notice

Message: Undefined index: password2

Filename: controllers/pages.php

Line Number: 156

please any one help me
 

 Signature 

Thanks
Ratikanta Mohapatra

 
Posted: 14 June 2012 05:14 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2012-03-20
69 posts
try this to get your email and password values
  $sub 
= array( 
  
‘email2’  => $this->input->post(‘email2’),
  
‘password2’  => $this->input->post(‘password2’)
  ); 
 
Posted: 15 June 2012 03:41 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-06-07
3 posts

Thanks, its working

 Signature 

Thanks
Ratikanta Mohapatra

 
Posted: 15 June 2012 04:15 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-03-20
69 posts

cheers:-)

 
Posted: 15 June 2012 10:44 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2011-09-18
61 posts

Maybe it’s working, maybe not. All we really know is that it’s not throwing PHP Notices any more.

The Codeigniter input->post() function tests whether the post array entry exists and if not returns the boolean false. So you now always have a value, which is why it doesn’t fail.

So if you need values for email2 and password2, then you’ve just disguised the problem, and it will pop up again in a different form later. But if they’re not essential, and false is an acceptable way for your other code to detect that you’d don’t have them, then you’re probably OK.

 
Posted: 19 June 2012 07:04 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2012-06-19
1 posts

just to add a note:
codeigniter gets all values from _GET & _POST and unsets the arrays before running your code,
so the only way is to use $this->input->post() ....

 
Posted: 19 June 2012 08:10 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2011-09-18
61 posts
TheLastBlack - 19 June 2012 07:04 AM

just to add a note:
codeigniter gets all values from _GET & _POST and unsets the arrays before running your code,
so the only way is to use $this->input->post() ....

This is overstating it somewhat. The session token is removed from the $_POST array, and the $_GET array is destroyed if $config[‘allow_get_array’] is set to FALSE.

But the only circumstance in which both will be destroyed is if register globals is set in your PHP settings, which would be very rare nowadays and can’t happen at all in PHP 5.4.

Nevertheless, using $this->input->get() and $this->input->post() have many advantages over access the global vars directly.