I am trying to create a simple login script in php. This is a learning exercise so I am trying to keep it as simple as possible. After playing around with it for an hour or two, I got it working - I was able to log in and I was greeted - but then it stopped. Now, every time I try to login, I don't get past my login page.
My admin page did not originally include a logout function, I added that later and tried logging out several times thinking that was the issue. However, I also cannot access my admin page directly so that suggest to me that I am not logged in.
login_page.php
<p> Please login to access admin: </p
<form action="verify.php" method="post">
User Name:<br>
<input type="text" name="username"><br><br>
Password:<br>
<input type="password" name="password"><br><br>
<input type="submit" name="submit" value="Login">
</form>
verify.php
<?php
$host = 'localhost';
$username = 'user';
$pswd = 'pass';
$con = mysql_connect($host, $username, $pswd);
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$usr = $_POST['username'];
$pas = $_POST['password'];
$sql = mysql_query("SELECT * FROM users_table
WHERE username='$usr' AND
password='$pas'
LIMIT 1");
if(mysql_num_rows($sql) == 1){
$row = mysql_fetch_array($sql);
session_start();
$_SESSION['username'] = $row['username'];
$_SESSION['fname'] = $row['first_name'];
$_SESSION['lname'] = $row['last_name'];
$_SESSION['logged'] = TRUE;
header("Location: viewRecords.php");
}else{
header("Location: index.php");
exit;
}
mysql_close($con);
?>
I added a link on my admin page which goes to logout.php - here I just kept adding commands hoping something would end the session and let me log in again.
<?php
session_start();
$_SESSION = array();
unset($_SESSION["logged"]);
session_destroy();
header("Location: index.php");
?>
Here's the top of my admin page:
<?php
session_start();
if(!$_SESSION['logged']){
header("Location: login_page.php");
exit;
}
echo 'Welcome, '.$_SESSION['username'];
?>
I am wondering if there is an error in my code (maybe something I introduced while editing) or if the issue has to do with sessions or any other suggestions. Any feedback would be appreciated. Thanks.