Problem creating a new session
Posted: Tue Mar 02, 2004 8:15 pm
We are new and just learning to use php. We are trying to create a Login page that will create a session once a user has logged in.
Our error is:
Warning: session_start(); cannot send session cookie - headers already sent by (output started at /home/nwmissouri/public_html/USer_Validate.php:8)
Our code is:
EDITED BY BECH100: ADDED PHP TAGS
Our error is:
Warning: session_start(); cannot send session cookie - headers already sent by (output started at /home/nwmissouri/public_html/USer_Validate.php:8)
Our code is:
Code: Select all
<html>
<head>
<title>Access Test</title>
</head>
<script language = "JavaScript">
function emptyUserOrPass()
{
document.URL = "http://www.nwmissouri.kkytbs.net/Error_Login.php";
}
function invalidUserOrPass()
{
document.URL = "http://www.nwmissouri.kkytbs.net/Invalid_Login.php";
}
function adminCheck()
{
document.URL = "http://www.nwmissouri.kkytbs.net/Admin_Welcome.php";
}
function memberCheck()
{
document.URL = "http://www.nwmissouri.kkytbs.net/Member_Welcome.php";
}
</script>
<body>
<?php
session_start();
if(isset($HTTP_POST_VARS['username']) && isset($HTTP_POST_VARS['password']))
{
$username = $HTTP_POST_VARS['username'];
$password = $HTTP_POST_VARS['password'];
if(!$username || !$password)
{
session_destroy();
echo("<script language = 'JavaScript'>");
echo("emptyUserOrPass();");
echo("</script>");
}
//trims extra spaces off username
$username = trim($username);
$password = trim($password);
$db = mysql_connect('localhost', 'nwmissouri', 'info1') or die ("Could not connect: " . mysql_error());
//Checks to see if connection failed
if(!$db)
{
echo 'Error: Could not connect to database. Please try again later.';
}
mysql_select_db('nwmissouri');
$query = 'select * from LOGIN '
."where Username = '$username' "
." and Password = '$password'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if(mysql_num_rows($result) > 0)
{
session_register('username');
//if they are in the database register the user id
$HTTP_SESSION_VARS['valid_user'] = $username;
if($row['User_Level'] == 1)
{
echo ("<script language = 'JavaScript'>");
echo ("adminCheck();");
echo ("</script>");
}
echo ("<script language = 'JavaScript'>");
echo ("memberCheck();");
echo ("</script>");
}
else
{
session_destroy();
echo ("<script language = 'JavaScript'>");
echo ("invalidUserOrPass();");
echo ("</script>");
}
}//end if
?>
</body>
</html>