Reputation: 77
public function admin_login()
{
$this->form_validation->set_rules('email','E-mail','required|trim');
$this->form_validation->set_rules('password','Password','required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>"," </p>");
if( $this->form_validation->run())
{
$email=$this->input->post('email');
$password=$this->input->post('password');
$link='http://api.amid.tech/admin/'.$email.'/'.$password;
$data = (array) json_decode(file_get_contents($link,true));
if($email== $data['email'] && $password== $data['password'])
{
var_dump($data = (array) json_decode(file_get_contents($link,true)));
echo $userid=$this->session->set_userdata('name',$data);
exit;
$this->load->view('admin/dashboard');
}
else
{
// echo"false";
$this->load->view('index');
//echo validation_errors('');
}
}
}
I want to set session id in this code through api
Upvotes: 2
Views: 95
Reputation: 2425
Not Exactly sure what you want but if you have refereed CI's user guide then, this can help you set session and save them in database.
First Load The Session Library:
$this->load->library('session');
Now If you have some value in variable and want to store it into session then you can do this:
$this->session->set_userdata('string', $variable);
If there is an array of data then as mentioned in user guide:
$newdata = array(
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
And if you want a specific item from session then you can do:
echo $this->session->userdata('session-variable-name');
For all session data do:
var_dump($this->session->all_userdata());
And if you want to save your sessions to database then simply create a table called ci_sessions
CREATE TABLE IF NOT EXISTS `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(45) DEFAULT '0' NOT NULL,
user_agent varchar(120) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id),
KEY `last_activity_idx` (`last_activity`)
);
Like so.
I suggest you to read the user guide it has lot more.
Upvotes: 0
Reputation: 1008
Codeigniter 2.X's session management is NOT implemented on top of PHP Sessions. So if you were trying to print $_SESSION
, it will show up an empty array. Instead you should use $this->session->userdata('key')
. $_SESSION
should however work just fine if you are using CI 3.X.
Have a look at this: https://www.codeigniter.com/userguide3/installation/upgrade_300.html#step-6-update-your-session-library-usage
Upvotes: 1