Welcome Guest, Not a member yet? Register   Sign In
session login
#1

[eluser]r0mel[/eluser]
good day everyone,, i need your help guys, I have a problem with my session, when I logout, though it works but still when i press the back button, it still can go through the home page. I want my page go steady in the login form after i press logout. I'am a novice about codeigniter. please hepl me,, you appreciatition with this problem would help a lot.

-->>here is my controller test.php
Code:
<?php if( !defined('BASEPATH') ) exit("No direct access script allowed");

class Test extends CI_Controller {
var $model;

function __construct() {
  parent::__construct();
  
  // for session library
  $this->load->library( array('session') );
  
  // form helper
  $this->load->helper( array('form','url', 'html', 'string') );
  
  // load the test model
  $this->load->model("test/testmodel");
  
  // initialize the test model
  $this->model = new TestModel();
}


// function name index is a reserved word
function index() {
  // echo "te";  
  if( ! $this->session->userdata('isLogin'))
   $this->load->view("test/login", array('hh', 'laur0n'));
}

function check_login() {
  $username = $this->input->post('username') ? $this->input->post('username') : '' ;
  $password = $this->input->post('password') ? $this->input->post('password') : '' ;
  
  // var_dump( $this->input->post()  );
  $query = $this->model->authenticate_user( $username, $password);
  
  if( $query ) {
   $this->session->set_userdata('isLogin', 'mi agi ko');
   redirect("test/test/loginKo");
   // echo json_encode( $query );
  }
  else {
   // echo json_encode( $query );
   $this->load->view('test/login');
  }
}

function register() {
  $this->load->view("test/register");
}

public function loginKo(){
  
   // if( gettype( $this->session->userdata('isLogin') ) === "boolean" ) redirect('test/test');
   // echo gettype( $this->session->userdata('isLogin') );
   if($this->session->userdata("isLogin")){
  
     $this->load->view('test/information');
   }else{
    redirect("/test/test");
   }
}
  
function save_register() {
  $item = array(
   'objid'    => null,
   'firstname'=> $this->input->post('firstname'),
   'lastname' => $this->input->post('lastname'),
   'email'    => $this->input->post('email'),
   'password' => $this->input->post('password'),
   'gender'   => $this->input->post('gender'),
   'status'   => 'A'
  );
  
  if( $this->model->save_user( $item ))
   $this->load->view('test/login');  
}

function logout() {
  $this->session->sess_destroy();
  // echo json_encode( $this->session->all_userdata() );
  redirect("test/test");
}
}

?>

-->> here is my model

Code:
<?php if( !defined('BASEPATH') ) exit("No direct access script allowed");

class TestModel extends CI_Model {
function __construct() {
  parent::__construct();
  $this->load->database();
}

function save_user( $item ) {
  $query = $this->db->insert('users', $item);
  
  return $query ? 1 : 0;
}

function authenticate_user( $email='', $password='' ) {

  
  $query  = "select * from users where email='{$email}' and password='{$password}'";
  // echo $query;
  $query = $this->db->query( $query );
  
  if( $query->num_rows() > 0 ) {
   return $query->result();
  }
  return false;  
}
}

?>
-->> here is my view

Code:
<?php


if( ! $this->session->userdata('isLogin') )
redirect('test/test');

?>

this is the home page



<?php echo anchor('test/test/logout', 'logout'); ?>
#2

[eluser]r0mel[/eluser]
i think you can do that by doing an ajax call to observe your server and then redirect if there is no current session available.
#3

[eluser]CroNiX[/eluser]
Please use code tags when posting code.
#4

[eluser]r0mel[/eluser]
sorry sir,, I'm not familiar with the codeigniter... sorry,,, thank you sir for your response.. i'll edit the one i had posted previously,
#5

[eluser]r0mel[/eluser]
@cronix: Sir, just want to ask about the problem of my code, i knew it was a bit buggy. I used a session but i have a problem with that. In pure php, i can handle the session as what i had expected, it couldn't return/access to the home after i logout, though i press the back button in th browser still it was in the login form which is correct. With the session in codeigniter, i have a problem with that because the back button in the browser still can go through the home page though i checked if has a session login or not.Sad
please guide me sir. thank you sir
#6

[eluser]InsiteFX[/eluser]
You are using strings for your isLogin and it should be boolean TRUE/FALSE which is what you are checking for in
your index function. Change your isLogin to TRUE and FALSE.
Code:
function logout()
{
    $this->session->set_userdata('isLogin', FALSE);
    $this->session->sess_destroy();

    // echo json_encode( $this->session->all_userdata() );

    redirect('test/test', 'refresh');
}

Also on all your input->post
Code:
'firstname'=> $this->input->post('firstname', TRUE), // check for xss ( cross site scripting ).
#7

[eluser]r0mel[/eluser]
@insiteFX: Thank you for you response sir....I did already what you suggest but still it goes back to the home page when i press back button in the browser. seems that the page was cache,,, i don't know what is the problem with it. ??
#8

[eluser]r0mel[/eluser]
i hack your account men.. :cheese: :ahhh:
you have so easy username and password romel ^_^ Tongue




Theme © iAndrew 2016 - Forum software by © MyBB