hi i'm new to Code Igniter here is my controller for signup & login.i'm having a little problem while working with sessions.when i cal logout function it was redirecting the page to login page with out destructing sessions.
after doing logout when i press back button it was displaying the user data.please check mycode

<?php

class Learning extends CI_Controller {


   public function __construct()
    {
       parent::__construct();
       $this->load->library('session');
       $this->load->helper('url'); 
       session_start();    
    }
    public function index()
    {
    $this->load->view('learning');   
    }

     public function loadsignup()
    {

                $this->load->library('form_validation');
                $this->load->library('session');
                $this->load->library('encrypt');
                $this->load->library('email');
                $this->load->helper('url'); 
                $this->load->helper('captcha');
                $this->load->model('learning_model');
                $this->load->database();

                $this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_dash|xss_clean');
                $this->form_validation->set_rules('password', 'Password', 'required|trim|alpha|xss_clean|matches[pass]');
                $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
                $this->form_validation->set_rules('mobile', 'Mobile Number', 'required|trim|xss_clean|numeric');
                $this->form_validation->set_rules('captcha','Captcha','required|xss_clean');


               if($this->input->post()!=NULL)
                {

                     $captcha=$this->input->post('captcha');
                     $expiration = time()-600; // Two hour limit
                     $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);  

// Then see if a captcha exists:
                    $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
                    $binds = array($captcha, $this->input->ip_address(), $expiration);
                    $query = $this->db->query($sql, $binds);
                    $row = $query->row();
                    if ($row->count == 0)
                    {
                    $error=TRUE;
                    $data['error']="You must submit the word that appears in the image";
                    } else $error=FALSE;

             }

              if($this->form_validation->run()===FALSE || $error===TRUE)
            {
                 $data['error']=""; 
                 $this->load->view('lsignup',$data);
            }
            else 
            {

                            $password=$this->input->post('password');
                            $password=$this->encrypt->sha1($password);
                            $data['username']=$this->input->post('username');
                            $data['password']=$password;
                            $data['email']=$this->input->post('email');
                            $data['mobile']=$this->input->post('mobile');
                            echo "Details Taken Sucessfully"."<br/>";
                            $result=$this->learning_model->signup($data);
                            if($result===true)echo 'yes Data Inserted Sucessfully';else echo ' Data Not Inserted Sucessfully';
                            echo $data['email'];
                            $this->email->clear();
                            $this->email->to($data['email']);
                            $this->email->from('eswarvarma141@gmail.com');
                $this->email->subject('Hii dude This Is Sample Mail By using Code iGniter Email Function'. $data['username']);
                            $this->email->message('Hi '.$data['username'].' Have a Good Day.');
                            $this->email->send();
                            if ( ! $this->email->send()) echo "Sorry Email Not Send";else echo "Email Sent";



            }



        }






    public function login()
    {
                $this->load->helper('url'); 
                $this->load->library('form_validation');
                $this->load->library('session');
                $this->load->library('encrypt');
                $this->load->model('learning_model');

                 $this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_dash|xss_clean');
                 $this->form_validation->set_rules('password', 'Password', 'required|trim|alpha|xss_clean');
                 $error='';
                 if ($this->form_validation->run() == FALSE)
                {
                $this->load->view('lsignin');
                } 
                else 
                {
                     $password=$this->input->post('password');
                     $password=$this->encrypt->sha1($password);
                     $data['username']=$this->input->post('username');
                     $data['password']=$password;
                     $result =$this->learning_model->login($data);
                        if($result<='0')
                        {
                         $login='false';
                        $this->session->set_userdata('login','false');
                        }
                        else
                        {
                         $login='true';
                        $this->session->set_userdata($result);
                        $this->session->set_userdata('login','true');

                        }
              if($login=='true')
              {
              $this->load->view('lwel');
              }
             else 
             {
              echo 'Sorry User Name Or Password May Be Wrong';
              $this->load->view('lsignin');
             }
    }
  }  
public function logout()
{

if(session_destroy())
{
$this->session->unset_userdata('login');
$this->session->unset_userdata('username');   
$this->session->unset_userdata($result);    
$this->session->sess_destroy();
redirect('learning/login');
}
}




}
?>

Thamk you all

CI Session doesn't use PHP native Session, so you don't have to use session_destroy(), change you logout method to:

public function logout()
{
    $this->session->unset_userdata('login');
    $this->session->unset_userdata('username');   
    $this->session->unset_userdata($result); # error
    redirect('learning/login');
}

Also, if $result variable is global then use $this->result otherwise you get an error because the variable is not declared in the function.

Note: you don't need $this->session->sess_destroy() unless you want to destroy all the variables of the current session, if this is your goal then you don't need the previous $this->session->unset_userdata(''):

public function logout()
{
    $this->session->sess_destroy();
    redirect('learning/login');
}

For more information check the user guide: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.