2017-06-05 102 views
-1

你好,我做了一個項目,我有休耕問題。 在我的索引頁,我有一個動態的佈局:PHP頭衝突

<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="index2.css"> 
</head> 
<body class="news"> 
    <header> 
    <div class="nav"> 
     <ul> 
     <li class="home"><a href="index2.php?page=welcome">Home</a></li> 
     <li class="logare"><a class="active" a href="index2.php?page=admin_login">Admin</a></li> 
     <li class="registration"><a href="index2.php?page=registration">Registration</a></li> 
     <li class="produse"><a href="index2.php?page=produse">Produse</a></li> 
     </ul> 
    </div> 
    </header> 

    <div id="content"> 
<?php 
$p=$_GET['page']; 

switch($p){ 
     case "welcome": 
      include('welcome.php'); 
      break; 
     case "admin_login": 
      include('Admin_login.php'); 
      break; 
     case "registration": 
      include('Registration.php'); 
      break; 
     case "produse": 
      include('produse.php'); 
      break; 
     default: 
      include('welcome.php'); 
      break; 
}?> 
</div> 


</body> 
</html> 
在默認

和迎賓情況下,S重定向我對我的welcome.php頁:

<?php 
    session_start(); 

if(!$_SESSION['email']) 
    { 

     header("Location: login.php");//redirect to login page to secure the welcome page without login access. 
    } 

    ?> 

<html> 
<head> 

<title> 
Registration 
</title> 
</head> 

<body> 
<h1>Welcome</h1><br> 
<?php 
    echo $_SESSION['email']; 
    ?> 


<h1><a href="logout.php">Logout here</a> </h1> 


</body> 

</html> 

,因爲我已經在歡迎重定向。 PHP的太,我的頭沒有從動態佈局加載。如何修復這個,但仍然重定向我登錄?

+0

我認爲你可以使用'output buffering'來實現這個 – RamRaider

+0

http://php.net/manual/en/function.error-reporting.php返回什麼?標題發送?你也應該添加一個退出到標題。 –

+0

你需要重新安排你的邏輯;你試圖設置一個頭文件(在welcome.php中)_after_你已經發送了內容到頁面(index.php)。此外,您還定義了兩個''標籤,兩個''標籤等,因此它是無效的HTML。創建一個導航文件並將該文件包含在welcome.php和其他頁面中。 –

回答

0

爲了避免PHP頭部衝突,我寧願使用JS重定向。請參閱我爲您修改的以下代碼。

<?php 
    session_start(); 

    if(!$_SESSION['email']) { 
     ?> 
      <script language="javascript" type="text/javascript"> 
       alert("User not logged in."); 
       window.location = 'login.php'; 
      </script> 
     <?php 
    }  
?> 

希望這會有所幫助。

+1

我個人不喜歡這個解決方案;禁用JavaScript的用戶不會受到影響。 –

+0

謝謝,但仍然在登錄頁面,我看不到標題 –