在 Codeigniter 中重定向

发布于 2024-12-19 04:23:33 字数 622 浏览 4 评论 0原文

我有一个分页显示,在每个页面调用中我将当前 URI 字符串设置到会话中(在我的控制器中)。

$current = $this->uri->uri_string();
$this->session->set_userdata('return_to', $current);

从视图中,用户可以单击链接转到其他地方(编辑表单),提交后(并且表单验证正确)我想返回到分页结果的正确页面。

if($this->form_validation->run('edit') == TRUE )
{
    $back_to = NULL;
    $back_to = '/'.$this->session->userdata('return_to');
    ....
    redirect($back_to);
}

似乎有时在 Firefox 中工作,但在 Chrome 中死机,重定向到 favicon.ico

任何和所有帮助都表示赞赏

更新: 我的问题不是由于相对/绝对地址造成的,由于第二个控制器中的某些奇怪原因,重定向的目标是 favicon.ico,而不是会话中存储的内容。

I have a paginated display in which on each page call I set the current URI string into a session (in my controller).

$current = $this->uri->uri_string();
$this->session->set_userdata('return_to', $current);

From the view the user can click a link to go somewhere else (an edit form), which when submitted (and form validation is correct) I want to return to the correct page of the paginated results.

if($this->form_validation->run('edit') == TRUE )
{
    $back_to = NULL;
    $back_to = '/'.$this->session->userdata('return_to');
    ....
    redirect($back_to);
}

Seems to work sometimes in Firefox but dies in Chrome, redirect to favicon.ico

Any and all help appreciated

UPDATE:
My problem isn't due to relative/absolute addresses, for some weird reason in the second controller the redirect aims to favicon.ico, not what was stored in the session.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-12-26 04:23:33
if($this->form_validation->run() == TRUE )
{
    if($this->session->userdata('return_to')
    {
       redirect($this->session->userdata('return_to'));
    }
    else
    {
      redirect(base_url());
     // or trigger some error
    }
}

不需要分配变量,redirect() 使用完整的 url 和分段的 url(很像 site_url(),这正是 uri_string() 返回的内容)因此

,如果这不起作用,则可能是您的会话未设置或已过期。此外,请考虑 CI 的会话是 cookie ,因此浏览器可以在其中发挥作用。

if($this->form_validation->run() == TRUE )
{
    if($this->session->userdata('return_to')
    {
       redirect($this->session->userdata('return_to'));
    }
    else
    {
      redirect(base_url());
     // or trigger some error
    }
}

Don't need to assign a variable, redirect() uses both a full url and a segmented url (much like site_url(), which is exactly what is returned by uri_string().

So if this doesn't work, it's likely your session is not set or expired. Also, consider that CI's sessions are cookies , so a browser can has its role in here.

苦笑流年记忆 2024-12-26 04:23:33

我会使用绝对 URL,而不是相对 URL。

尝试一下

if($this->form_validation->run('edit') == TRUE )
{
    $back_to = NULL;
    $back_to = 'http://www.mydomain.com/'.$this->session->userdata('return_to');
    ....
    redirect($back_to);
}

正如 Stealthyninja 指出的那样,最好使用 CodeIgniter 的 site_url() 或 base_url() 而不是对其进行硬编码。在 config.php 中设置 site_url() (我相信)

I would use an absolute URL and not a relative one.

Try this

if($this->form_validation->run('edit') == TRUE )
{
    $back_to = NULL;
    $back_to = 'http://www.mydomain.com/'.$this->session->userdata('return_to');
    ....
    redirect($back_to);
}

As stealthyninja pointed out it's probably better to use CodeIgniter's site_url() or base_url() instead of hard coding it. Set the site_url() in the config.php (i believe)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文