如何在php中将会话从用户名更改为id?

如何在php中将会话从用户名更改为id?,php,mysql,session,Php,Mysql,Session,这段代码也可以工作,但我想将用户名会话替换为id会话。请帮助我如何用id替换它。 在这里,我想从用户名中将会话设置为id。因此,请帮助我如何获得代码的解决方案。只需设置$\u会话['id']=$id只需复制以下代码: 替换您的代码 // LOGIN USER if (isset($_POST['login_user'])) { $username = mysqli_real_escape_string($db, $_POST['username

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

只需设置
$\u会话['id']=$id

只需复制以下代码: 替换您的代码

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

看起来像:

$_SESSION['id'] = $username;

您可以这样获得值,因为我们要将结果放入一个数组中

$row=mysqli\u fetch\u数组($result)

$id=$row[0]

正确的方法如下

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

旁注:与使用
密码\u hash()
安全散列相比,使用MD5密码有什么特别的原因吗?或者这只是为了学术目的?MD5一点也不安全。当我更新用户配置文件以便会话时,我会因为更改用户名而被销毁。设置用户名的原因是,我希望将用户名替换为会话id,而MD5用于学术目的
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");
}
// 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");
        }
      }
    ?>