PDA

View Full Version : Login tutorial - session_start() error message


lux
02-02-2007, 09:14 AM
hey, trying to use the login script from the video tuts.

it worked fine on my machine locally, and am now trying to use it on a linux server connecting to a postgresql db.

i get this error message:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /var/www/html/login.php:6) in /var/www/html/login.php on line 9

heres my login.php page


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login</title>
<?php
require_once("connections/conn.php"); // database connection

session_start();

$userId = $_POST['userId'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];
/////////////////////////////////////////////////////////////
if ($userId && $password){
$query = sprintf("SELECT * FROM tbl_user WHERE fullName ='$userId' and emailAddress ='$password'");
$result = pg_exec($query);
$rowAccount = pg_fetch_assoc($result);
}

if ($rowAccount) {

$_SESSION['id'] = $rowAccount['user_id'];

header("location:welcome.php");
exit;

} elseif ($submitted){

echo "Your username / password combinations is not recognised! Try again if you dare";

}
///////////////////////////////////////////////////////////////
?>
</head>

<body>
<form name="form1" id="form1" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<table width="180" border="1" align="center" cellpadding="1" cellspacing="0">
<tr>
<td>User ID</td>
<td><input name="userId" type="text" id="userId" /></td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="text" id="password" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="submitted" type="hidden" id="submitted" value="1" />
<input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>



can't see whats wrong? maybe whitespace or something, but i can't see it!

davidj
02-02-2007, 10:54 AM
Your php needs to be above the html not between the HEAD tags

lux
02-02-2007, 11:00 AM
cheers davidj

davidj
02-02-2007, 11:13 AM
also you have this...


echo "Your username / password combinations is not recognised!


for total control it would be better to assign this string to a variable then you can echo the variable anywhere you want in the page


$warning = "Your username / password combinations is not recognised!

<body ...

<?php echo $warning; ?>

.../body>

davidj
02-02-2007, 11:13 AM
also you have this...


echo "Your username / password combinations is not recognised!


for total control it would be better to assign this string to a variable then you can echo the variable anywhere you want in the page


$warning = "Your username / password combinations is not recognised!

<body ...

<?php echo $warning; ?>

.../body>

lux
02-02-2007, 11:16 AM
i was going to change that next! honest (hehe)