I am learning how to build a login-system. It's not meant to be the most secure but it should work without getting hacked too easy by using sessions.
After logging in successfully you should be redirected to the index-page and some entries in the navigation should change (e.g. Login should be Logout).
My navigation:
<nav>
<?php
if(!isset($_SESSION["username"])) {echo "<a class='left"; if($section == "login") {echo " active";} echo"' href='index.php?p=login'>Login</a>";}
else {echo "<a class='left"; if($section == "logout") {echo " active";} echo"' href='index.php?p=logout'>Logout</a>";}
?>
</nav>
My login form:
<form action="index.php?p=login_check" method="post">
Username: <input type="text" size="24" maxlength="50" name="username"><br />
Passwort: <input type="password" size="24" maxlength="50" name="password"><br />
<input type="submit" value="Abschicken">
</form>
My login validation:
<?php
$verbindung = mysql_connect("localhost", "root" , "")
or die("Verbindung zur Datenbank konnte nicht hergestellt werden");
mysql_select_db("website") or die ("Datenbank konnte nicht ausgewählt werden");
$username = $_POST["username"];
$password = $_POST["password"];
$abfrage = "SELECT username, password FROM logins WHERE username LIKE '$username' LIMIT 1";
$ergebnis = mysql_query($abfrage);
$row = mysql_fetch_object($ergebnis);
if(@$row->password == $password)
{
session_start();
$_SESSION["username"] = $username;
echo "<p>Login erfolgreich.</p>";
}
else
{
echo "<p>Benutzername oder Passwort waren falsch. <a href=\"index.php?p=login\">Login</a></p>";
}
?>
My logout:
<?php
session_destroy();
$hostname = $_SERVER['HTTP_HOST'];
$path = dirname($_SERVER['PHP_SELF']);
?>
Now when I am back on the starting-page I still see "Login" instead of "Logout". Do I have to include session_start() on my index-page or is there another way of checking if I am logged in?