登录后如何将用户重定向到当前页面(Codeigniter)
How to redirect user to current page after login (Codeigniter)
如何在登录后将用户重定向到当前页面?
User is reading a news at this URL domain.com/article/news-18565
once he click the login button then how do i get currenturl() before going to domain.com/login
and after login user should go to domain.com/article/news-18565
Form.html
<form action="signin" method="post" accept-charset="utf-8">
<div class="form-group">
<label for="">Email:</label>
<input type="text" name="email" value="" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for="">Password:</label>
<input type="password" name="password" value="" class="form-control form-control-lg">
</div>
<div class="form-group">
<input type="submit" name="submit" value="Sign in to your account" class="float-md-right btn btn-primary">
</div>
</form>
Controller.php
function index(){
$user_profile = 'user';
$this->authModel->loggedin() == FALSE || redirect($user_profile);
$rules = $this->authModel->rules;
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == TRUE){
if($this->authModel->login() == TRUE){
redirect($user_profile);
}else{
$this->session->set_flashdata('error', 'That email/password combination does not exist');
redirect('signin');
}
}
}
Model.php
function login(){
$user = $this->get_by(array(
'email' => $this->input->post('email', TRUE),
'password' => $this->hash($this->input->post('password'))
), TRUE);
if(count($user)){
$data = array(
'name' => $user->name,
'email' => $user->email,
'username' => $user->username,
'loggedin' => TRUE
);
$this->session->set_userdata($data);
return TRUE;
}
}
嗯,你需要使用 HTTP_REFERER url。 http Referer url 将用户重定向到上次访问的 url 正如你所说
E.g: Member is reading a news at this URL domain.com/article/news-18565 once he click the login button then how to get currenturl() before going to domain.com/login and after login member should go to domain.com/article/news-18565
为此,您需要检查上次访问的 url 参见代码示例。
$this->session->set_userdata($data);
if(!empty($_SERVER['HTTP_REFERER'])){
redirect($_SERVER['HTTP_REFERER']);
} else {
// add here the default url for example dashboard url
}
希望对你有帮助,有什么问题可以私信我
如何在登录后将用户重定向到当前页面?
User is reading a news at this URL
domain.com/article/news-18565
once he click the login button then how do i get currenturl() before going todomain.com/login
and after login user should go todomain.com/article/news-18565
Form.html
<form action="signin" method="post" accept-charset="utf-8">
<div class="form-group">
<label for="">Email:</label>
<input type="text" name="email" value="" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for="">Password:</label>
<input type="password" name="password" value="" class="form-control form-control-lg">
</div>
<div class="form-group">
<input type="submit" name="submit" value="Sign in to your account" class="float-md-right btn btn-primary">
</div>
</form>
Controller.php
function index(){
$user_profile = 'user';
$this->authModel->loggedin() == FALSE || redirect($user_profile);
$rules = $this->authModel->rules;
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == TRUE){
if($this->authModel->login() == TRUE){
redirect($user_profile);
}else{
$this->session->set_flashdata('error', 'That email/password combination does not exist');
redirect('signin');
}
}
}
Model.php
function login(){
$user = $this->get_by(array(
'email' => $this->input->post('email', TRUE),
'password' => $this->hash($this->input->post('password'))
), TRUE);
if(count($user)){
$data = array(
'name' => $user->name,
'email' => $user->email,
'username' => $user->username,
'loggedin' => TRUE
);
$this->session->set_userdata($data);
return TRUE;
}
}
嗯,你需要使用 HTTP_REFERER url。 http Referer url 将用户重定向到上次访问的 url 正如你所说
E.g: Member is reading a news at this URL domain.com/article/news-18565 once he click the login button then how to get currenturl() before going to domain.com/login and after login member should go to domain.com/article/news-18565
为此,您需要检查上次访问的 url 参见代码示例。
$this->session->set_userdata($data);
if(!empty($_SERVER['HTTP_REFERER'])){
redirect($_SERVER['HTTP_REFERER']);
} else {
// add here the default url for example dashboard url
}
希望对你有帮助,有什么问题可以私信我