【问题标题】:How to create session of username in php? [duplicate]如何在php中创建用户名会话? [复制]
【发布时间】:2017-04-29 07:59:05
【问题描述】:

我在我的项目中使用核心 php 和 mysql。我被困在会话部分。我无法理解如何为用户名创建会话以在登录后显示他的名字。我尝试了很多次但失败了。请帮助我。下面是我的 insertUser.php 代码。其中总共有 3 个登录查询。我正在为超级管理员进行第二次查询。正如您在我的代码中注意到的那样,我为超级管理员的用户名创建会话。我在 home.php 中包含 menubar.php、header.php 和 sidebar.php。我想在 menubar.php 和 header.php 中添加会话。

插入用户.php

<?php
include 'db.php';
if (isset($_REQUEST['insert'])) 
    {
        $acc_name = $_REQUEST['username'];
        $acc_email = $_REQUEST['email'];
        $acc_pass = $_REQUEST['password'];
        $role_id = $_REQUEST['roleid'];
        $sql = mysqli_query($conn,"INSERT INTO `accountants`(`acc_name`, `acc_email`, `acc_pass`, `roleId`) VALUES ('".$acc_name."','".$acc_email."','".$acc_pass."','2')");
        if ($sql>0) 
            {
                header('Location: home.php');
                echo 'data added successfully';
            }
        $row = mysqli_query('SELECT * FROM `accountants`');     
        $data = mysqli_fetch_array($row);
        $data = mysqli_num_rows($conn,$data);
        $_SESSION['role'] = $data['roleId'];
    }
if (isset($_REQUEST['submit'])) 
{
        $username = $_REQUEST['user'];
        $password = $_REQUEST['pass'];
        $sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '".$username."' AND `acc_pass` = '".$password."'");
        $data = mysqli_fetch_array($sql);        
        $_SESSION['role']=$data['roleId'];
        $_SESSION['username']=$data['acc_name'];
        $sql1 = mysqli_query($conn,"SELECT * FROM `superadmin` where `username` = '".$username."' AND `password` = '".$password."'");
        $data02 = mysqli_fetch_array($sql1);        
        $_SESSION['role']=$data02['roleId'];

        $sql2 = mysqli_query($conn,"SELECT * FROM `member` where `email` = '".$username."' AND `password` = '".$password."'");
        $data01 = mysqli_fetch_array($sql2);        
        $_SESSION['role']=$data01['roleId'];
        $_SESSION['username']=$data01['fname'];
        if ($data01>0) 
        {
            header('Location: DigitalSociety - Member/production/societyList.php');    
        }
        elseif ($data>0) 
            {
                header('Location: societyList.php');
            }
       elseif ($data02>0) {
            $_SESSION['login_user']=$username;
                header('Location: DigitalSociety - Superadmin/production/societyList.php');
       }

       else
            {
                header('Location: index.php');
                echo 'incorrect login';
            }
}
?>

menubar.php

<div class="profile clearfix">
  <div class="profile_pic">
    <img src="images/img.jpg" alt="..." class="img-circle profile_img">
  </div>
  <div class="profile_info">
    <span>Welcome,</span>
    <h2><?php echo $_SESSION['login_user'];?></h2>
  </div>
</div>

我使用上面的代码来显示用户名它显示错误Undefined variable: _SESSION in

请帮助我。

【问题讨论】:

  • 在顶部开始会话

标签: php mysql session login


【解决方案1】:

您的文件顶部缺少session_start();

【讨论】:

    【解决方案2】:

    一开始你必须使用

    <?php
     session_start();
     include 'db.php';    
    

    在你的 php 页面的第一行你应该使用 session_start()。

    【讨论】:

      【解决方案3】:

      首先,您应该从不存储纯文本密码!最好使用原生PHP函数,如password_hashpassword_verify

      请参考:

      ->http://php.net/manual/en/function.password-hash.php

      ->http://php.net/manual/en/function.password-verify.php

      那么,您需要在页面顶部使用session_start

      -> 见http://php.net/manual/en/session.examples.basic.php

      最后但同样重要的是,永远不要相信用户的数据!你应该真的考虑使用PPS : Prepared Parameterized Statements。这将有助于Preventing SQL injection

      【讨论】:

        【解决方案4】:

        您应该在文件的&lt;?php 之后的第一行 中插入session_start(); 以开始使用会话。

        编辑:

        正如'OldPadawan's answer中提到的,如果你不够小心,可能会执行恶意查询,例如我们有类似 "SELECT * FROM USERS WHERE USER = '$USER';" 的东西,我们假设 $USER = "Jac'k"; 这使得查询看起来像"SELECT * FROM USERS WHERE USER = 'Jac'k';",当然会出现错误,并且可能会因利用 SQLi 漏洞而破坏数据库。 在这种情况下,您需要使用mysqli_real_escape_string($con,$USER); 它的真正作用是什么?它在单引号之前添加一个反斜杠,就像 "Jac\'k"。

        【讨论】:

          猜你喜欢
          • 2019-04-17
          • 1970-01-01
          • 2011-07-08
          • 2012-04-05
          • 1970-01-01
          • 2015-03-05
          • 2012-01-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多