Welcome Guest, Not a member yet? Register   Sign In
redirect after login
#11

[eluser]Kraig[/eluser]
After doing what you said I still get the return_url to be "http://localhost:8888/ci/home/validate_credentials" no matter what page I am on. Here's how I implemented it into my controller:

Code:
class Home extends CI_Controller {

public function __construct()
{
     parent::__construct();

     $is_logged_in = $this->session->userdata('is_logged_in');
     if (!$is_logged_in)
     {
         $this->session->set_userdata('return_url', current_url());
     }
}

function index()
{
  $is_logged_in = $this->session->userdata('is_logged_in');
  if(!$is_logged_in)
  {
   $data['main_content'] = 'home';
   $this->load->view('includes/template', $data);
  }
  else
  {
   $data['main_content'] = 'home';
   $this->load->view('includes/template', $data);
  }
}
  
public function validate_credentials()
{
  // Checking whether the Login form has been submitted
  if($this->input->post('submit'))
  {
   // Will hold errors
   $err = array();
  
   $this->load->library('form_validation');
  
   // field name, error message, validation rules
   $this->form_validation->set_rules('password', 'Password', 'trim|required');
   $this->form_validation->set_rules('username', 'Username', 'trim|required');
  
   if($this->form_validation->run() == FALSE)
   {
    $err[] = 'All the fields must be filled in!';
   }
    
   $this->load->model('membership_model');
   $q = $this->membership_model->validate();
  
   if($q) // if the user's credentials validated...
   {
    $data = array(
     'username' => $this->input->post('username'),
     'password' => $this->input->post('password'),
     'is_logged_in' => true
    );
    
    $this->session->set_userdata($data);
    $this->config->set_item('sess_expiration', 7200);
    redirect($this->session->userdata('return_url')); // Not redirecting right after login!!
   }
   else
   {
    $this->index();
    $link = $this->session->userdata('return_url');
    $err[] = $link.'Username and or password are incorrect.';
   }
  
   if($err){
    $data = array(
     'msg' => implode('<br />',$err),
     'reg-msg' => ' '
     );
    $this->session->set_userdata($data);
    // Save the error messages in the session
   }
   redirect('home/home');
  }
}
#12

[eluser]Ayeyermaw[/eluser]
Ah right. I think you misunderstand.

MY_controller is an extension of CI_controller. It's one file and you create it yourself then reference it in all your controllers.
so instead of
Code:
class home extends CI_Controller {
you'd have
Code:
class home extends MY_Controller {

take a look at http://ellislab.com/codeigniter/user-gui...asses.html for an overview on extending the core classes
#13

[eluser]Kraig[/eluser]
This is what I did and now I get a server error...I don't see any mistakes

Code:
&lt;?php

class MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
        $is_logged_in = $this->session->userdata('is_logged_in');
     if (!$is_logged_in)
     {
         $this->session->set_userdata('return_url', current_url());
     }
    }
}

Code:
class Home extends MY_Controller {

function index()
{
  $is_logged_in = $this->session->userdata('is_logged_in');
  if(!$is_logged_in)
  {
   $data['main_content'] = 'home';
   $this->load->view('includes/template', $data);
  }
  else
  {
   $data['main_content'] = 'home';
   $this->load->view('includes/template', $data);
  }
}
#14

[eluser]Ayeyermaw[/eluser]
post the error
#15

[eluser]Kraig[/eluser]
Server error
The website encountered an error while retrieving http://localhost:8888/ci/home/. It may be down for maintenance or configured incorrectly.
Here are some suggestions:
Reload this webpage later.
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
#16

[eluser]Kraig[/eluser]
When I change
Code:
class Home extends MY_Controller {
to
Code:
class Home extends CI_Controller {
it works just like it did. Not sure why my MY_Controller class is not working...below is the entire class:
Code:
&lt;?php

class MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
        $is_logged_in = $this->session->userdata('is_logged_in');
     if (!$is_logged_in)
     {
         $this->session->set_userdata('return_url', current_url());
     }
    }
}
#17

[eluser]CroNiX[/eluser]
where is the location of your MY_Controller.php file?
/application/core/ ?? That's where it should be if you extend a CI Core Class.
#18

[eluser]Kraig[/eluser]
Ah, no it was in /system/core/

After moving it the script works now, but it still says that url is http://localhost:8888/ci/home/validate_credentials. I know the form is submitted through validate_credentials(), but the page that it is displayed on is home. How can I go about getting the "current controller", or controller before submission for lack of better words?

#19

[eluser]CroNiX[/eluser]
You could create a hidden form element and put it there. Then it will get sent along with the form and you can check it when logging in.
#20

[eluser]Kraig[/eluser]
Omg I don't know how I didn't think of that...I guess I was wondering if CI had a built in feature. Thank you!! Worked like a charm!

Are you good with cookies? I'm trying to reset the config['sess_expiration'] when the user checks remember me and logs in with
Code:
$this->config->set_item('sess_expiration', 10);
but it is not working.




Theme © iAndrew 2016 - Forum software by © MyBB