问题
can you help me out here?
i transfer to a new hosting and i suddnley getting this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/capital/public_html/Google/Connect.php:1) in /home/capital/public_html/Google/CheckLogin.php on line 25
this is my script:
<?php
session_start();
$server = 'localhost';
$user = '***';
$pass = '***';
$db = '***';
// Connect to Database
$connection = mysql_connect($server, $user, $pass) or die ("Could not connect to server ... \n" . mysql_error ());
mysql_select_db($db);
$my_t=getdate(date("U"));
$DateTime = ("$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]");
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql=mysql_query("SELECT * FROM Users WHERE Username='$myusername' and Password='$mypassword' and status='1'")or die(mysql_error());
if(mysql_num_rows($sql) > 0){
while($count=mysql_fetch_array($sql)){
$_SESSION['user']=$count['userid'];
$TodayLogin = $count['TodayLogin'];
}
mysql_query("UPDATE Users SET LastLogin = '$TodayLogin' WHERE userid = '$_SESSION[user]'");
mysql_query("UPDATE Users SET TodayLogin = '$DateTime' WHERE userid = '$_SESSION[user]'");
header("location:index.php");
}
else
{
echo "<div style='text-align:center;font-family:arial;font-size:32px;font-weight:bold;'>user or password incorrect</div>";
}
?>
回答1:
check that you dont have anything before the <?php
tag.
the problem is that if you have anything before the start tag then it get translated to html and sent with the header before the rest of the page is created. later you try to send another header after one already was sent.
i found this steps to follow:
1) Find the header() statement that is causing the problem. The error must be at or before this line.
2) Look for any statements that could send output to the user before this header statement.
If you find one or more, find some way to move the header statement before them. Complex conditional statements may complicate the issue, but they may also help solve the problem.
Consider a conditional expression at the top of the PHP script that determines the header value as early as possible and sets it there.
3) Make sure there is no white space outside of the php start and end tags. While a blank line before the
<?php
start tag may look innocent, when processed by PHP, it will turn into an echo statement printing out a blank line. This is a common culprit.
回答2:
You are not allowed to print or echo before header(). Might be you have white spaces, the problem is due to that.
回答3:
Check this link http://kb2.adobe.com/community/publishing/505/cpsid_50572.html
Remember this ,
- Any HTML output, including the DOCTYPE declaration or any HTML tag, including the head of the page
- Extra whitespace before the opening PHP tag of the page, or outside the PHP tags of an include file
- Using print() or echo before calling header() or session_start()
- Using virtual() to include files
- Using the byte-order mark (BOM) at the beginning of a page
来源:https://stackoverflow.com/questions/6743414/php-header-problem