2015-02-24 145 views
0

我有一个看起来像这样的index.php的网站:重定向在PHP中不包括重定向主页

<?php 
ob_start(); 
include_once 'config.php'; 
include_once 'dbconn.php'; 


session_start(); 


?> 
<html> 
<body> 
<p>Some content</p> 
<br> 
<?php include_once 'loginform.php'; ob_end_flush(); ?> 
</form> 
</body> 
</html> 

loginform.php检查用户的cookie,看看他们是否已登录,如果所以重定向到account.php:

$regAddr = mysqli_query($conn, "SELECT * FROM users WHERE address = '$addr'"); 
$addrRow = mysqli_num_rows($regAddr); 

//check if address is in db 
if($addrRow !== 0) { 
    header("Location: account.php"); 

如果他们没有登录,它会显示一个登录表单。 我这里有两个问题:

  1. 如果我删除ob_start()及ob_end_flush()函数,头被发送的包括行,我不能重定向。
  2. 如果我离开他们,用户登录,整个index.php重定向到account.php。

有什么办法可以将login.php重定向到account.php,同时保持index.php静态(不刷新)和不使用iframe?

回答

1

否。整个文档将被重定向,因为您认为loginform.php的行为类似于iframe,但其行为与整个文档的一部分相似。

你有一堆可用的选项来实现这一点...使用iframe是我不推荐的东西,而是使用类或函数来验证用户登录,然后根据该结果包括一个文件。

<?php 
if($logedin) { 
    include("dashboard.php"); 
} else { 
    include("loginform.php"); 
} 

显然,这可以在很多方面,我建议使用验证会话将呈现观点类和类,所以您不必重复HTML标题或类似的东西的实现你要加载的每个视图。

我用于我的一个系统的实际代码。

<?php 
include_once("../models/class-Admin.php"); 

class AdminViewRender { 

    public static function render() { 
     $request = "home"; 
     $baseFolder = "../views/admin/"; 

     //index.php?url=theURLGoesHere -> renders theURLGoesHere.php if 
     //exists, else redirects to the default page: home.php 
     if(isset($_GET["url"])) { 
      if(file_exists($baseFolder.$_GET["url"].".php")) { 
       $request = $_GET["url"]; 
      } else { 
       header("Location: home"); 
      } 
     } 

     $inc = $baseFolder.$request.".php"; 
     if($request !== "login") { //if you are not explicitly requesting login.php 
      $admin = new Admin(); 
      if($admin->validateAdminSession()) { //I have a class that tells me if the user is loged in or not 
       AdminPanelHTML::renderTopPanelFrame(); //renders <html>, <head>.. ETC 
       include($inc); //Includes requestedpage 
       AdminPanelHTML::renderBottomPanelFrame(); //Renders some javascript at the bottom and the </body></html> 
      } else { 
       include($baseFolder."login.php"); //if user validation (login) fails, it renders the login form. 
      } 
     } else { 
      include($inc); //renders login form because you requested it 
     } 

    } 

} 
+0

完美无瑕地工作,它呈现合适的文件。但现在我有另一个问题。当我提交登录表单时,它可以工作,但屏幕上没有任何变化,表单会一直保留,直到刷新页面,我试图避免这种情况。与注销一样。 – 2015-02-24 04:11:49

+0

我认为问题是ob_star和flush .... ob_start必须是整个PHP文件中的第一行。 flush()最后..试试让我知道 – JuanBonnett 2015-02-24 04:20:33

+0

我已经删除它们,因为现在我没有使用标题重定向。您的答案只适用于页面加载,所以我需要用已更新的页面“更新”包含内容。 – 2015-02-24 04:26:11