如何在使用PHP和codeigniter一段时间后重定向用户?


How can I redirect the user after an amount of time using PHP and codeigniter?

我有一个页面,我想加载并显示一段时间。然后应该将用户重定向回索引视图。

下面的代码是我的控制器。

function send()
{
    $data = array(
        'author' => $_POST['author'],
        'header' => $_POST['header'],
        'paragraph' => $_POST['paragraph']
    );
    $result = $this->database_model->insert_into_articles($data);
    if( $result )
    {
        $view['content'] = "articles_send_view";
            $this->load->view('includes/template',$view );
    }
}

以下是我的观点。

<div id="content">
<div id='other_content'>
<p>
    article sent thanks 
</p>
</div>

我不相信这可以单独用PHP完成。在HTML中有两个选项:

<head>中的<meta http-equiv="refresh">标记可用于在指定的秒数后重新加载或重定向用户。维基百科有几个例子:

放置在<head>内,5秒后刷新页面:

<meta http-equiv="refresh" content="5">

5秒后重定向到http://example.com/:

<meta http-equiv="refresh" content="5; url=http://example.com/">

立即重定向到http://example.com/:

<meta http-equiv="refresh" content="0; url=http://example.com/">

另一个选择是使用JavaScript重定向用户。您可以通过修改window.location属性,使用setTimeout函数来延迟调用,例如,延迟5秒(5000毫秒)。

var redirect = function() {
    window.location = "/index.html";
};
setTimeout(redirect, 5000);

<meta>重定向应该适用于几乎所有的访问者,而JavaScript将无法工作,如果他们有JavaScript禁用。但是,它们不会以任何方式冲突,因此为了安全起见,您可以将它们都包含在您的页面中。