So I'm trying to show an users username after he or she has logged in but I just can't get it to work... Here are the codes I am using:
First off, an user has to log in.
<div>
<form method="POST" action="login.php">
<h2>LOGIN</h2>
<input type="text" placeholder="Username" name="username" />
<input type="password" placeholder="Password" name="password" />
<br />
<input class="btn btn-large btn-primary" type="submit" name="login" value="Log In" />
</form>
</div>
After using the form, this sript will load, this is 'login.php':
<?php
session_start();
if(!$_SERVER['REQUEST_METHOD'] == 'POST') {
echo "Go away.<br /><br />";
exit();
}
if(mysql_connect('localhost', 'root', '')) {
if(mysql_select_db('users')) {
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$zoekresultaat = mysql_query($query);
if($zoekresultaat = mysql_affected_rows() > 0) {
$record = mysql_fetch_assoc($zoekresultaat);
$_SESSION['login'] = true;
$_SESSION['username'] = $record['username'];
header('location: dashboard.php');
} else {
header('location: loginerror.php');
}
exit();
} exit(); {
echo "Can't find database.";
}
} else {
echo "Can't connect to database.";
}
?>
When this is done, the user gets redirected to 'dashboard.php' which is the following:
<?php include "includes/menu.php";?>
<div id='content'>
<h1><strong>Welkom <?php echo $_SESSION['username']; ?></strong></h1>
<p></p>
</div>
<?php include "includes/foot.php";?>
Problem is when I try to login I get the following error:
Notice: Undefined variable: _SESSION in D:\xampp\htdocs\NGP\dashboard.php on line 5
I'm really lost and hoping to get some answers here! Thanks in advance!