phphtmldatabasepostgresqlsession

PHP Sessions Dies on next page


I have a login page which sends a variable to another page verifying if login is true (1) or false (0). I can echo $_SESSION['logon'] on page1 but just as I go to page2, my session seems to be dead and gives me * Notice: Undefined index: logon in * I have already fiddled with php.ini but to no avail. Register_globals = off by the way. I'm a rookie so I might have passed something wrong, didn't change the right parameter in php.ini, don't know.

Login Page:

<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
 <head>
 <title>Alpha</title>
 <link rel="stylesheet" href="CSS.css">  
 <script type="text/javascript" src="js/jquery-1.3.1.min.js"></script>
 <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
 <script type="text/javascript" src="js/jquery.history.js"></script>  
</head>
<body>
<form id="logbox" method="post" action="verylog.php">
User     <input id="user" name="user" type="text" required/>
Password <input id="pass" name="pass" type="PASSWORD" required/>
<input id="send" type="submit"/>
</form>     
<?php
if(isset($_POST['user'])){
    header("Location: verylog.php");
}
?>
</body>
</html>

verylog.PHP "page1"

 <?php
  session_start();
  $user=$_POST['user'];
  $pass=$_POST['pass'];
  $_SESSION['logon']=0;
  $cost   = ['cost' => 10,];
  $hasheduser=password_hash($user, PASSWORD_BCRYPT,$cost);
  $hashedpass=password_hash($pass, PASSWORD_BCRYPT,$cost);
  $storeuserhash=file_get_contents('sunburn/userburn.txt');
  $storepasshash=file_get_contents('sunburn/passburn.txt');
  if(password_verify($user, $storeuserhash) && password_verify($pass, $storepasshash)){
    session_regenerate_id(true);
    $_SESSION['logon']=1;
    header ("Location: ../sunburn/selector.php");   
    die();
    //echo $_SESSION['logon'];
  } else{
   header ("Location: ../burnlogin.php");
   die();
  }
?>

selector.php "Page2"

<?php
      session_start();
      $logon=$_SESSION['logon'];
    if($logon==0){
        //header("Location: index.php");
        exit();
        echo $_SESSION['logon'];
    }else{
        
?>
                            <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
<head>
<link rel="stylesheet" href="localhost/CSS.css"> 
<link rel="stylesheet" href="CSS.css">  
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="divreg">    
        <div>
        <a href="../sunburn/register.php">
        <button  type="button" >-REGISTRAR-</button>
        </a>
        </div>
        <div>
        <a href="../sunburn/deltePAGE.php">
        <button class="button1"  type="button" value="deltePAGE" >-DELETAR-</button>
        </a>
        </div>          
</div>          
    <div id="select">
    </div>
</body>
<?php
    }
    ?>

And I'm sorry about any typo I may have sent.


Solution

  • You are using exit before echo and you need to check condition on pag2.php like this :

    if(isset($_SESSION['logon']) && $_SESSION['logon'] != "") {
        $logon=$_SESSION['logon'];
            if($logon==0){
            //header("Location: index.php");
            echo $_SESSION['logon'];
            exit();
           }
    }