I’ve got a problem with my login system/logout system.
So the problem is that I can not login. When I enter the correct username and password I get redirected to index.php when that is actually only going to happend after I have logged out. I think the error is in login_success.php with the if (!isset($_SESSION['mittbrukarnamn'])) line, (mittbrukarnamn = myusername), that will make the problem above, but when I remove the ! so the isset is not false anymore, I can login but then the logout.php does not “work”.
And I also get an error in line 19 that starts with if ($_POST['mittbrukarnamn'] && $_POST['mittpassord']) when I enter the wrong username or password in IE 9, or does not enter any username or password. But in Google Chrome I do not get that error. Can someone tell me if this is a safe system? I am going to add a code line later that makes the password convert to MD5 and an Admin user page.
Here is the check.php
<title>Login side</title>
<?php
session_start();
// 'vert' er det same som 'host' på engelsk (ikkje så viktig).
$vert = "localhost";
$brukarnamn = "root";
$passord = "";
$db_namn = "login";
$tbl_namn = "members";
// Tilkobling til MySQL databasen.
mysql_connect("$vert", "$brukarnamn", "$passord") or die ("Kan dessverre ikkje koble til databasen.");
mysql_select_db("$db_namn") or die ("Kan ikkje finna den ynkjande tabellen 'namn'.");
?>
<?php
if ($_POST['mittbrukarnamn'] && $_POST['mittpassord']) {
$mittbrukarnamn = $_POST['mittbrukarnamn'];
$mittpassord = $_POST['mittpassord'];
$sql = "SELECT * FROM $tbl_namn WHERE `brukarnamn` = '$mittbrukarnamn' AND `passord` = '$mittpassord'";
// '$res' forkorting for 'resultat'.
$res = mysql_query($sql);
// '$tell' er skreve på norsk, og er det same som 'count' på engelsk (f.eks. og telja til 10). Slek ein ikkje trur da er 'tell' på engelsk; (ikkje så viktig).
$tell = mysql_num_rows($res);
if ($tell == 1) {
$_SESSION['mittbrukarnamn'];
$_SESSION['mittpassord'];
header ("location: login_success.php");
} else {
echo "Brukarnamnet eller passordet er feil.";
header ("refresh: 2; index.php");
}
} else {
echo "Vennligst tast inn eit brukarnamn og eit passord.";
header ("refresh: 2; index.php");
}
?>
And login_success.php
<title>Medlem side</title>
<?php
session_start();
if (!isset($_SESSION['mittbrukarnamn'])) {
header("location: index.php");
}
?>
<html>
<body>
Du er innlogga.<br />
<a href="logout.php"> Logg ut</a>
</body>
</html>
I hope I have made clear what the problem is. Thank you for any answers 🙂
Some of this text is on norwegian.
(mittbrukarnamn = myusername)
(mittpassord = mypassword)
Its addition to Norton’s answer:
after placing the session_start() at the very beginning of the file,
replace
$_SESSION['mittbrukarnamn'];with$_SESSION['mittbrukarnamn']='';to set the session variable.What you are doing now does not set the session variable , its only getting the
$_SESSION['mittbrukarnamn']