php会话不适用于弹出登录 [英] php session not working for popup login

查看:48
本文介绍了php会话不适用于弹出登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

get.php

<html lang="zh-cn"><头><meta charset="utf-8"><link rel="stylesheet" href="style.css" media="all"><身体><?php include("header.php");?><?php include("menu.php");?><?php include("slider.php");?><?php include("content.php");?><div id="页脚"><?phpif(isset($_SESSION['currentuser'])==true){echo"$用户名";}别的{回声未登录";}?>

</html>

login.php

0){$_SESSION['currentuser']=true;header("位置:get.php");}别的{header("位置:get.php#loginfail");}}?>

解决方案

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

另一个注意事项:无论您想在任何地方使用任何类型的会话,您都需要以以下方式开始会话:

session_start();

所以你的 get.php 文件看起来像这样:

<!DOCTYPE html><html lang="zh-cn"><头><meta charset="utf-8"><link rel="stylesheet" href="style.css" media="all">.....等等

I am not good at this and have been trying but its not working. I don't know why the get.php is index page and its file header.php has another file login.php. I don't know why the session is not working the way I think it should. Footer should show username when logged in but it doesn't.

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>

login.php

<?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"); 
      }
  }
?>

解决方案

Sessions can only be accessed after a page refresh when they are set. You'll need to reload the page to be able to access the session variables.

Another note: Everywhere you want you use any kind of session, you'll need to start the session with:

session_start();

So your get.php file would look something like this:

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

.....etc

这篇关于php会话不适用于弹出登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆