php 重定向到 codeigniter 中的引用 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11374759/
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-25 00:22:14  来源:igfitidea点击:

Redirect to referer url in codeigniter

phpcodeigniterhttp-referer

提问by Prathamesh mhatre

In messaging system of my project when you get a message from a user you a email alert saying that the another user has sent a message to view the message click here (i.e the url of message) So if the user is not logged in to system he gets redirect to login page and after login it should get back to the referer url. I have made a basecontoller in core folder and extending the CI_controller the authenticating code is as follows.

在我的项目的消息传递系统中,当您收到来自用户的消息时,您会收到一封电子邮件警报,说明另一个用户已发送消息以查看消息,请单击此处(即消息的 url)因此,如果用户未登录系统他被重定向到登录页面,登录后它应该返回到引用网址。我在核心文件夹中创建了一个 basecontoller 并扩展了 CI_controller 身份验证代码如下。

function authenticate($type = 'user')
    {
        if($type == 'user')
        {
            if($this->user_id)
            {
                // user is logged in. check for permissions now
            }
            else
            {
                // user isnt logged in. store the referrer URL in a var.
                if(isset($_SERVER['HTTP_REFERER']))
                {
                    $redirect_to = str_replace(base_url(),'',$_SERVER['HTTP_REFERER']);
                }
                else
                {
                    $redirect_to = $this->uri->uri_string();
                }            

                redirect('user/login?redirect='.$redirect_to);
                exit;
            }
        }

        if($type == 'admin')
        {
            if($this->session->userdata('admin_id') && $this->session->userdata('user_type') ==5)
            {
                // Admin is logged in
            }
            else
            {
                redirect('admin/login');
                exit;
            }
        }
    }

The referer url is "http://example.com/project/pm/view_conversation?id=11" now the problem is I am getting referer url till view_conversation and not able to get the id part.

引荐网址是“http://example.com/project/pm/view_conversation?id=11”,现在的问题是我正在获取引荐网址,直到 view_conversation 并且无法获得 id 部分。

Any suggestion ?

有什么建议吗?

Thank you.

谢谢你。

回答by Adrian P.

回答by maxxon15

How about just

刚刚怎么样

redirect($_SERVER['HTTP_REFERER']);

Using php's $_SERVERglobal variable.

使用 php 的$_SERVER全局变量。

This worked for me!

这对我有用!

回答by Binayak Das

Put that code in your Login Controler

将该代码放在您的登录控制器中

function index() {
    $this->load->library('user_agent');  // load user agent library

    //Set session for the referrer url
    $this->session->set_userdata('referrer_url', $this->agent->referrer() );  
}

After Login Redirection Code

登录后重定向代码

// user is authenticated if referrer is there
if( $this->session->userdata('referrer_url') ) {
    //Store in a variable so that can unset the session
    $redirect_back = $this->session->userdata('referrer_url');
    $this->session->unset_userdata('referrer_url');
    redirect( $redirect_back );
}

回答by machineaddict

Because you have double question mark in the url, the browser ignores the url part after the second one. Use urlencodefor you redirect part, like so:

因为你在 url 中有双问号,所以浏览器会忽略第二个之后的 url 部分。使用urlencode重定向部分,如下所示:

redirect('user/login?redirect='.urlencode($redirect_to));

I've tested it out and it works this way.

我已经测试过了,它是这样工作的。

回答by Patrick Savalle

By default CI is configured to ignore the query part of the URL (the part after the '?').

默认情况下,CI 配置为忽略 URL 的查询部分('?' 之后的部分)。

See: http://codeigniter.com/user_guide/general/urls.html

请参阅:http: //codeigniter.com/user_guide/general/urls.html