Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have set a session on the login script and on the next page i want to check if the session is set however it does not login. when a i try to access page 2 without logging in it takes me to page 1 which works fine however when i do log in it still takes me to login page. please help.
my login script looks like this. I am still working on the crsf method
PHP
<?php 
ini_set('display_errors', '0');
session_save_path('/tmp');
session_start();
$_SESSION['logged_in'];
//login page is like this

require_once 'connection.php';


if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
$username = trim($_POST['username']);

try{

  $Query = "SELECT * FROM users WHERE username = :username";
  $statement = $conn->prepare($Query);
  $statement->bindValue(':username', $username);
  $statement->execute();
  $user = $statement->fetch(PDO::FETCH_ASSOC);    
  $RowCount = $statement->rowCount();

} catch (PDOerrorInfo $e){

    die('QuerySCD Error '.$e->getMessage());

  }

  if( $RowCount == 0 ){
   // User doesn't exist
    $_SESSION['message'] = "error!";
    //header("location: error-login.php");
    $message = "Invalid login credentials";

  } else{ // User exists

      if( password_verify($_POST['password'], $user['password'])){

        $_SESSION['username']  = $user['username'];
        $_SESSION['active']    = $user['active'];
        $_SESSION['logged_in'] = $user['logged_in'];

        $_SESSION['logged_in'] = true;

        header("location: studentportal.php?onions=no&pickles=yes");

      } else {

          $_SESSION['message'] = "error!";
          //header("location: error-login.php");
          $message = "Invalid login credentials";

        }      
    }  
}

$conn = NULL;
?>
my second page look like this 
 
<?php
ini_set('display_errors', '0');
session_save_path('/tmp');

if ($_SESSION['logged_in']) {
	$_SESSION['logged_in'] = true;
	header('Location: studentportal.php');
	$message = 'welcome user';
} else {
	header('Location: index.php');
}

?>


What I have tried:

i have tried research and tutorials
Posted
Updated 31-Aug-20 2:51am
v2
Comments
gavin_daCEO 31-Aug-20 8:35am    
If anyone can show me where I am failing please so I can correct my mistake
Sandeep Mewara 31-Aug-20 8:43am    
Just check once for this in following page and see if this too is empty: $_SESSION['username']
gavin_daCEO 31-Aug-20 8:46am    
it is not there so i am supposed to add it,i also implemented crsf so i also need to add that on the session
Sandeep Mewara 31-Aug-20 8:47am    
think you might need this too at the start of the page2: session_start();
gavin_daCEO 31-Aug-20 8:48am    
thanks for showing me the light

1 solution

Currently, session would not be passed across.

believe you need to add session_start() in the second page too. If a session is already existing it will continue using it and will find if anything in it.
PHP
session_start(); // in top of second page
 
Share this answer
 
Comments
gavin_daCEO 31-Aug-20 9:06am    
i wanted the second page to use a session from the first page and not start a session on the second page. will it work the way i am doing it
Sandeep Mewara 31-Aug-20 9:32am    
If someone visits the second page without login, this session will help. Try out.
gavin_daCEO 31-Aug-20 12:44pm    
i will try it out thanks for the advice neh
gavin_daCEO 31-Aug-20 14:50pm    
i am only failing on the session for the username or is there anything else ? i am working it out but it still gives me problems
Sandeep Mewara 31-Aug-20 15:21pm    
https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php

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