使用會話我們可以實作這一點,但需要在沒有會話或 cookie 的情況下實作。
<?php
class Employees extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function auth() {
$adminEmail = $this->input->post('adminEmail');
$adminPassword = $this->input->post('adminPassword');
if ($adminEmail != "" && $adminPassword != "") {
$query = $this->db->query("select * from admin_tbl where email= '$adminEmail' and password = '$adminPassword'");
//if user exist
if ($query->num_rows() <= 0) {
$response = array();
$jwtoken = "";
$this->session->set_flashdata("invalid", "Wrong email or password");
$response = array(
'status' => 'invalid',
'message' => $_SESSION['invalid'],
'token' => $jwtoken,
);
//used to send finalized values
$this->output
->set_content_type('application/json')
->set_output(json_encode($response));
return $jwtoken; //return value
} else {
// $this->session->set_userdata('adminEmail', $adminEmail);
$response = array();
$jwt = new JWT();
$data = array(
'adminEmail' => $adminEmail,
'iat' => time()
);
$jwtoken = $jwt->encode($data, jwtSecretKey, 'HS256');
// I want to pass $jwtoken's variable to all the functions in a controller
$this->session->set_flashdata("login", "Scucessfully login!");
// if (isset($_SESSION['adminEmail'])) {
if ($jwtoken != "") {
$response = array(
'status' => 'valid',
'message' => $_SESSION['login'],
'token' => $jwtoken
);
}
$abc = $jwtoken;
//used to send finalized values
$this->output
->set_content_type('application/json')
->set_output(json_encode($response));
return $jwtoken; //return value
}
}
}
public function addNew() {
$response = array();
$this->auth(); // this value is always null returned by auth() method
}
}
uj5u.com熱心網友回復:
這更像是一個 OOP 編程基礎問題。如果你想在同一個控制器物件的另一個函式中重用一個變數,你必須為雇員類全域設定變數,然后使用 $this->yourVariableName 在你的函式中設定/獲取它的值。但是物件實體的設定值只能在該實體中重復使用。這意味著在 auth() 函式之后,應該隨后呼叫另一個函式來“訪問” $this->yourVariableName。另一種方法是將 $jwtoken 作為引數傳遞給函式。
但是以下代碼回答了您的問題“如何將一個函式的計算/最終值傳遞給 Codeigniter 應用程式控制器中的其他函式”,如果沒有,那么我猜您的問題應該得到糾正。
編輯: 好吧,首先呼叫 auth() 函式,然后您想將 $jwtoken 值傳遞給另一個函式,對嗎?好吧,一旦函式完成執行,如果沒有傳遞給另一個函式,變數就會“消失”。如果您想立即在 auth() 函式中處理 $jwtoken 值,那么答案是將 $jwtoken 值從 auth() 函式中傳遞給另一個函式:
<?php
class Employees extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function auth() {
$adminEmail = $this->input->post('adminEmail');
$adminPassword = $this->input->post('adminPassword');
if ($adminEmail != "" && $adminPassword != "") {
$query = $this->db->query("select * from admin_tbl where email= '$adminEmail' and password = '$adminPassword'");
//if user exist
if ($query->num_rows() <= 0) {
$response = array();
$jwtoken = "";
$this->session->set_flashdata("invalid", "Wrong email or password");
$response = array(
'status' => 'invalid',
'message' => $_SESSION['invalid'],
'token' => $jwtoken,
);
//used to send finalized values
$this->output
->set_content_type('application/json')
->set_output(json_encode($response));
return $jwtoken; //return value
} else {
// $this->session->set_userdata('adminEmail', $adminEmail);
$response = array();
$jwt = new JWT();
$data = array(
'adminEmail' => $adminEmail,
'iat' => time()
);
$jwtoken = $jwt->encode($data, jwtSecretKey, 'HS256');
// I want to pass $jwtoken's variable to all the functions in a controller
// this is one way you can pass the value to another function, depending on what you want to do, you can also place a condition and continue only if the return value of the following function is respected:
$this->addNew($jwtoken);
// What is the addNew() supposed to do?
$this->session->set_flashdata("login", "Scucessfully login!");
// if (isset($_SESSION['adminEmail'])) {
if ($jwtoken != "") {
$response = array(
'status' => 'valid',
'message' => $_SESSION['login'],
'token' => $jwtoken
);
}
$abc = $jwtoken;
//used to send finalized values
$this->output
->set_content_type('application/json')
->set_output(json_encode($response));
return $jwtoken; //return value
}
}
}
public function addNew($jwtoken = "default_value_if_not_set") {
echo $jwtoken;
}
}
uj5u.com熱心網友回復:
由于您正在創建一個 API,我假設該 API 是一個 REST api 并且是無狀態的,因此沒有會??話和 cookie 的干擾。
我假設你的程序是這樣的:
- 用戶從應用程式向 api 發出登錄請求,當憑據檢查有效時,api 回傳令牌
- 令牌存盤在應用程式中(例如在本地資料庫中)并用于其他請求
所以你唯一需要做的就是(我假設你有一個 addNew 的路線):
public function addNew() {
$token = $this->input->get('token');
$loginData = $this->validateToken($token);
//... add new process
}
從您的應用程式中,您需要將帶有請求的令牌傳遞給 api。
你如何驗證令牌?
要獲取您在令牌中設定的資料,您必須對令牌進行解碼:
/**
* throws SignatureInvalidException
*/
function validateToken($token)
{
$jwt = new JWT();
return $jwt->decode($token, jwtSecretKey, 'HS256');
}
代碼改進
避免使用會話和 cookie
由于您的 api 是無狀態的,您必須避免設定 cookie 或會話。因此,在您的控制器中,您可以洗掉閃存資料助手:
public function auth() {
$adminEmail = $this->input->post('adminEmail');
$adminPassword = $this->input->post('adminPassword');
if ($adminEmail != "" && $adminPassword != "") {
$query = $this->db->query("select * from admin_tbl where email= '$adminEmail' and password = '$adminPassword'");
//if user exist
if ($query->num_rows() <= 0) {
$response = array();
$jwtoken = "";
# REMOVE THIS LINE
# $this->session->set_flashdata("invalid", "Wrong email or password");
$response = array(
'status' => 'invalid',
'message' => "Wrong email or password", //CHANGE THIS LINE
'token' => $jwtoken,
);
//used to send finalized values
$this->output
->set_content_type('application/json')
->set_output(json_encode($response));
return $jwtoken; //return value
} else {
// $this->session->set_userdata('adminEmail', $adminEmail);
$response = array();
$jwt = new JWT();
$data = array(
'adminEmail' => $adminEmail,
'iat' => time()
);
$jwtoken = $jwt->encode($data, jwtSecretKey, 'HS256');
// I want to pass $jwtoken's variable to all the functions in a controller
# REMOVE THIS LINE
# $this->session->set_flashdata("login", "Scucessfully login!");
// if (isset($_SESSION['adminEmail'])) {
if ($jwtoken != "") {
$response = array(
'status' => 'valid',
'message' => "Scucessfully login!", //CHANGE THIS LINE
'token' => $jwtoken
);
}
$abc = $jwtoken;
//used to send finalized values
$this->output
->set_content_type('application/json')
->set_output(json_encode($response));
return $jwtoken; //return value
}
}
}
回傳輸出回應而不是 $jwtoken
在您的回應中,您已經設定了令牌,因此您可以簡單地回傳回應:
return $this->output
->set_content_type('application/json')
->set_output(json_encode($response));
您的查詢容易受到 sql 注入
在變數周圍使用轉義方法或系結引數:
$sql = "select * from admin_tbl where email=? and password = ?";
$query = $this->db->query($sql, array($adminEmail, $adminPassword));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/352921.html
下一篇:將資訊從AJAX傳遞到控制器類
