I'm getting the following warning;

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\ep\login.php:1) in C:\xampp\htdocs\ep\login.php on line 2

If i comment session_start(), everthing works fine. Where is the actual problem?

Here is my code;

<?
session_start();

include "config.php";
global $c;
require('functions.php');

if($_POST)
{
	$username=uc($_POST['username']);
	$password=uc($_POST['password']);
	
	/*if($_POST['code']!=$_SESSION['string'])
	{
		header("Location: login.php?error=1");
	}
	else
	{*/
		$query = mysql_query("SELECT username,password FROM tb_users WHERE username = '$username'") or die(mysql_error());
		if(mysql_num_rows($query) == 0)
		{
		echo "asdsadasdsad";
		header("Location: index.php?error=2");
		
		}
		else
		{
			$data = mysql_fetch_array($query);
			if($data['password'] != $password) 
			{
				header("Location: login.php?error=2");
			}
			else
			{
				$query = mysql_query("SELECT username,password FROM tb_users WHERE username = '$username'") or die(mysql_error());
				$row = mysql_fetch_array($query);

				$nick=$row['username'];
				$pass=$row['password'];

				
				setcookie("usNick",$nick,time()+7776000);
				setcookie("usPass",$pass,time()+7776000);


				$lastlogdate=time();
				$lastip = getRealIP();

				$querybt = "UPDATE tb_users SET lastlogdate='$lastlogdate', lastiplog='$lastip' WHERE username='$nick'";
				mysql_query($querybt) or die(mysql_error());


				$checkuser = mysql_query("SELECT username FROM tb_acchold WHERE username='$nick'");
				$username_exist = mysql_num_rows($checkuser);
				
				header("Location: index.php");
				
?>

<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=index.php">

<?
			
			}
		
		}
	}
	
	
//}

?>
<? include "header.php"; ?>
<head>
<title>Login | ePaisa.PK</title>
<script type='text/javascript'>

function formValidator(){
	
	var user = document.getElementById('username');
	var pass = document.getElementById('password');
	var code = document.getElementById('code');
	
	
	if(userEmpty(user, "You must enter a username")){
		if(passEmpty(pass, "You must enter a password")){
			if(codeEmpty(code, "Enter the correct security code"))
						{
			
							return true;
						}
					}
				}
	return false;
	
}

function userEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function passEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function codeEmpty(elem, helperMsg){
	if(elem.value.length == 0 || elem.value.length < 6){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}


</script>
</head>


<table style="width: 100%; height: 100%;" cellpadding="0" cellspacing="0" class="style1">
									<tr>
										<td valign="top" style="width: 415px">
										<div style="padding-left:15px; padding-top:10px"class="style31">

											<strong>Don't have an account?</strong></div>
										&nbsp;</td>
										<td valign="top">
										<table style="width: 100%; height: 75%;" cellpadding="0" cellspacing="0" class="style29s">
											<tr>
											<div style="padding-right:50px; padding-top:10px">

												<td class="style30s" valign="top">
												<div style="padding-left:10px; padding-top:10px"class="style20">
												<strong>Login</strong>
												<div style="padding-top:15px; padding-right:10px;" class="style35">
													<hr class="style34"></div>


<?php
$error = $_GET["error"];
switch($error) {
//case(loged):
default:


if($_GET['error'] == 1)
{
print "<h4 style=\"color:red;\"><b>Error</b> - Wrong Security Code</h4><br /><br/>";
}
if($_GET['error'] == 2)
{
print "<h4 style=\"color:red;\"><b>Error</b> - Invalid Username/Password</h4><br><br>";
exit();
}
if($_GET['error'] == 3)
{
print "<h4 style=\"color:red;\"><b>Error</b> - You can not login to your account untill you post your payment proof in our forum.</h4><br /><br />";
}

?>
												<div class="style32" style="padding-top:30px">
													<span class="style33">
													

													<form method="post" action="login.php"  onsubmit="return formValidator()">
													Username:
													<br>
													<div style="padding-top:5px">
														<input name="username" type="text" size="25" id="username">
														<div style="padding-top:15px">
															Password:
															<div style="padding-top:5px">
																<input name="password" type="password" size="25" id="password">
																<div style="padding-top: 15px">
																	Security 
																	Code: 153214<div style="padding-top:5px; padding-left: 100px">
																		<input name="code" type="text" size="8" id="code" maxlength="6"></div>
																</div>
																<div style="padding-top:25px; padding-left:120px">
																<input type="submit" value="Login"></div>
															</div>
														</div>
													</div>
													</form>
													<div style="padding-right:15px; padding-top:10px">
														<hr></div>
													<div style="padding-top:5px; padding-left: 60px">
														<a href="recover.php" class="red">Forgot 
														Username/Password?</a>
														</div>
													<br>
													</span><br>
												</div>
												</div>
												</td>
												
												</div>
													
											</tr>
										</table>
										</td>
										<td style="width: 10px"></td>
										<td valign="top">
										&nbsp;</td>
									</tr>
								</table>
								
<? } ?>

<? include "footer.php"; ?>

The main reason this happens is because some text was already sent.
(Either with an echo/print statement or anything not enclosed in <?php ?> tags.)

Your code shows the PHP tag starting on the second line, is the file the same way?

No, the PHP starts on the first line in my file.
Moreover, when i remove session_start(); , the page doesn't give any warning.

Yeah, the error is caused by some kind of output being sent before session_start().

What text editor are you using, and what format are you doing it in? I had the same error when I was using regular ANSI UTF-8, but it was fixed by choosing the "without BOM" option. One suggestion someone else also said was to try opening the file in notepad(the really simple one) to see if your editor put anything in before your text.

I'm using Programmers Notepad.
I tried in notepad as well but the problem is still the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.