2017-02-21 73 views
-1
<?php 
    session_start(); 
    if (empty($_SESSION['id'])) { 
    $_SESSION['errMsg'] = '<div class="alert alert-danger" role="alert"> 
    <strong>Post:</strong> You must be logged in `enter code here`in order to post. 
    </div>'; 
    header('Location: ../../index.php'); 
    exit; 
    } 
?> 

上运行它只是没有我重定向到index.php,这是在合适的地方,即使会话不存在。头似乎并不Web服务器

+0

你试过了什么?你检查了你的日志文件吗?当你删除“退出”时会发生什么? – Oliver

+0

没有退出;它似乎仍然不起作用。我会检查它是否可以在没有GZip的情况下运行,但这应该不成问题。 –

+0

是的,在重定向时必须退出。在标题功能中使用FQDN(意味着像http://example.com/index.php这样的绝对url)。 – Oliver

回答

0

我明白您的重定向问题

从上面的代码

header('Location: ../../index.php'); 

你的项目是example.com/testfolder/view.php

做出改变的路径,如果你的index.php在之前的文件夹中意味着只需添加

../index.php 

同样的监视器并在标题中设置路径。

+0

它仍然不起作用。在我的本地主机上它工作,但它只是无法在Strato(网域主机) –

+0

我认为你的代码或你的主机有一些问题。好吧,只需通过Javascript试试吧

0

只是尝试: -

header('Location: http://myhost.com/mypage.php'); 

PHP header()

+0

很遗憾,这也不起作用 –

+0

首先在url中打开index.php页面,然后添加如上所示的标题。 –

0
<?php 
    session_start(); 
    if (!isset($_SESSION['id'])) { 
    header('Location: ../../index.php'); 
    exit; 
    } 
?> 

我固定它,我不知道怎么回事,但现在的工作。这是我现在使用的代码

+0

请在这里阅读http://stackoverflow.com/questions/7191626/isset-and-empty-what-to-use然后你就会明白为什么它现在工作 –