php会话不适用于弹出式登录

php会话不适用于弹出式登录,php,session,Php,Session,我不擅长这个,一直在尝试,但它不起作用。我不知道为什么get.php是索引页,它的文件header.php有另一个文件login.php。我不知道为什么会议没有按我认为应该的方式进行。登录时页脚应显示用户名,但不显示 get.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="styl

我不擅长这个,一直在尝试,但它不起作用。我不知道为什么
get.php
是索引页,它的文件
header.php
有另一个文件
login.php
。我不知道为什么会议没有按我认为应该的方式进行。登录时页脚应显示
用户名
,但不显示

get.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css" media="all">
  </head>
  <body>
    <?php include("header.php");?>
    <?php include("menu.php");?>
    <?php include("slider.php");?>
    <?php include("content.php");?>
    <div id="footer">
      <?php
        if(isset($_SESSION['currentuser'])==true)
        {
            echo"$username";
        }
        else
        {
            echo" not logged in ";
        }
      ?>
    </div>
  </body>
</html>
<?php
  session_start();
  include("connect.php");

  if(isset($_POST['login']))
  {
      $username=$_POST['username'];
      $password=$_POST['password'];
      $query="select * from user where username='$username' AND password='$password'";  
      $run=mysql_query($query);

      if(mysql_num_rows($run)>0)
      {
          $_SESSION['currentuser']=true; 
          header("Location:get.php");
      }
      else
      {
          header("Location:get.php#loginfail"); 
      }
  }
?>

login.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css" media="all">
  </head>
  <body>
    <?php include("header.php");?>
    <?php include("menu.php");?>
    <?php include("slider.php");?>
    <?php include("content.php");?>
    <div id="footer">
      <?php
        if(isset($_SESSION['currentuser'])==true)
        {
            echo"$username";
        }
        else
        {
            echo" not logged in ";
        }
      ?>
    </div>
  </body>
</html>
<?php
  session_start();
  include("connect.php");

  if(isset($_POST['login']))
  {
      $username=$_POST['username'];
      $password=$_POST['password'];
      $query="select * from user where username='$username' AND password='$password'";  
      $run=mysql_query($query);

      if(mysql_num_rows($run)>0)
      {
          $_SESSION['currentuser']=true; 
          header("Location:get.php");
      }
      else
      {
          header("Location:get.php#loginfail"); 
      }
  }
?>

试试这个:

if(isset($_SESSION['currentuser']) && $_SESSION['currentuser'] == true)

另外,请记住在每个需要会话的页面上包含
会话\u start()

会话只有在页面刷新后才能访问。您需要重新加载页面才能访问会话变量

另一个注意事项:无论您想在哪里使用任何类型的会话,您都需要从以下内容开始会话:

session_start();
因此,您的
get.php
文件如下所示:

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css" media="all">
  </head>

.....etc

每当我在每页上使用session\u start()时,我都会收到一个错误消息,即会话已经启动-忽略session\u start(),请确保您没有两次包含它。在get.php和header.php中有session_start()吗?header.php在索引页(get.php)中。我需要在这两个页面中都使用session_start()吗?不,只有一个,以先加载的为准,所以get.phpand在header.php中有login.php是正确的还是我必须有一个单独的页面来登录顺序是get.php(第一个主页)然后是header.php(包含文件),然后是login.php(包含在header中的文件,php)先生,我想像这个网站www.flipkart.com一样登录