2015-01-13 86 views
-4

我有這樣的錯誤:警告:不能更改頭信息 - 頭已經發出PHP ERRORR

Warning: Cannot modify header information - headers already sent by (output started at D:\xamp\htdocs\talcioc\login.php:46) in D:\xamp\htdocs\talcioc\login.php on line 77

這就是今天的代碼(的index.php):當你說

<?php 
if(isset($_SESSION['IS_LOGEDIN']) == 'Y') 
{ 
header("location:index.php"); 
exit(); 
} 
?> 
<form action="" method="post"> 
    <table width="524" border="0" style="-webkit-border-radius:9px; 
    -moz-border-radius:9px; 
    -ms-border-radius:9px; 
    border-radius:9px; background-image:url(images/bgtable.png);" align="center"> 
    <tr> 
     <td width="514"><center><img src="pics/admin2.png" width="128" height="128" /></center></td> 
    </tr> 
    <tr> 
     <td><p>&nbsp;</p></td> 
    </tr> 
    </table> 
    <p>&nbsp;</p> 
    <table width="304" border="0" style="background-color:#000; -webkit-border-radius:9px; 
    -moz-border-radius:9px; 
    -ms-border-radius:9px; 
    border-radius:9px;" align="center"> 
    <tr> 
     <td width="78">Username:</td> 
     <td width="210"><input type="text" name="myusername" /></td> 
    </tr> 
    <tr> 
     <td>Password:</td> 
     <td><input type="password" name="mypassword" /></td> 
    </tr> 
    <tr> 
     <td>&nbsp;</td> 
     <td><input type="submit" value="Login!" name="submit" /></td> 
    </tr> 
    <tr> 
     <td>&nbsp;</td> 
     <td><img src="images/key.png" alt="" width="16" height="16" border="0" align="bottom" />&nbsp;<a href="index.php?pagina=forgotpass">Ti-ai uitat parola?</a></td> 
    </tr> 
    <tr> 
     <td>&nbsp;</td> 
     <td> 
     <?php 
if(isset($_POST['submit'])) 
{ 

if(!isset($_SESSION['IS_LOGEDIN'])) 
{ 
    $_SESSION['IS_LOGEDIN'] = 'N'; 
} 
// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes(mysql_real_escape_string($myusername)); 
$mypassword = stripslashes(mysql_real_escape_string($mypassword)); 

$encrypted_mypassword=md5($mypassword); 

$sql="SELECT * FROM administratori WHERE username='$myusername' and password='$encrypted_mypassword'"; 
$result=mysql_query($sql); 


// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 
// If result matched $myusername and $mypassword, table row must be 1 row 

if($count==1){ 
// Register $myusername, $mypassword and redirect to file "login_success.php" 
$_SESSION['myusername'] = $myusername; 
$_SESSION['IS_LOGEDIN'] = 'Y'; 
//$lastlogin = $_POST['date("d/m/y H:i:s", time()+25200)']; 
header("location:administrare.php"); 
} 
else { 
echo '<img src="images/delete.png" width="16" height="16" />' . '&nbsp;' .'User sau Password gresit!'; 
} 
} 
?> 
</td> 
    </tr> 
    </table> 
</form> 
+0

你知道有哪個被命名權的部分:'Related'看看那裏! – Rizier123

+2

錯誤告訴你到底發生了什麼問題。 –

+0

http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Zeeshan

回答

0

header("location:administrare.php");那是不好的,因爲在header之前,緩衝區中不應該有任何輸出。

將您的整個if(isset($_POST['submit']))條件移到文件頂部,在您檢查登錄狀態後。

0

header函數必須在任何輸出之前調用。

文件的結構必須是:

<?php 

if(isset($_SESSION['IS_LOGEDIN']) == 'Y') { 
    header("location:index.php"); 
    exit(); 
} 

if (isset($_POST['submit'])) { 
    // process form 
} 

?> 

<form> 
    ... 
</form> 
相關問題