如何获取已登录用户的ID

时间:2019-04-24 12:55:41

标签: php sql mysqli

我有用于注册用户和登录用户的代码,对于项目的另一部分,我需要获取已登录用户的用户ID。

我用$ _SESSION ['id']尝试了一些例子。但我无法使它正常工作。这是整个注册/登录文件。

 <?php
session_start();

// initializing variables

$username = "";
$email    = "";
$errors = array(); 

// connect to the database
include '../../Connection/connection.php';

// REGISTER USER
if (isset($_POST['reg_user'])) {
  // receive all input values from the form
  $fname = mysqli_real_escape_string($con, $_POST['fname']);
  $sname = mysqli_real_escape_string($con, $_POST['sname']);
  $username = mysqli_real_escape_string($con, $_POST['username']);
  $email = mysqli_real_escape_string($con, $_POST['email']);
  $password_1 = mysqli_real_escape_string($con, $_POST['password_1']);
  $password_2 = mysqli_real_escape_string($con, $_POST['password_2']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($fname)) { array_push($errors, "First name is required"); }
  if (empty($sname)) { array_push($errors, "Surname is required"); }
  if (empty($username)) { array_push($errors, "Username is required"); }
  if (empty($email)) { array_push($errors, "Email is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
  }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
  $result = mysqli_query($con, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $query = "INSERT INTO users (Firstname, Surname, username, email, password) 
              VALUES('$fname','$sname', '$username', '$email', '$password')";
    mysqli_query($con, $query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";
    header('location: ../home_pageOwner.php');
  }
}

// LOGIN USER
if (isset($_POST['login_user'])) {  

  $username = mysqli_real_escape_string($con, $_POST['username']);
  $password = mysqli_real_escape_string($con, $_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 users WHERE username='$username' AND password='$password'";
    $results = mysqli_query($con, $query);

    if (mysqli_num_rows($results) == 1) {   
    $row = mysqli_fetch_assoc($results);
    $id =  $row['id'];

    //using session
    $_SESSION["id"] = $id;



      $_SESSION['username'] = $username;     
      $_SESSION['success'] = "You are now logged in";
      header('location: ../home_pageOwner.php');
    }else {
        array_push($errors, "Wrong username/password combination");
    }

  }


?>

这只是我需要的一个简单示例,在这里我需要说回声$ _SESSION ['id'];这是我的主页文件

 <?php 
session_start();
if(!isset($_SESSION['username'])){
   header("Location: registration/Owner/OwnerReg/loginOwner.php");
}

?>

    <b>Welcome</b><?php echo $_SESSION["username"] ; ?>

还:用户的ID在sql数据库中自动递增

编辑:已解决,这是我需要的:

 if (mysqli_num_rows($results) == 1) {
        while($row = mysqli_fetch_assoc($results)) {
        $_SESSION["OwnerID"] = $row["ownerID"];

0 个答案:

没有答案