I have seen this in a few posts, but i still cant seem to find the problem. Im basically trying to log in to a chat page and don't know what I'm doing wrong.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
//Start a session
session_start();
//Setup and use a tracking variable to count login attempts
if(isset($_SESSION['logoncount']))
{
    $_SESSION['logoncount']++;  //Increment for every logon attempt
}
else
{
    $_SESSION['logoncount']=0;  //Nr of failed login attempts initially zero
}
if(isset($_POST['Submit']))   //Check if the login form has been submitted
{   

    //Get the values from the new user form
    $pw = md5($_POST['Password']); //Note use of MD5 hash function
    $username = $_POST['Username'];
    //Set up and execute the INSERT query
    $query = "SELECT * FROM user WHERE (Username='$username' AND Password='$pw')";
    $result=mysql_query($query);  //Get the query result
    $num=mysql_numrows($result);  //Get number of records returned 
    if ($num)  //Logon is successful - redirect to restricted home page
    {
        $_SESSION['userid']=$username; //Save the username in a session variable
        if(isset($_SESSION['views']))
            {$_SESSION['views'] = $_SESSION['views']+ 1;}
            include ('open.php');
        else
            header('Location: mainpage.php'); //display the restricted page
            {$_SESSION['views'] = 1;} //set the session variable views
            unset($_SESSION['logoncount']);//Unset the logon tracking count

            exit();
    }
    else    //Logon has failed - reload the logon page
    {
        unset($_SESSION['views']);
    }
}
?>

<html>
<head>
<title>Login Form</title>
</head>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<link href="chatstyle.css" rel="stylesheet" type="text/css" />

<body>

<h1>Login Form</h1>
<p><p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  UserName: 
  <input   name="Username" type="text" size="30" maxlength="30" /><br />
    Password:
    <input name="Password" type="password" size="30" maxlength="30" /><br /><p>
    <input name="Submit" type="submit" value="Login" /><br />
</form>
<?php
//This code displays the logon count in the event of a failed login attempt

if (!$_SESSION['logoncount'])
    {
    echo "<hr>";
    echo "<h3>Please enter your username and password</h3>";
    }
else
    {
    echo "<hr>";
    echo "<h3>Invalid logon attempt - please try again</h3>";
    echo "<p>Failed logon attempts=",$_SESSION['logoncount'];
    }
?>

</head>
</div>
</body>
</html>

You must call session start BEFORE outputting the doctype (before ANY output).

commented: Thank you, that was so obvious, +0
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.