So I am having a problem storing the username typed into my site and keeping myself logged into the site. I can log in (I see this because of the "You have been logged in" message. but the second I click the link to go to the members.php page it returns the echo "You are not logged in.". Why isn't the $username value being added to the SESSION?
Here are 4 of my codes. I have a fifth one aswell with all my MySQL information, which I dont wish to share, however, my username is confirmed via the MySQL since I receive the first login validation. YES, I followed a tutorial on YouTube.
index.php
<html>
<form action='login.php' method='POST'>
Username: <input type='username' name='username'><br>
Password: <input type='password' name='password'><br>
<input type='submit' name='button' value='Login'>
</form>
</html>
login.php
<?php
session_start();
$password = $_POST['password'];
$username = $_POST['username'];
include ("connect.php");
if ($username && $password)
{
// info is provided
$queryget = mysql_query("SELECT * FROM login WHERE username='$username' AND password ='$password' ");
$numrows = mysql_num_rows($queryget);
if ($numrows != 0)
{
$_SESSION['username'] = $username;
echo "You have been logged in. <a href='members.php'>Go to the members page</a>";
}
else echo "This password is wrong";
include ("index.php");
}
else
{
echo "Wrong password, try again!";
include ("index.php");
}
?>
members.php
<?php
session_start();
$username = $_SESSION['username'];
if ($username)
{
echo "Welcome $username | <a href='logout.php>Logout</a>'";
}
else
{
echo "You are not logged in.";
include ("index.php");
}
?>
logout.php
<?php
session_start();
$username = $_SESSION['username'];
session_destroy();
echo "You have been logged out.";
?>