[英]How to redirect user to current page after login (Codeigniter)
登錄后如何將用戶重定向到當前頁面?
用戶在這個網址閱讀新聞
domain.com/article/news-18565
一旦他點擊登錄按鈕,然后我怎么之前會得到CURRENTURL()domain.com/login
和用戶登錄后,應及時到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');
}
}
}
模型.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網址。 如您所說,http Referer URL將用戶重定向到上次訪問的URL
例如:成員單擊登錄按鈕后,正在該URL domain.com/article/news-18565上閱讀新聞,然后在進入domain.com/login之前以及在登錄后成員應該進入domain.com之后如何獲取currenturl() / 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
}
希望有任何問題讓我知道
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.