簡體   English   中英

如何在PHP中將會話從用戶名更改為ID?

[英]How to change session from username to id in php?

        // 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。 因此,請幫助我如何獲得代碼的解決方案。

只需復制此代碼:替換您的代碼

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

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

您可以像這樣獲取值,因為我們將結果獲取到數組中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM