I want try if within if loop use but getting some error black page no error show please check its and help me
public function login2($otp) {
$this->db->where('otp', $otp);
$query = $this->db->get('login');
if ($query->num_rows() == 1)
{
//$this->load->view('index');
if ($role=='1' && $type=='1')
{
$this->load->view('index');
}
else if ($role=='2' && $type=='1')
{
$this->load->view('admin/index');
}
}
else
{
$_SESSION['error']='Wrong OTP Plese Enter correct OTP';
$this->load->view('otp');
}
}
Can you echo the contents of the variables used in the if conditions? Check they match your statements. Unless they're global, I can't see where they're populated.
I'm Using codeigniter framework and i'm having a problem with a session.. I was trying to create login page, but there was a problem when I used set_userdata().
It doesn't work. When I checked, there was no session created.
Is there any wrong with my code?? Any one has same problem with it??
function __construct(){
$this->CI=&get_instance();
$auth=$this->CI->config->item('auth');
$this->CI->load->helper('cookie');
$this->CI->load->library('session');
$this->CI->load->model('Auth_users_model');
}
function login($username,$password){
$result=$this->CI->Auth_users_model->get_login_info($username);
if($result){
$password=md5($password);
if($password==$result->password){
$user_ses=array('user_id_hcp'=>$result->id,'user_access_hcp'=>$result->access, 'user_username_hcp'=>$username);
$this->CI->session->set_userdata($user_ses);
return TRUE;
}else{
return FALSE;
}
}else{
return FALSE;
}
}
}
Instead of $this->CI->session->set_userdata($user_ses);
try using simply
$this->session->set_userdata($user_ses);
For some reason I cannot for the life of me get the edit page to display. Not sure what I am doing wrong I went through the code so many times. When I click to edit a record the page is blank and no error messages come up.
here is the code for the model which I think is why it is causing an issue.
public function update_employee($data,$id)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
}
Here is the code for the contoller
public function update_employee()
{
$id=$this->input->post('id');
$data=array('Employee_Name'=>$this->input->post('Employee_Name'),
'Employee_Number'=>$this->input->post('Employee_Number'));
$result=$this->employee_model->update_employee($data);
if($result > 0)
{
$this->session->set_flashdata('msg',"Employee Records Updated Successfully");
redirect('employee/list_employees');
}
else
{
$this->session->set_flashdata('msg1',"No changes Made in Employee Records");
redirect('employee/list_employees');
}
}
Your function have a typo?
$data=array('Emplyee_Name'...
Maybe it should be:
$data=array('Employee_Name'...
Hope this helps
I replaced return ($this->db->affected_rows() != 1 ) ? false:true;
with return print_r($data); and now it works
I am using Codeigniter 2.1.4 and I have facing some issues with flashdata.
When I successfully submit record I can display the flashdata message. But if go to the other page from the page where flashdata message was displayed and then go back to previous page using browser back button it shows me flashdata message again.
How to clear flashdata message once it used?
I think its not the flashdata issue its cache problem. I am confused why this is happening. If its cache issue then how to remove it?
Below is code I have used,
//In the manage of controller
$this->session->set_flashdata('message', "Record updated successfully.");
// In the view of controller
$data['message'] = $this->session->flashdata('message');
// In the view page
echo $message;
Flash disappears only after next refresh
your code in controller is right
//In the manage of controller
$this->session->set_flashdata('message', "Record updated successfully.");
redirect('controller_name/function_name','refresh');
now in view use like this
if($this->session->flashdata('message')){echo $this->session->flashdata('message');}
hope it will work
Go to System->libries->Session->session.php
Find flshdata fuction and replace with this
public function flashdata($key = NULL)
{
if (isset($key))
{
$return= (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
? $_SESSION[$key]
: NULL;
unset($_SESSION[$key]);
return $return;
}
$flashdata = array();
if ( ! empty($_SESSION['__ci_vars']))
{
foreach ($_SESSION['__ci_vars'] as $key => &$value)
{
is_int($value) OR $flashdata[$key] = $_SESSION[$key];
}
}
unset($_SESSION[$key]);
return $flashdata;
}
You must redirect the page somewhere after $this->session->set_flash('item','value');
Example:
if ($this->form_validation->run() == FALSE){
$this->session->set_flashdata('error',validation_errors());
redirect(base_url().'user/login');
}
else{
$this->session->set_flashdata('success','Thank you');
redirect(base_url().'user/login');
}
Usually developer make a mistake when they submit data to same page. They set flash data but forget to redirect.
if you want to clear set_flash in controller or another view file, then you can use this simple code.
$this->session->set_flashdata('error', 'User not found...'); //create set_flash
destroy set_flash
//echo "<pre>"; print_r($_SESSION); die; //for check
if(isset($_SESSION['error'])){
unset($_SESSION['error']);
}
$this->session->set_flashdata('message', "Record updated successfully.");
After setting the flashdata redirect to some function or to the same function.
If you refresh in the same controller function the flashdata won't be deleted.Also going back and forth in the browser does't affect the flashdata.
to clear the flashdata redirect to another controller function and it will work.
As Code igniter does not offer the possibility to destroy the flashdata, you can work around this problem with a second fictitious call of the flashdata function without echo :
if ($this->session->flashdata('message')) :
echo $this->session->flashdata('message'); // First normal call
$this->session->flashdata('message'); // Second fictitious call
endif;
Looks like this will be fixed in 3.1.12: https://github.com/bcit-ci/CodeIgniter/pull/6013
It is php version issue. you need to modify your session file.
go to your Session.php file
find the _ci_init_vars function and add this "$value === 'old' ||" in the elseif condition.
or replace the function with following.
protected function _ci_init_vars()
{
if ( ! empty($_SESSION['__ci_vars']))
{
$current_time = time();
foreach ($_SESSION['__ci_vars'] as $key => &$value)
{
if ($value === 'new')
{
$_SESSION['__ci_vars'][$key] = 'old';
}
// Hacky, but 'old' will (implicitly) always be less than time() ;)
// DO NOT move this above the 'new' check!
elseif ($value === 'old' || $value < $current_time)
{
unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
}
}
if (empty($_SESSION['__ci_vars']))
{
unset($_SESSION['__ci_vars']);
}
}
$this->userdata =& $_SESSION;
}
I have faced same problem when I have update php version 7 to php 8.
You can manually unset flash data just after display your error or success message.
$this->session->unset_userdata('err_msg')
it's work : https://github.com/bcit-ci/CodeIgniter/pull/6013#issuecomment-1316482414
This is an issue in newer version of php . You can fix it by replacing line 420 in system/libraries/Session/Session.php:
elseif ($value < $current_time)
with
elseif ($value === 'old' || $value < $current_time)
thanks,
I need little Active record help. I am trying to update page views which i store in database. Each time post() function runs, add +1 . Here is my code:
public function post($id) {
if($id == NULL){
redirect('blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1');
$this->db->update('entry');
Please help!
Correct your code like this for example:
public function post($id) {
if(!isset($id) || (int) $id<=0){
header('Location: /blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1');
$this->db->update('entry');
}
Mark the header('Location: /blog') - you should put here your real server path to the blog.
If you use a custom redirection function, like your redirect() then check if the is an output before calling her. Header redirection only works if no other output is sent to the browser before invoking it.
public function post($id) {
if($id == NULL){
redirect('blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1',FALSE);
$this->db->update('entry');
FALSE turns off Active record escaping,by default it ignores +1.