简体   繁体   中英

How to maintain current URL in CodeIgniter

once again I'm lost. I'm gonna start ahead with the issue, because I am not even sure what I really want.

Example: I have an article view. The URL is: http://index.php/news/article/3 This actually runs the article($id) function in my news controller and gives it the 3 as an argument. The function then fetches the article information and displays it in the view.

On the article page, the user can also log in. Logging in is triggered on pressing submit button inside my form form_open('core/login') ...button... </form> In the function I log the user in and refresh the current view with some elements changed according to the user being logged. The problem is that the URL is now http://index.php/core/login . Obviously I would like it to be the original URL.

Is there any, possibly simple, solution to achieve this? Thank you all for reading and in advance for your replies.

Difficult without more code but let me give you my theory / take on this:

Default controller:

  1. User is not logged in - show default header, content, footer
  2. User presses login, form is shown
  3. User is authenticated - yes (continue) no (go back to form)
  4. User is redirect('home')'d
  5. Your default controller/home controller checks if auth'd: if authorised then show logged in header, content and logged in footer or simply pass 'loggedIN' as a $data['loggedIN'] variable to the view - but this breaks the ideology of MVC framework.

More info from you and I can be more specific, or we can talk on IRC.

Adding this code from a controller i'm working on right this minute - I use ion_auth library (you should look it up, it's excellent).

This is my default controller - and as you can see some simple logic loads the different views/states.

public function index(){


        if ($this->data['auth_login'] ) {

            /*is already signed in so just present the lobby?*/
            $data['page_title'] = "HN Lobby";
            $data['menuItems'] = nav_anchor_helper_authd();
            $data['myUserID'] = $this->ion_auth->get_user_id();

            $data['lobby_players'] = $this->lobby_model->get_players();

            $this->load->view('template/public/header',$data);
            $this->load->view('player_pages/nav_2',$data);
            $this->load->view('player_pages/lobby',$data);



            $this->load->view('template/scripts/main',$data);/*scraper and other scripts*/
            $this->load->view('template/public/footer',$data);

        } else {

            /*request login*/
            $data['page_title'] = "PLAY HN";
            $data['menuItems'] = nav_anchor_helper();

            $data['auth_rtnurl'] = current_url();
            $data['auth_conturl'] = current_url(); /*for now just come back to same page where lobby should load - perhaps in future a semi gooey login? e.g where was user going - this could be the continue url in this var right here << */

            $data['message_body'] =   $this->session->flashdata('message');


            $this->load->view('template/public/header',$data);
            $this->load->view('template/public/nav_1',$data);
            $this->load->view('public_pages/play',$data);
            $this->load->view('template/public/footer',$data);
        }
    }

Here is how I handle the return URL in my login function: Login/auth controller: (using ion_auth)

function login()
    {
        $this->data['title'] = "Login";

        //validate form input
        $this->form_validation->set_rules('identity', 'Identity', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run() == true)
        {


            if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
            {
                //if the login is successful
                //redirect them back to the home page
                $this->session->set_flashdata('message', $this->ion_auth->messages());



                $rtnurl = $this->input->post('auth_conturl');
                if(!$rtnurl || $rtnurl == ""){
                    $rtnurl = '/';
                }

                redirect($rtnurl, 'refresh');
            }

This is only an extract/segment of the login function - but as you can see i utilise the function 'redirect' from code igniter to push the user back to the return URL posted with the login form (which was set in the view/previous controller using the current_url() function.

Finally my default view file with login form to show you how i am passing the return url:

<div>

    <h4>Login</h4>
    <div id="infoMessage" class="errortext"><?php echo $message_body;?></div>


    <?php echo form_open('auth/login', array('class' => 'form col')); ?>

      <p>
        <label for="identity">Email:</label>    <input type="text" name="identity" value="" id="identity">  </p>

      <p>
        <label for="password">Password:</label>    <input type="password" name="password" value="" id="password">  </p>

      <p>
        <label for="remember">Remember Me:</label>    <input type="checkbox" name="remember" value="1" id="remember">  </p>


      <p><input type="submit" name="submit" value="Login &raquo;"></p>


    <p>
        <a href="<?php echo site_url(); ?>/auth/forgot_password">Forgot password</a> ?
    </p>



      <input type="hidden" name="auth_rtnurl" value="<?php echo $auth_rtnurl; ?>"/>
      <input type="hidden" name="auth_conturl" value="<?php echo $auth_conturl; ?>"/>

    <?php echo form_close();?>
</div>

To use the current dynamic url as form action, just use -

<?= form_open(current_url()); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM