【问题标题】:How to change session from username to id in php?如何在php中将会话从用户名更改为id?
【发布时间】:2018-07-20 12:26:53
【问题描述】:
        // LOGIN USER
        if (isset($_POST['login_user'])) {
          $username = mysqli_real_escape_string($db, $_POST['username']);
          $password = mysqli_real_escape_string($db, $_POST['password']);

          if (empty($username)) {
            array_push($errors, "Username is required");
          }
          if (empty($password)) {
            array_push($errors, "Password is required");
          }

          if (count($errors) == 0) {
            $password = md5($password);
            $query = "SELECT * FROM register WHERE username='$username' AND password='$password'";
            $results = mysqli_query($db, $query);
        }   

            $qry = "SELECT * from register";
            $result = mysqli_query($qry);

            $row = mysqli_fetch_array($result);
            $id = $row[0];


            if (mysqli_num_rows($results) == 1) {
    //here i want to change session from username to id          
     $_SESSION['username'] = $username;
              $_SESSION['success'] = "You are now logged in";
              header('location: index.php');
            }else {
                array_push($errors, "Wrong username/password combination");
            }
          }
        ?>

此代码也有效,但我想将用户名会话替换为 id 会话。请帮助我如何用 id 替换它。 在这里,我想将会话设置为用户名中的 id。所以请帮助我如何获得我的代码的解决方案。

【问题讨论】:

  • 旁注:使用 MD5 密码而不是使用 password_hash() 安全地散列密码有什么特殊原因吗?或者这只是为了学术目的? MD5 根本不安全。
  • 当我更新用户配置文件时,会话由于更改设置的用户名而被破坏,这就是为什么我想用 id 作为会话替换用户名作为会话,而 md5 用于学术目的

标签: php mysql session


【解决方案1】:

只需复制此代码: 替换你的代码

$_SESSION['username'] = $username; 

$_SESSION['id'] = $username;

看起来像:

if (mysqli_num_rows($results) == 1) {
    //here i want to change session from username to id          
    $_SESSION['id'] = $username;
    $_SESSION['success'] = "You are now logged in";
    header('location: index.php');
}else {
    array_push($errors, "Wrong username/password combination");
}

【讨论】:

    【解决方案2】:

    你可以这样获取值,因为我们是把结果放入一个数组中

    $row = mysqli_fetch_array($result);

    $id = $row[0];

    正确的方法如下

    // LOGIN USER
        if (isset($_POST['login_user'])) {
          $username = mysqli_real_escape_string($db, $_POST['username']);
          $password = mysqli_real_escape_string($db, $_POST['password']);
    
          if (empty($username)) {
            array_push($errors, "Username is required");
          }
          if (empty($password)) {
            array_push($errors, "Password is required");
          }
    
          if (count($errors) == 0) {
            $password = md5($password);
            $query = "SELECT * FROM register WHERE username='$username' AND password='$password'";
            $results = mysqli_query($db, $query);
        }   
    
            $qry = "SELECT * from register";
            $result = mysqli_query($qry);
    
            $row = mysqli_fetch_array($result);
    
            // $id =  $row[0];
    
            $id = $row['id'];  //this is the primary key
    
    
            if (mysqli_num_rows($results) == 1) {         
              $_SESSION['id'] = $id;
              $_SESSION['success'] = "You are now logged in";
              header('location: index.php');
            }else {
                array_push($errors, "Wrong username/password combination");
            }
          }
        ?>
    

    【讨论】:

      【解决方案3】:

      只需设置$_SESSION['id'] = $id

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-31
        • 1970-01-01
        • 1970-01-01
        • 2020-08-01
        • 1970-01-01
        • 2015-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多