I have moved onto PHP and was designing a register and login system that would allow the user whos username and password is stored in the database to access the Members page.
I have set up the register and login page with validation that was needed but i have one small problem.
When i log in using the username and password i am redirected to the members page.
I want the script of my login and member page to use session variables that would keep the user logged in unless they press logout on the member page. Can you please help me.
This is the code for the login and member page.
Login page:
<!DOCTYPE html>
<html lang="en">
<head>
<!--title of the web page-->
<title>Login - Mas Agency</title>
<!--web page meta tags-->
<meta charset="utf-8" />
<!--specific web page meta tags-->
<!--CSS Links-->
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<!--Register users-->
<form name="register" method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<strong> Register Account</strong> <br /><br />
User name:
<input type="text" name="username"><br /><br />
Password:
<input type="text" name="password"><br /><br />
<br /><br />
<input type = "submit" value = "register" / >
</form>
<?php
session_start();
//validate registration form
if ( (empty($_POST['username'])) OR (empty($_POST['password'])) )
{
//if the values are empty -
echo "<br /> Please fill in all the above inputs";
}else{
echo "<br> Process Validation";
//if the inputs has values
//store the data into local variables
$Username = $_POST['username'];
$Password = $_POST['password'];
$Check=true;
//Validate password - has to be more than 5 char
if (strlen($Username) <5 )
{
echo "<br> Username or Password is incorrect";
$Check=false;
}
//if first name has any special characters
if (is_numeric($Username))
{
echo "<br> error - username has number...";
$Check=false;
}
//if username is email format
// Variable to check
// Validate email
if(filter_var($_POST['username'],FILTER_VALIDATE_EMAIL)){
}
else{
echo '<br>Invalid email format';
$Check=false;
}
//all the password validations
if (strlen($Password) <5 )
{
echo "<br> error - password has less than 5 characters";
$Check=false;
}
//if all the validation are true then prepare
//to store to online database
if ($Check == true)
{
echo "<br> Checking.....";
require_once('db.php'); //connect the database
$SQL = "SELECT * FROM user WHERE username='$Username' and password='$Password'";
$result = $conn->query($SQL); //execute the SQL query and store the data in $result as an array
if ($result->num_rows>0)
{
$_SESSION['username'] = $username;
//user information available in database
echo "Login successful";
echo "<br><br> re-directing to members page";
header ( 'refresh:5; url=member.php?id='.$Username ); //re-directing to member page
}
else{
//users info not found
echo "<br> Username or Password incorrect";
if(isset($_GET['logout'])){
session_unregister('username');
}
}
}
}
?>
</body>
</html>
Member page:
<?php
session_start();
if ((empty($_GET['id'])) )
{
//if the values passed is empty -
if(!isset($_SESSION['username']))
{
header("location:login.php");
}
//redirect to login page after 5 seconds
}
else
{
echo "<br> -------- Hello --------<br><br>";
//local variables
$Username = $_GET['id'];
require_once('db.php'); //connect the database
$SQL = "SELECT * FROM user WHERE username='$Username'";
$result = $conn->query($SQL); //execute the SQL query and store the data in $result as an array
if ($result->num_rows>0)
{
//loop through the $result array and display the data
while($row = $result->fetch_assoc())
{
echo "<br>".$row['fname']." ".$row['sname'];
echo '<br><a href="login.php?action=logout">Logout</a>';
}
}
else
{
echo "can not open members page",
session_destroy();
}
}
?>