2016-08-14 99 views
-2

所以我有一個基本的登錄系統。我試圖在成功登錄後使用標題重定向。但我得到一個錯誤,打印出「無法修改標題」我完全不熟悉PHP。但所有的幫助將不勝感激。無法修改標題信息 - 標題已發送

錯誤消息

Warning: Cannot modify header information - headers already sent by (output started at /home4/jachun39/public_html/ap/login.php:6) in /home4/jachun39/public_html/ap/login.php on line 22 

的login.php文件

<?php 
include('SqlConnect.php'); 
?> 
<?php 
if (isset($_POST['Login'])){ 
    if ([email protected]_connect($host, $username, $password)) die("Can't connect to database"); 
    if (!mysql_select_db($db_name)) die("Can't select database"); 
    $username=$_POST['username']; 
    $password=$_POST['password']; 
    $username = stripslashes($username); 
    $password = stripslashes($password); 
    $username = mysql_real_escape_string($username); 
    $password = mysql_real_escape_string($password); 
    $sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'"; 
    $result=mysql_query($sql); 
    $count=mysql_num_rows($result); 
    if($count >= 1){ 
    $_SESSION['username']= "username"; 
    $_SESSION['password']= "password"; 
    header("location: index.php?message=success"); 
    echo "<center><font color='green'><b>Logged in Successfully</center></font></b>"; 
    } else { 
     echo "<center><font color='red'><b>Wrong username or password</center></font></b>"; 
    } 
} 
    <link rel="Stylesheet" type="text/css" href="style.css" /> 
    <link href='http://fonts.googleapis.com/css?family=Karla:400,700,700italic,400italic' rel='stylesheet' type='text/css'> 

這裏也是我Checklogin.php文件,該文件是 「包含」 在網站的首頁頂部。

<?php 
if(!isset($_SESSION['username'])){ 
header("location: login.php"); 
} 
?> 

///添加請求的(的index.php)

<?php 
ob_start(); 
include('CheckLogin.php'); 
include('SqlConnect.php'); 
include('Userbar.php'); 
?> 
<link rel="Stylesheet" type="text/css" href="style.css" /> 
<link href='http://fonts.googleapis.com/css?family=Karla:400,700,700italic,400italic' rel='stylesheet' type='text/css'> 
</html> 
<body> 
<br> 
<form name="form" method="POST" action=""><td> 
<table width="325" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="#212121"> 
<td><table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#404040"></td> 
<tr colspan="3"><strong> <font color="ECECEC"> Create Code </font></strong></tr> 
<tr> 
<td><center><font color="ECECEC">Code: <input name="code" type="text"></font> 
<input type="Submit" value="Create Code" name="Addcode" /></td></tr></center> 
</table></table> 
</table></table> 
</form> 
<?php 
if (!mysql_connect($host, $username, $password)){ 
    die("Can't connect to database"); 
} 
if (!mysql_select_db($db_name)){ 
    die("Can't select database"); 
} 
if (isset($_POST['Addcode'])){ 
    mysql_query("INSERT INTO $table (username, password, hwid, ip, code, banned) VALUES('-','-','-','-','$_POST[code]','0')"); 
} 
if (isset($_POST['Action'])){ 
    if ($_POST['Action'] == "Delete"){ 
     $ID = $_POST['ID']; 
     $result = mysql_query("DELETE FROM $table WHERE `id` = $ID"); 
     if (!$result){ 
      die(mysql_error()); 
     } 
    } elseif ($_POST['Action'] == "Ban"){ 
     $ID = $_POST['ID']; 
     $Banned = $_POST['Banned']; 
     if ($Banned == 0){ 
      $result = mysql_query("UPDATE $table SET `banned` = '1' WHERE `id` = '".$ID."'"); 
     } else { 
      $result = mysql_query("UPDATE $table SET `banned` = '0' WHERE `id` = '".$ID."'"); 
     } 
     if (!$result){ 
      die(mysql_error()); 
     } 
    } 
} 
echo "<div class='table' align='center'><table><tr class='top'>"; 
echo "<td \"Username\"'>Username<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>"; 
echo "<td \"Password\"'>Password<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>"; 
echo "<td \"Hwid\"'>Hwid<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>"; 
echo "<td \"Code\"'>Code<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>"; 
echo "<td \"IP\"'>IP<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>"; 
echo "<td \"Active\"'>Active<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>"; 
echo "<td \"Delete\"'>Delete<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>"; 
echo "</tr>\n"; 
$type = "first"; 
$query = mysql_query("SELECT * FROM $table"); 
while($row = mysql_fetch_array($query)){ 
    $ID = $row['id']; 
    $Username = $row['username']; 
    $Password = $row['password']; 
    $Hwid = $row['hwid']; 
    $Code = $row['code']; 
    $IP = $row['ip']; 
    $Banned = $row['banned']; 
    $Delete = "0"; 
    echo '<td>'.$Username.'</td><td>'.$Password.'</td><td>'.$Hwid.'</td><td>'.$Code.'</td><td>' 
     .$IP.'</td><td>'; 
     ?> 
     <form action="" class="form" method="POST"> 
     <input type="hidden" name="Action" value="Ban" /> 
     <input type="hidden" name="ID" value=<?php echo "$ID";?> /> 
     <input type="hidden" name="Banned" value=<?php echo "$Banned";?> /> 
     <?php 
     if ($Banned == 1){ 
      echo '<input type="image" src="img/cross.png" name="Ban" />'; 
     } else { 
      echo '<input type="image" src="img/tick.png" name="Ban" />'; 
     } 
     ?> 
     </td><td> 
     </form> 
     <form action="" class="form" method="POST"> 
     <input type="hidden" name="Action" value="Delete" /> 
     <input type="hidden" name="ID" value=<?php echo "$ID";?> /> 
     <input type="image" src="img/delete.png" name="Delete" /> 
     </form> 
     </td> 
     <?php 
    echo "</tr>\n"; 
} 
?> 
</body> 
</html> 

回答

-1

放在上面這條線在您php標籤開始。

ob_start(); 

喜歡這裏,您可以:

<?php 
ob_start(); 
include('SqlConnect.php'); 
?> 
+0

當我這樣做,它不顯示成功登錄的消息了,只是重新加載 – John

+0

它重新加載,因爲你的頭(「位置:index.php」) ;'這行代碼。 嘗試評論這一行,並看到結果一次 –

+0

是的,現在它顯示的消息,但我如何使其重定向到index.php?因爲它已成功登錄 – John

0

PHP頭必須在頁面的任何內容之前發送。從PHP文檔:

的HTTP狀態標題行永遠是首先發送到客戶端 ,而不管實際的報頭()調用爲所述第一或不是。 除非已經發送了HTTP標頭,否則可以隨時通過調用標頭()以及新狀態 行來覆蓋狀態。

在您的文件中,您在設置導致此錯誤的標頭之前發送了HTML鏈接標記。簡單地把你的PHP之前的任何輸出,它會工作

+0

是的,但是如何將頁面重定向到index.php,它只是保留在login.php上並重新加載頁面。更新代碼在帖子 – John

+0

檢查你的index.php,你可能會重定向到login.php! –

+0

你可以檢查一下嗎?我在主要問題中添加了index.php的代碼! – John

相關問題