Codeigniter:弹出消息后无法重定向

Codeigniter: unable to redirect after popup message

echo '<script>alert("Your session has timeout, please relogin again!");</script>'; 
            redirect('main/login', 'refresh');

这是错误:

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/swtwnonu/public_html/ignite_project/application/controllers/member.php:19)

Filename: helpers/url_helper.php

Line Numer: 540

我想将该人重定向到我的 main/login 页面,在他为我显示的消息单击“确定”之后,但我不知道为什么会发生错误。请帮忙!

使用 HTTP headers 重定向工作,无法在任何输出后发送(文本、HTML、不可见字符、Javascript,无论什么...)。

如果您使用 Javascript 打印消息,您仍然可以使用它重定向:

echo '<script>alert("Your session has timeout, please relogin again!"); window.location.replace = \'index.php/main/login\'; window.location.href = \'index.php/main/login\';</script>';

您也可以使用 HTML <meta /> 标签来完成:

<meta http-equiv="refresh" content="0; url=index.php/main/login" />

此标记将在加载网站后立即重定向,您可以将 0 更改为任何值(以秒为单位)以在重定向前等待。