This question already has an answer here:
- The 3 different equals 5 answers
i have got a problem with my login script. i have a small div, where i'd like to echo a form so users can log in. after submitting the form, the values will be sent to another page which create a session. After that, when the user is back on the index, where the login form was, there should be no login form anymore but the info about the user. But the problem: There's nothing echoed in the div, even if there is no session created yet.
<div id="updates">
<?php
if(isset($_SESSION['username']))
{
$query = mysql_query("SELECT * FROM users WHERE username='$dbusername'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
$dbvipshouts = $row['vip_shouts'];
echo"
<table>
<tr>
<td><font color='white'>Nickname:</font></td><td>$dbusername</td>
</tr>
<tr>
<td><font color='white'>Vip shouts over:</font></td><td>$dbvipshouts</td>
</tr>
</table>";
}
}
else
{
echo "
<form name='login' action='login.php' method='POST'>
<table>
<tr>
<td><font color='white'>Nickname:</font></td><td><input class='inputname' type='text' name='nickname'></td>
</tr>
<tr>
<td><font color='white'>Wachtwoord:</font></td><td><input class='inputname' type='password' name='password'></td>
</tr>
<tr>
<td colspan='2'><center><input class='inputname' type='submit' name='submit' value='Log in!'></center></td>
</tr>
</table>
</form>";
}
}
?>
</div>
and the code of the file that should create a session:
<?php
$username = $_POST["nickname"];
$password = $_POST["password"];
if ($username&&$password)
{
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
$dbvipshouts = $row['vip_shouts'];
}
// controleren of ze bij elkaar passen
if ($username==$dbusername&&$password==$dbpassword)
{
$_SESSION['username']==$dbusername;
$_SESSION['vip_shouts']==$dbvipshouts;
header('Location:index.php');
}
else
echo "Gebruikersnaam of wachtwoord is onjuist! Klik <a href='index.php'>hier</a> om terug te gaan.";
}
else
die("Deze gebruiker bestaat niet! Klik <a href='index.php'>hier</a> om terug te gaan.");
}
else
die ("Voer een gebruikersnaam en wachtwoord in! Klik <a href='index.php'>hier</a> om terug te gaan.");
?>
</div>