
Originally Posted by
StarLion
I'd advise against using 'AND' as opposed to '&&', for starters, but that's irrelevant to the problem.
Secondly, why seperate the two?
PHP Code:
if (isset($_GET['action']) && $_GET['action'] == 'logout') {
session_destroy();
echo 'You are successfully logged out.'
}
May I ask the reason why you'd advise against 'AND'?
I need to separate the two because I am displaying the message on another page (login.php) which includes this login script (login-script.php). Anyway, perhaps it will be clearer with my actual setup:
login.php
PHP Code:
<?php
session_start();
require 'login-script.php'
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Login</title>
</head>
<body>
<?php if ($missing_info) echo 'Please enter a username and password.' ?>
<?php if ($invalid_login) echo 'Invalid login details.' ?>
<?php if ($logged_out) echo 'You are successfully logged out.' ?>
<form action="" method="post">
<p>Username<br /><input type="text" name="username" value="<?php if (isset($_POST['username'])) echo $_POST['username'] ?>" /></p>
<p>Password<br /><input type="password" name="password" /></p>
<p><input type="submit" name="login" value="Log In" /></p>
</form>
</body>
</html>
login-script.php
PHP Code:
<?php
$logged_out = FALSE;
// Check if Logout button is clicked.
if (isset($_GET['action']) AND $_GET['action'] == 'logout') {
$logged_out = TRUE;
session_destroy();
}
// Check if the user is already logged in.
if (isset($_SESSION['username'])) {
header('Location: index.php');
} else {
$missing_info = FALSE;
$invalid_login = FALSE;
// Check if submit button is clicked.
if (isset($_POST['login'])) {
// Check if username and/or password fields are empty.
if (empty($_POST['username']) OR empty($_POST['password'])) {
$missing_info = TRUE;
} else {
// Check if the username and password are correct.
if (($_POST['username'] != 'nayen') OR $_POST['password'] != 'xyz123') {
$invalid_login = TRUE;
} else {
$_SESSION['username'] = $_POST['username'];
header('Location: index.php');
}
}
}
}
?>
index.php
PHP Code:
<?php
// Content...
<a href="login.php?action=logout">Log Out</a>
?>
Apart from displaying that "logged out" message, I would be happy if you have any suggestions about my logic or code or any points that I might have missed. Thank you both.
Bookmarks