I have a function named check_database
in which I have variable $username
, I have created another function named get_val()
in which I am trying to access $username
variable in my get_val()
method. Please let me know how to do it.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' => $row->username
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
function get_val()
{
$this->check_database();
}
}
</div>
Since you are setting session in check_database method you can retrieve it via session.
$username= $this->session->all_userdata()['username'];
Create a private variable username in class then assign the value post('username') in to it ... then access anywhere you wish.
class VerifyLogin extends CI_Controller {
private $username;
function check_database($password){
$this->username = $this->input->post('username');
}
function get_val(){
echo $this->username;
}
}