php Codeigniter 控制器返回上一页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37349470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:11:20  来源:igfitidea点击:

Codeigniter Controller return to previous page

phpcodeigniter

提问by luka032

I'd like to ask you how can I instead of $this->load->view('some_view.php') at the end of controller code, return user to page from where he invoked controller method? Simple return statement is not working.

我想问你如何在控制器代码的末尾而不是 $this->load->view('some_view.php') ,将用户返回到他调用控制器方法的页面?简单的 return 语句不起作用。

ie.

IE。

public function someMethod($IDCustomer) {

     $this->Some_modal->persist($IDCustomer);   
     // how to return to previous page instead of line after?
     // i've used $this->load->view('someView.php');
}

回答by CodeGodie

This should help http://www.codeigniter.com/user_guide/libraries/user_agent.html

这应该有助于http://www.codeigniter.com/user_guide/libraries/user_agent.html

$this->load->library('user_agent');
if ($this->agent->is_referral())
{
    echo $this->agent->referrer();
}

or straight PHP:

或直接PHP:

redirect($_SERVER['HTTP_REFERER']);

回答by luka032

I've found answer on some thread.

我在某个线程上找到了答案。

In the page that you want to go back to you can do:

$this->session->set_userdata('referred_from', current_url());

Then redirect back to that page

$referred_from = $this->session->userdata('referred_from');
redirect($referred_from, 'refresh');

在您要返回的页面中,您可以执行以下操作:

$this->session->set_userdata('referred_from', current_url());

然后重定向回那个页面

$referred_from = $this->session->userdata('referred_from');
redirect($referred_from, 'refresh');

回答by Melvin Villas

I've tried header('location:'.$_SERVER['HTTP_REFERER']);and it's working quite well.

我试过了header('location:'.$_SERVER['HTTP_REFERER']);,效果很好。

Just a one-liner plain old PHP code.

只是一个单行普通的旧 PHP 代码。

回答by santosh ghodke

Use the REDIRECT_QUERY_STRINGalternative of HTTP_REFERRER:

使用以下REDIRECT_QUERY_STRING替代方法HTTP_REFERRER

// set in session redirect back URL in your common is_logged_in function if user is not logged in

$CI->session->set_userdata('redirect_back', $_SERVER['REDIRECT_QUERY_STRING']); 


// below code user after successful login in auth.php library

if($this->ci->session->userdata('redirect_back')){
    $redirectBackUrl = $this->ci->session->userdata('redirect_back');
    $this->ci->session->unset_userdata('redirect_back');
    redirect(base_url() . $redirectBackUrl);
}