I have this two controller methods that both set session data
//Profile
public function profile()
{
$this->session->set_userdata('title', 'some_value');
}
//KYC
public function kyc()
{
$this->session->set_userdata('title', 'KYC');
$this->load->view('basic/basic_app_views/kyc');
}
I am first loading the function kyc and using the session in my view
<title><?php echo $this->session->userdata('title'); ?></title>
I then load profile in my browser and refresh kyc but the value in kyc is still KYC like i had set in the kyc function.
How come title still has the value KYC even after resetting the value in another controller function?.
It is because each time you call your function it will set the userdata as it defines.
When try to set a title in a page, i suggest you to do this instead.
//Profile
public function profile()
{
$data['title'] = 'Profile Page';
$this->load->view('basic/basic_app_views/kyc', $data);
}
//KYC
public function kyc()
{
$data['title'] = 'KYC Page';
$this->load->view('basic/basic_app_views/kyc', $data);
}
then call it inside your kyc.php file.
<title><?php echo $title; ?></title>
hope that helps.
To see the change in the session title variable you would need to change up your code a little...
So your
//Profile
public function profile()
{
$this->session->set_userdata('title', 'some_value');
}
//KYC
public function kyc()
{
$this->session->set_userdata('title', 'KYC');
$this->load->view('basic/basic_app_views/kyc');
}
To something like...
//Profile
public function profile()
{
$this->session->set_userdata('title', 'some_value');
$this->load->view('basic/basic_app_views/kyc'); // View the title value
}
//KYC
public function kyc()
{
$this->session->set_userdata('title', 'KYC');
$this->load->view('basic/basic_app_views/kyc'); // View the title value
}
That way you will see your value change. Each of your two methods individually change the value. But in your previous code only the kyc was displaying it.
Related
I have a issue , I am creating session but when we access session in another controller,
so the session variable not found there
Route
Route::get('entry1', 'TestController#entry1');
Route::get('entry2', 'TestController#entry2');
Controller function
public function entry1(Request $request)
{
Session::put('username', 'adminTest');
echo Session::get('username');
}
public function entry2(Request $request)
{
echo Session::get('username');
}
You can use like this :
To set value in session :
Session(['sessionId' => $sessionId]);
To get value from session
$sessionId = Session('sessionId');
I am working on login and search functionality using CodeIgniter.
What Exactly I am doing?
When the user searches something in the search form and on submitting that form I put a check if the user is logged in then it will directly show the result & if the user is not logged in then it redirects the user to the login page and I saved the search data in session as search_session_data.
I have a controller which redirects user regarding the session. If the user came directly to the login page then the user is redirected to the dashboard after login. but if the user is coming from search page then the user is redirected to the search result page after login.
I have mentioned the problem inside the code with a comment.
This is the controller:
public function get_the_search_result() {
$search_session_data = $this->session->userdata('search_session_data');
$this->load->model ('my_model');
$result_data = $this->my_model->find_data_regarding_search_criteria($search_session_data);
$this->load->view ('app/app_statch_result',['result_data' => $result_data,]);
}
public function login_function() {
//necessary login code
if ($this->session->userdata('search_session_data')) {
//Here I want the if `search_session_data` available in session then
// user goest to `get_the_search_result` and view the
//'app/app_statch_result` but it is not working.
$this->get_the_search_result();
} else {
return redirect('dashboard');
}
}
So How do I redirect the user to app_statch_result from
login_function function?
Any suggestion regarding improvement is applicable. Thanks
If I were you, I would do the following:
Create login controller, or better, library and put all login logic inside that class. Then you can load that library from any controller... For example:
Sample library
class User_controll{
private $CI;
public function __construct() {
$this->CI = &get_instance();
}
public function check_user_data($username, $password) {
$tmpId;
// To increase security you can encrypt password
$user = $this->CI->db->from('users')->where(array('username' => $username, 'password' => $password))->get()->result();
if(count($user) == 1){
$this->CI->session->set_userdata('loggedIn', 1);
redirect('user_page');
}
}
protected function logout() {
$this->CI->session->set_userdata('loggedIn', 0);
}
public function isLoggedIn() {
return $this->CI->session->userdata('loggedIn') == 1;
}
}
Sample Controller
class User_page extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->library('User_controll');
}
public function index() {
if(!$this->user_controll->isLoggedIn()){
redirect('user_page/loginForm');
}
else {
// do whatever you want
}
}
}
I have a situation of loosing value stored in $member variable.
class User extends CI_Controller {
protected $message;
function list()
{
$data['message'] = $this->message; // it's empty
$this->load->view('view', $data);
}
function delete($id)
{
$this->user_model->delete($id);
$this->message = "Success";
redirect('user/list');
}
}
The reason of using a redirect is to get a clean URL. I get empty value for $this->message in list() after getting redirected.
I even tried making it static, but still no luck.
You could try using flash messages:
function delete($id)
{
$this->user_model->delete($id);
$this->session->set_flashdata('message', 'Success');
redirect('user/list');
}
For this you are likely to need to load session library in constructor of you controller:
$this->load->library('session');
In your view use this:
<?php echo $this->session->flashdata('message');?>
You can preserve data in flash messages for several requests like this:
$this->session->keep_flashdata('message');
Have a look at this link
I am trying to build a simple login system using CodeIgniter. I have placed all of my functions into one controller and I have also placed a session check function called is_logged_in into the controller. However, the user can still copy and paste the URL once they have logged out and view the main page. It seems like my is logged in function isn't working. Am I missing something? It only works if I place a check in my header every time instead. Here is my code.
Controller file
function index()
{
$this->load->view('login');
}
function checklogin()
{
$this->load->model('user');
$query = $this->user->validate_login();
if($query)
{
redirect('portal/home');
}
else
{
$this->index();
}
}
function logout()
{
$this->session->unset_userdata('username');
$this->session->unset_userdata('is_logged_in');
$this->session->sess_destroy();
redirect($this->index(),'refresh');
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in)||$is_logged_in!=TRUE)
{
$this->index();
}
}
function home()
{
$this->is_logged_in();
$data['main_content'] = 'home';
$this->load->view('includes/template',$data);
}
function statements()
{
$this->is_logged_in();
$data['main_content'] = 'statements';
$this->load->view('includes/template',$data);
}
function about()
{
$this->is_logged_in();
$data['main_content'] = 'about';
$this->load->view('includes/template',$data);
}
}
?>
This is what I place into my headers that actually works instead of the function
<?php
if(!$this->session->userdata('is_logged_in'))
{
redirect('portal/index');
}
?>
User Model
<?php
class User extends CI_Model{
function __construct()
{
parent::__construct();
}
public function validate_login()
{
//$this->form_validation->set_rules('username', 'Username', 'trim|required');
//$this->form_validation->set_rules('password', 'Password', 'trim|required');
$username = $this->security->xss_clean($this->input->post('username'));
$password = $this->security->xss_clean(md5($this->input->post('password')));
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('accounts');
if($query->num_rows()==1)
{
foreach($query->result() as $row)
{
$sess_data = array(
'username'=>$this->input->post('username'),
'is_logged_in'=>TRUE,
'privilege'=>$row->privilege
);
}
$this->session->set_userdata($sess_data);
return true;
}
else
{
// echo $password;
}
}
}
?>
Also, how can I combine the commented out part in the user model with the next lines? I want an error message to be sent if no input is placed. Hope I can get help with this.
So much wrong in this. Do not, not ever, use a single controller for your application. Things will get pretty big, pretty fast, and then you have one unreadable blob of a file. Keep your application code normalized. That's why you are using MVC. If you then need a method accessible from every, or nearly every controller, then put this function into a base controller(MY_Controller i.e.), or a model, or a library, depending on the functionality of this method. But in your case, this is for a base controller. This wont help with your problem, but just a friendly suggestion.
The problem with your code is, the visitor hits the URL for method "statements" and this is what happens:
statements calls is_logged_in
is_logged_in determines user is not logged in and calls index
index loads the login view
statements method loads the statements view
After you check for log in, and determine that the user is not logged in, you have to prevent further execution of other parts of your code. I would suggest having is_logged_in method returning a bool(false) value, and the statements(and other methods) then stopping execution. But if you would separate your code like you should over multiple controllers, then maybe have the is_logged_in controller to redirect the user to the login page.
I want to send a variable '$msg_notf' from my controller to my view but every time I do this, codeigniter returns the error "Undefined variable: msg_notf" .
My Controller,
public function send_message(){
$this->load->model('model_student');
$msg_send=$this->model_student->send_message($this->session->userdata('roll_no'));
if($msg_send==true){
$result['msg_notf']='message sent';
$this->load->helper('url');
redirect('http://localhost/CheckIn_System/index.php/student',$result);
}else{
$result['msg_notf']='unable to send message';
$this->load->helper('url');
redirect('http://localhost/CheckIn_System/index.php/student',$result);
}
}
In view,
echo $msg_notf;
In Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');//load once Controller load
}
public function send_message()
{
$this->load->model('model_student');
$msg_send=$this->model_student->send_message($this->session->userdata('roll_no'));
if($msg_send==true){
$result['msg_notf']='message sent';
$this->load->view('student',$result);//passing data to view
}else{
$result['msg_notf']='unable to send message';
$this->load->view('student',$result);//passing data to view
}
}
in view
foreach ($msg_notf as $new_msg_notf)
{
echo $new_msg_notf['your_data_field'];//showing your data
}
Your controller may look like this :
public function __construct(){
parent:: __construct();
$this->load->helper('url');
}
public function send_message(){
$this->load->model('model_student');
$msg_send=$this->model_student->send_message($this->session->userdata('roll_no'));
if($msg_send==true){
$result['msg_notf']='message sent';
$this->load->view('path', $result); // path of the
http://localhost/CheckIn_System/index.php/student
}else{
$result['msg_notf']='unable to send message';
$this->load->view('path',$result);
}
}
If you want use redirect, the best idea is use Flashdata. Is a one-time session var that persists until you use it, then is deleted.
you need call: $this->load->library('session');
Declaration:
$this->session->set_flashdata('item', 'value');
To read:
$this->session->flashdata('item');
https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
The redirect() function does a "header redirect" to the URI specified. The optional second parameter allows you to choose between the "location" method (default) or the "refresh" method.
If you want to pass data into a view use
$this->load->view("View_file", $result);
and on the view page access it like
echo $msg_notf;
With redirect function you should use Session (Userdata or Flashdata)
Flashdata is preferred in this case.