I'm only just starting to learn PHP and have done ok so far but I have confused myself with sessions.
If someone could have a look at my code and let me know where i have went wrong and how i can fix it that would be fantastic!
The username/password is hard coded to admin 123 as instructed.
Thanks
HTML
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>if statements</title>
</head>
PHP
<body>
Login
<form action="username.php" method="post">
<p>
<label for="username">Enter your username</label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="password">Enter your password</label>
<input type="text" name="password" id="password" />
</p>
<p>
<label for="GO"></label>
<input type="submit" name="GO" id="GO" value="Submit" />
</p>
</form>
</body>
</html>
<html>
<body>
<?php
session_start();
If($username==”admin” && $password==”123”)
$username = isset($_POST['username']) ;
$password = isset($_POST['password']) ;
if ($name != 'admin' || $pass != '123')
{
elseif ($username === '')
die("ERROR: Please enter the username!");
elseif ($password === '')
die("ERROR: Please enter the password!");
elseif ($name == "test" && $pass == "test") {
// authentication successful, save username in session:
$_SESSION['user'] = 'admin';
// redirect to index.php to welcome the logged in user:
header('Location: index.html');
exit;
}
else {
die("ERROR: Incorrect username or password!");
}
}
?>
</body>
</html>
EDIT
<html>
<body>
<?php
session_start();
If($username==”admin” && $password=="123”)
$username = isset($_POST['username']) ;
$password = isset($_POST['password']) ;
if ($username == "admin" && $password == "123") {
// authentication successful, save username in session:
$_SESSION['username'] = 'admin';
$_SESSION['password'] = '123';
// redirect to welcome.html to welcome the logged in user:
header('Location: welcome.html');
exit;
}
else {
die("ERROR: Incorrect username or password!");
}
// no submitted data, display form:
?>
</body>
</html>
EDIT AGAIN:
<html>
<body>
<?php
session_start();
if(isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if($username === 'admin' && $password === '123')
$_SESSION['username'] = 'admin';
$_SESSION['password'] = '123';
session_write_close();{
// redirect to welcome.html to welcome the logged in user:
header('Location: welcome.html');
exit;
}
}
else {
die("ERROR: Incorrect username or password!");
}
// no submitted data, display form:
?>
</body>
</html>
It kind of works now, but even with wrong username/password it goes to welcome.html