0

So here is the code for eppkm.php (the page that I need to be protected) The session is settled simply by submit a form that have token value.

<?php session_start();
include_once"pengaturan/crud.php";

if (isset($_POST['token'])) 
{
        $token = $_POST['token'];
        $_SESSION['user'] = true;
        $konek->login($token);
}
else
{
        $_SESSION['user'] = true;
        $konek->login($token);
}

?>

And here is the pengaturan/crud.php file, login function:

public function login($token)
    {

      if (isset($token)) 
      {  
        $query = "SELECT * FROM master_users WHERE remember_token = '". $token ."'" ;
        $hasil = pg_query(Parent::get_dbcon(),$query);

        if (pg_num_rows($hasil) != 1)
        {
          echo "nothing";
          //header("Location: http://192.168.0.190/eppkm_ex/public/master/login");       
        }

      }

      else{
        if(!isset($_SESSION['user'])){
        session_destroy();
        header("Location: http://192.168.0.190/eppkm_ex/public/master/login");
          }
      }

    }

When I tried to echo the session, it says Undefined index: user and also tried var_dump the session, the result comes to null which mean the session isn't settled.

Can you help me fix the session? And point me where am I doing wrong :(

2 Answers2

0

I've encounter similar issue in the past 7-8 years ago from what I remember the issue is not related to $_SESSION but the value Boolean

I would suggest converting em to string/integer or alternatively per explanation below

here's explaination:

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

Reference here

Fernan Vecina
  • 144
  • 1
  • 8
  • Yes, i change `$_SESSION['user'] = $token`, but nothing happened. The problem is same, session not settled. – Marionette Readram Jul 10 '17 at 03:16
  • are you trying to redirect page after setting session? if Yes can you try adding session_write_close(); above redirect code like example: session_write_close(); header("Location: /yourpage.php"); – Fernan Vecina Jul 10 '17 at 03:29
0

Your code is working fine !

Check

Please set error_reporting(E_ALL); and ini_set('display_errors', 1); at top of the file or CHeck.

Jaydeep Mor
  • 1,690
  • 3
  • 21
  • 39