简体   繁体   中英

redirect url to requested page after login codeigniter

I have an issue while login . when i click on a link and if i am not loggedin than it will redirect me to login page and after success login it should redirect me requested url but it redirect me to profile page. here is my view file:

<!--Left cols-->
<?php error_reporting(0);?>
<div class="col-lg-8 col-sm-8 col-md-8">
<div class="reg-form">
<h2>Member's Login</h2>
<title>Login</title>
<form method="post" action="<?php echo base_url();?>index.php/register_step1/login">

<div class="row">
<label for="name">Email</label>
<span><i class="icon-name"></i><input type="email" placeholder="example@gmail.com" required id="name" name="email"/></span>
</div>

<div class="row">
<label for="pass">Your Password</label>
<span><i class="icon-pass"></i><input type="password" placeholder="*************" id="pass" required name="password"/></span>
</div>
<!--input type="hidden" name="redirurl" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" /-->
<div class="row">
Forgot Password <a href="">click here</a> / <a href="">Register</a> new user
</div>

<div class="row">
<input type="submit" value="Login" name="submit"/>
</div>

</form>
</div>
</div>
<?php $this->load->view("home_template/rightbar");?>

And this is my controller :

public function login(){
        $this->load->model("User_model");
        $this->load->library('user_agent');

        if(isset($_POST["submit"])){ 
            //$url = $this->input->post("redirurl");
                $data = $this->User_model->login();
                if($data){
                    foreach($data as $dta){
                        $id = $dta->id;
                        $email = $dta->email;
                    }

                    $session_array = array("id"=>$id,"email"=>$email);
                    $this->session->set_userdata("logged_by_login",$session_array);

                    redirect("profile");

                }
            }
                else{

                }


        $data["main_content"] = "login_view";
        $this->load->view("home_template/template",$data);
    } 

You can use redirect like this. Might be controller not get correct path,

$this->load->helper('url');
redirect(base_url('profile'));

Let me know if it not works.

  • for page redirection we used redirect() statement in codeigniter. redirect() sends the user to the specified web page using a redirect header statement.

  • redirect() statement resides in the URL helper so we need to load url helper:

    $this->load->helper('url');

  • profile page redirection we write bellow statement and pass first parameter as "Profile" and second last parameter is "refresh/location".

    redirect('profile','refresh');

  • The redirect function loads a local URI specified in the first parameter of the function call and built using the options specified in your config file.

  • The second parameter allows the developer to use different HTTP commands to perform the redirect "location" or "refresh".

  • According to the Code Igniter documentation: "Location is faster, but on Windows servers it can sometimes be a problem." so better to use "refresh" as second parameter

change your code to

un comment your code " /-->

change to

<!input type="hidden" name="redirurl" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />

and in your controler change code as shown below

if($data){
                foreach($data as $dta){
                    $id = $dta->id;
                    $email = $dta->email;
                }

                $session_array = array("id"=>$id,"email"=>$email);
                $this->session->set_userdata("logged_by_login",$session_array);

                redirect("profile");
                 if(!empty($this->input->post('redirurl'))){
                        redirect($this->input->post('redirurl'),'refresh'); 
                    }else{
                        redirect('profile','refresh'); 
                    }

            }

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