gpt4 book ai didi

php - 登录成功后想返回用户roleid

转载 作者:行者123 更新时间:2023-11-29 19:30:03 25 4
gpt4 key购买 nike

下面是我的 MySQLDao.php

<?php
class MySQLDao {
var $dbhost = null;
var $dbuser = null;
var $dbpass = null;
var $conn = null;
var $dbname = null;
var $result = null;

function __construct() {
$this->dbhost = Conn::$dbhost;
$this->dbuser = Conn::$dbuser;
$this->dbpass = Conn::$dbpass;
$this->dbname = Conn::$dbname;
}


// function to open connection

public function openConnection() {
$this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
if (mysqli_connect_errno())
echo new Exception("Could not establish connection with database");
}

// function to return the connection

public function getConnection() {
return $this->conn;
}

// function to close the connection

public function closeConnection() {
if ($this->conn != null)
$this->conn->close();
}

// function to get user email

public function getUserDetails($email)
{
$returnValue = array();
$sql = "select * from ap_users where user_email='" . $email . "'";

$result = $this->conn->query($sql);
if ($result != null && (mysqli_num_rows($result) >= 1)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (!empty($row)) {
$returnValue = $row;
}
}
return $returnValue;
}

// get user details using email and password

public function getUserDetailsWithPassword($email, $userPassword, $roleid)
{
$returnValue = array();
$sql = "select id,user_email from ap_users where user_email='" . $email . "' and user_password='" .$userPassword . "' and user_roleid='" . $roleid . "'";
$result = $this->conn->query($sql);
if ($result != null && (mysqli_num_rows($result) >= 1)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (!empty($row)) {
$returnValue = $row;
}
}
return $returnValue;
}

// register user with all fields

public function registerUser($email, $password, $username, $fname, $lname, $mobile, $roleid)
{
$sql = "insert into ap_users set user_email=?, user_password=?, user_username=?, user_fname=?, user_lname=?, user_mobile=?, user_roleid=?";
$statement = $this->conn->prepare($sql);

if (!$statement)
throw new Exception($statement->error);

$statement->bind_param("sssssss", $email, $password, $username, $fname, $lname, $mobile, $roleid);
$returnValue = $statement->execute();

return $returnValue;
}

}
?>

我的 UserLogin.php 如下:

<?php

require("Conn.php");
require("MySQLDao.php");
$email = htmlentities($_POST["email"]);
$password = htmlentities($_POST["password"]);
$returnValue = array();

if(empty($email) || empty($password))
{
$returnValue["status"] = "error";
$returnValue["message"] = "Missing required field";
echo json_encode($returnValue);
return;
}

$secure_password = md5($password);

$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetailsWithPassword($email,$secure_password);

if(!empty($userDetails))
{
$returnValue["status"] = "Success";
$returnValue["message"] = "User is logged in";
$returnValue["role"] = "'" .$roleid. "'";
echo json_encode($returnValue);
} else {

$returnValue["status"] = "error";
$returnValue["message"] = "User is not found";
echo json_encode($returnValue);
}

$dao->closeConnection();

?>

这里的问题是当我推送 $roleid 值时它总是显示 null。结果是这样的:{“status”:“成功”,“message”:“用户已登录”,“角色:”“”}

最佳答案

添加了 MySqlDao getUserDetailsWithPassword() 方法

$sql = "从 ap_users 中选择 id,user_email,user_roleid,其中 user_email='"。 $电子邮件。 "' 和 user_password='".$userPassword 。 “' 和 user_roleid='” 。 $角色。 “'”;

并在用户登录中更改

$returnValue["role"] = "'".$roleid。 “'”;行到 $returnValue['user_roleid'] = $userDetails['user_roleid'];

这达到了我想要的结果!

关于php - 登录成功后想返回用户roleid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41848305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com