PDA

View Full Version : php login script part 2 issues


m1a2x3x7
05-06-2007, 05:31 AM
Not sure why i keep getting this warning. here is my code

Login Code


<?php
require_once("connections/connect.php");

session_start();

//catch field data
$userid = $_POST['userid'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];

if ($userid && $password) {

$query = sprintf("SELECT * FROM users where user_name='$userid' and user_password = '$password'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
}

if ($rowAccount) {

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

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

}elseif($submitted){

echo "You dont exist in the system so your not getting in!";
}
?>
<!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>Untitled Document</title>
</head>

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


Welcome Code


<?php
require_once("connections/connect.php");

session_start();

if(! isset($_SESSION['id'])){

header("location: login.php");
exit;
}

$id = $_SESSION['id'];

$query = sprintf("SELECT * FROM users where user_id=$id'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);

?>
<!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>Untitled Document</title>
</head>

<body>
Welcome to the application <?php echo $rowAccount['user_name'];?>
</body>
</html>


Connect Code


<?php
///database varables///
$database = "dbname";
$username = "username";
$password = "password";
///////////////////////
$link = @mysql_connect('localhost', $username, $password);
$db = mysql_select_db($database, $link);
?>

These are the warning messages


Warning: session_start() [function.session-start (http://www.dreamweaverclub.com/forum/function.session-start)]: Cannot send session cookie - headers already sent by (output started at C:\wamp\www\lesson2\connections\connect.php:11) in C:\wamp\www\lesson2\login.php on line 4
]
Warning: session_start() [function.session-start (http://www.dreamweaverclub.com/forum/function.session-start)]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\lesson2\connections\connect.php:11) in C:\wamp\www\lesson2\login.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\lesson2\connections\connect.php:11) in C:\wamp\www\lesson2\login.php on line 22


now I did part one of the login lesson and everything worked fine so it just has to do with the sessions and header

any and all help would be great

thanks
Chris

m1a2x3x7
05-06-2007, 06:02 AM
never mind i just added ob_start() at the top of the login and welcome page and fixed everything

davidj
05-06-2007, 10:57 AM
echo "You dont exist in the system so your not getting in!";

where you have the above line

its better to assign warning messages to variables

this was probably the cause of the error.

try this...


$warning = "You dont exist in the system so your not getting in!";


then you can echo $warning where you want on the page

m1a2x3x7
05-13-2007, 06:27 AM
thanks for the help mate.