Welcome Guest, Not a member yet? Register   Sign In
how to assign a value to session variable
#1

[eluser]Unknown[/eluser]
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

#2

[eluser]meer[/eluser]
Code:
try this to get your email and password values
  $sub = array(
  ‘email2’  => $this->input->post(‘email2’),
  ‘password2’  => $this->input->post(‘password2’)
  );
#3

[eluser]Unknown[/eluser]
Thanks, its working
#4

[eluser]meer[/eluser]
cheers:-)
#5

[eluser]kanjimaster[/eluser]
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.
#6

[eluser]Unknown[/eluser]
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() ....
#7

[eluser]kanjimaster[/eluser]
[quote author="TheLastBlack" date="1340103841"]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() ....[/quote]

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.




Theme © iAndrew 2016 - Forum software by © MyBB