从php文件回显会话变量时,页面变为空白

时间:2017-01-24 03:16:14

标签: php html session

有两个文件,index.php和file.php。我将file.php中的会话变量设置为1.然后我检查index.php是否将此会话变量设置为1.如果是,则应该回显该变量。但相反,我的浏览器空白。看起来所有的html代码都在浏览器的开发人员工具中看到(没有html内容)。如果我注释掉它,而是尝试回显满足if条件的字符串,那么页面会出现,但不会回显字符串。

file.php

<?php
session_start();
?>

<html>
<head>
  <title>Password test, page 2</title>
<head>
<body>
  .
  .
  .
 <?php
  if($name === $good_name && $pass === $good_pass){
        echo "That is the correct log-in information";
     }else{
        $_SESSION["flag"] = 1; // setting the variable in file.php
        header('Location: index.php'); exit(); //directing to that file
     } 
  ?>    
</body>
</html>

的index.php

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
     .
     .
     .
<?php
if ($_SESSION["flag"] === 1)
    {echo "invalid";  //this doesn't output the string
    echo . $_SESSION["flag"]; //this gives me blank page
}
?>

   

我的代码语法错误或逻辑是否错误?请指出。

编辑#1 file.php

<?php
    session_start();
?>

<html>
  <head>
    <title>Password test, page 2</title>
  <head>


  <body>
      <?php 
         $name  = $_POST['username'];
         $pass  = $_POST['password']; 

       //read the contents of our password file.
         $myFile = "pass/password.txt";
         $fh = fopen($myFile, 'r');
         $data = fgets($fh);
         fclose($fh);
         $data1 = trim($data);

         //split the text into an array
         $text = explode(":",$data1);

         //assign the data to variables
         $good_name = $text[0];
         $good_pass = $text[1];

         //compare the strings
         if($name === $good_name && $pass === $good_pass){
            echo "That is the correct log-in information";
         }else{
            $_SESSION["flag"] = 1;
            header('Location: index.php'); exit();
         } 
      ?>    
        <br> 

  </body>
</html>

0 个答案:

没有答案