Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to check if a session has been opened in the login page by putting an if statement on the welcome page, it does not work hence i can access the welcome page by just typing the url
$_SESSION['logged_in'] = true;
//login page is like this 

//welcome page is like this
<?php
$_SESSION['logged_in'] = true;
if(empty($_SESSION['logged_in']) || $_SESSION['logged_in'] == ''){
header("Location: index.php");
die();
}
?>

What I have tried:

research and youtube tutorials
Posted
Updated 26-Aug-20 4:23am
Comments
[no name] 26-Aug-20 6:15am    
Think about what you are doing here $_SESSION['logged_in'] = true;...
gavin_daCEO 26-Aug-20 7:10am    
I am thinking that I am setting the session to true for when the user logs in. can you assist me f you have any know-how, please
ZurdoDev 26-Aug-20 9:22am    
You are setting it and then testing in an if statement. You need to set it after the user has logged in.
gavin_daCEO 26-Aug-20 12:49pm    
ok thanks a lot

1 solution

Given 0x01AA & ZurdoDev already shared the details, have a look at this to try and learn: PHP - Login Example - Tutorialspoint[^]

Login sample:
PHP
<?php
    $msg = '';
    
    if (isset($_POST['login']) && !empty($_POST['username']) 
       && !empty($_POST['password'])) {
		
       if ($_POST['username'] == 'tutorialspoint' && 
          $_POST['password'] == '1234') {
          $_SESSION['valid'] = true;
          $_SESSION['timeout'] = time();
          $_SESSION['username'] = 'tutorialspoint';
          
          echo 'You have entered valid use name and password';
       }else {
          $msg = 'Wrong username or password';
       }
    }
 ?>

Logout:
PHP
<?php
   session_start();
   unset($_SESSION["username"]);
   unset($_SESSION["password"]);
   
   echo 'You have cleaned session';
   header('Refresh: 2; URL = login.php');
?>

Would suggest, pick a book or any online tutorial (like above reference) to go through basic chapters, that would really help.
 
Share this answer
 
Comments
gavin_daCEO 26-Aug-20 20:28pm    
thank you i will do that
Sandeep Mewara 27-Aug-20 1:37am    
Welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900