Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP可以';当调用两次时,在函数中重新声明对象_Php_Rest_Authentication - Fatal编程技术网

PHP可以';当调用两次时,在函数中重新声明对象

PHP可以';当调用两次时,在函数中重新声明对象,php,rest,authentication,Php,Rest,Authentication,我正在制作一个PHPAPI,并调用一个函数从我们的DBHandler中提取一个请求。当我获得“/Attendes”函数时,我正确地提取了数据。但是我想在api的每个调用中添加身份验证。我们将一个参数传递到DBHandler中,以指定要使用的连接字符串(因为我们有多个)。如果字符串定义为空,则将使用标准连接字符串 “身份验证”功能需要使用标准连接字符串,而“/Attendes”get需要不同的连接字符串。如前所述,“/Attendes”本身工作正常,但当我添加auth时,它在尝试执行sql时出错。

我正在制作一个PHPAPI,并调用一个函数从我们的DBHandler中提取一个请求。当我获得“/Attendes”函数时,我正确地提取了数据。但是我想在api的每个调用中添加身份验证。我们将一个参数传递到DBHandler中,以指定要使用的连接字符串(因为我们有多个)。如果字符串定义为空,则将使用标准连接字符串

“身份验证”功能需要使用标准连接字符串,而“/Attendes”get需要不同的连接字符串。如前所述,“/Attendes”本身工作正常,但当我添加auth时,它在尝试执行sql时出错。我知道这是因为身份验证使用了不同的连接字符串,并且以某种方式覆盖了连接

require_once '../include/DbHandler.php';
require_once '../include/PassHash.php';
require '.././libs/Slim/Slim.php';
ini_set('display_errors', 1);
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$user_id = NULL;

 function authenticate(\Slim\Route $route) {
     $headers = apache_request_headers();
     $response = array();
     $app = \Slim\Slim::getInstance();
     $dm_config = null;
     if (isset($headers['Auth'])) {
         $db = new DbHandler($dm_config);
         $api_key = $headers['Auth'];
         if (!$db->isValidApiKey($api_key)) {
             $response["error"] = true;
             $response["message"] = "Access Denied. Invalid Api key";
             echoRespnse(401, $response);
             $app->stop();
         } else {
           $response["error"] = false;
           $response["message"] = "Auth Accepted";
         }
     } else {
         $response["error"] = true;
         $response["message"] = "Api key is misssing";
         echoRespnse(400, $response);
         $app->stop();
     }
 }

$app->get('/attendees','authenticate', function() {
            global $event_id;
            $headers = apache_request_headers();
            $event_id = $headers['Eventid'];
            $dm_config = $headers['Dm'];
            $user_id = null;
            $response = array();
            $db = new DbHandler($dm_config);
            $result = $db->getAttendees($user_id, $event_id);
            $response["error"] = false;
            $response["nav"] = array();
            while ($task = $result->fetch_assoc()) {
                $tmp = array();
                $tmp["ea.id"] = $task["ea.id"];
                array_push($response["nav"], $tmp);
            }
            echoRespnse(200, $response);
        });

如何让函数调用同一个对象但单独使用?

在两个函数中传递相同的连接字符串
$dm_config


每个连接都应该有一个不同的变量。

在两个函数中传递相同的连接字符串
$dm\u config

require_once '../include/DbHandler.php';
require_once '../include/PassHash.php';
require '.././libs/Slim/Slim.php';
ini_set('display_errors', 1);
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$user_id = NULL;

 function authenticate(\Slim\Route $route) {
     $headers = apache_request_headers();
     $response = array();
     $app = \Slim\Slim::getInstance();
     $dm_config = null;
     if (isset($headers['Auth'])) {
         $db = new DbHandler($dm_config);
         $api_key = $headers['Auth'];
         if (!$db->isValidApiKey($api_key)) {
             $response["error"] = true;
             $response["message"] = "Access Denied. Invalid Api key";
             echoRespnse(401, $response);
             $app->stop();
         } else {
           $response["error"] = false;
           $response["message"] = "Auth Accepted";
         }
     } else {
         $response["error"] = true;
         $response["message"] = "Api key is misssing";
         echoRespnse(400, $response);
         $app->stop();
     }
 }

$app->get('/attendees','authenticate', function() {
            global $event_id;
            $headers = apache_request_headers();
            $event_id = $headers['Eventid'];
            $dm_config = $headers['Dm'];
            $user_id = null;
            $response = array();
            $db = new DbHandler($dm_config);
            $result = $db->getAttendees($user_id, $event_id);
            $response["error"] = false;
            $response["nav"] = array();
            while ($task = $result->fetch_assoc()) {
                $tmp = array();
                $tmp["ea.id"] = $task["ea.id"];
                array_push($response["nav"], $tmp);
            }
            echoRespnse(200, $response);
        });

每个连接都应该有一个不同的变量。

您可以通过声明
静态$db
变量初始化为null,并在连接db之前使用通过empty()函数检查它是否为空

require_once '../include/DbHandler.php';
require_once '../include/PassHash.php';
require '.././libs/Slim/Slim.php';
ini_set('display_errors', 1);
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$user_id = NULL;

 function authenticate(\Slim\Route $route) {
     $headers = apache_request_headers();
     $response = array();
     $app = \Slim\Slim::getInstance();
     $dm_config = null;
     if (isset($headers['Auth'])) {
         $db = new DbHandler($dm_config);
         $api_key = $headers['Auth'];
         if (!$db->isValidApiKey($api_key)) {
             $response["error"] = true;
             $response["message"] = "Access Denied. Invalid Api key";
             echoRespnse(401, $response);
             $app->stop();
         } else {
           $response["error"] = false;
           $response["message"] = "Auth Accepted";
         }
     } else {
         $response["error"] = true;
         $response["message"] = "Api key is misssing";
         echoRespnse(400, $response);
         $app->stop();
     }
 }

$app->get('/attendees','authenticate', function() {
            global $event_id;
            $headers = apache_request_headers();
            $event_id = $headers['Eventid'];
            $dm_config = $headers['Dm'];
            $user_id = null;
            $response = array();
            $db = new DbHandler($dm_config);
            $result = $db->getAttendees($user_id, $event_id);
            $response["error"] = false;
            $response["nav"] = array();
            while ($task = $result->fetch_assoc()) {
                $tmp = array();
                $tmp["ea.id"] = $task["ea.id"];
                array_push($response["nav"], $tmp);
            }
            echoRespnse(200, $response);
        });
function authenticate(\Slim\Route $route) {
     static $db=null;
     $headers = apache_request_headers();
     $response = array();
     $app = \Slim\Slim::getInstance();
     $dm_config = null;
     if (isset($headers['Auth'])) {
         if(empty($db))
         {
             $db = new DbHandler($dm_config);
         }
         $api_key = $headers['Auth'];
         if (!$db->isValidApiKey($api_key)) {
             $response["error"] = true;
             $response["message"] = "Access Denied. Invalid Api key";
             echoRespnse(401, $response);
             $app->stop();
         } else {
           $response["error"] = false;
           $response["message"] = "Auth Accepted";
         }
     } else {
         $response["error"] = true;
         $response["message"] = "Api key is misssing";
         echoRespnse(400, $response);
         $app->stop();
     }
 }
或者,您可以创建全局变量$db,并在这两个函数中通过全局关键字访问它,如下所述

require_once '../include/DbHandler.php';
require_once '../include/PassHash.php';
require '.././libs/Slim/Slim.php';
ini_set('display_errors', 1);
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$user_id = NULL;

$db=null; //global bariable

 function authenticate(\Slim\Route $route) {
     global $db; // access global variable
     $headers = apache_request_headers();
     $response = array();
     $app = \Slim\Slim::getInstance();
     $dm_config = null;
     if (isset($headers['Auth'])) {
         if(!empty($db)) {$db = new DbHandler($dm_config)};
         $api_key = $headers['Auth'];
         if (!$db->isValidApiKey($api_key)) {
             $response["error"] = true;
             $response["message"] = "Access Denied. Invalid Api key";
             echoRespnse(401, $response);
             $app->stop();
         } else {
           $response["error"] = false;
           $response["message"] = "Auth Accepted";
         }
     } else {
         $response["error"] = true;
         $response["message"] = "Api key is misssing";
         echoRespnse(400, $response);
         $app->stop();
     }
 }

$app->get('/attendees','authenticate', function() {
            global $db; // access global variable
            global $event_id;
            $headers = apache_request_headers();
            $event_id = $headers['Eventid'];
            $dm_config = $headers['Dm'];
            $user_id = null;
            $response = array();
            if(!empty($db)) {$db = new DbHandler($dm_config)};
            $result = $db->getAttendees($user_id, $event_id);
            $response["error"] = false;
            $response["nav"] = array();
            while ($task = $result->fetch_assoc()) {
                $tmp = array();
                $tmp["ea.id"] = $task["ea.id"];
                array_push($response["nav"], $tmp);
            }
            echoRespnse(200, $response);
        });

您可以通过声明
static$db
变量初始化为null,并在连接db之前使用expect()函数检查它是否为空

function authenticate(\Slim\Route $route) {
     static $db=null;
     $headers = apache_request_headers();
     $response = array();
     $app = \Slim\Slim::getInstance();
     $dm_config = null;
     if (isset($headers['Auth'])) {
         if(empty($db))
         {
             $db = new DbHandler($dm_config);
         }
         $api_key = $headers['Auth'];
         if (!$db->isValidApiKey($api_key)) {
             $response["error"] = true;
             $response["message"] = "Access Denied. Invalid Api key";
             echoRespnse(401, $response);
             $app->stop();
         } else {
           $response["error"] = false;
           $response["message"] = "Auth Accepted";
         }
     } else {
         $response["error"] = true;
         $response["message"] = "Api key is misssing";
         echoRespnse(400, $response);
         $app->stop();
     }
 }
或者,您可以创建全局变量$db,并在这两个函数中通过全局关键字访问它,如下所述

require_once '../include/DbHandler.php';
require_once '../include/PassHash.php';
require '.././libs/Slim/Slim.php';
ini_set('display_errors', 1);
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$user_id = NULL;

$db=null; //global bariable

 function authenticate(\Slim\Route $route) {
     global $db; // access global variable
     $headers = apache_request_headers();
     $response = array();
     $app = \Slim\Slim::getInstance();
     $dm_config = null;
     if (isset($headers['Auth'])) {
         if(!empty($db)) {$db = new DbHandler($dm_config)};
         $api_key = $headers['Auth'];
         if (!$db->isValidApiKey($api_key)) {
             $response["error"] = true;
             $response["message"] = "Access Denied. Invalid Api key";
             echoRespnse(401, $response);
             $app->stop();
         } else {
           $response["error"] = false;
           $response["message"] = "Auth Accepted";
         }
     } else {
         $response["error"] = true;
         $response["message"] = "Api key is misssing";
         echoRespnse(400, $response);
         $app->stop();
     }
 }

$app->get('/attendees','authenticate', function() {
            global $db; // access global variable
            global $event_id;
            $headers = apache_request_headers();
            $event_id = $headers['Eventid'];
            $dm_config = $headers['Dm'];
            $user_id = null;
            $response = array();
            if(!empty($db)) {$db = new DbHandler($dm_config)};
            $result = $db->getAttendees($user_id, $event_id);
            $response["error"] = false;
            $response["nav"] = array();
            while ($task = $result->fetch_assoc()) {
                $tmp = array();
                $tmp["ea.id"] = $task["ea.id"];
                array_push($response["nav"], $tmp);
            }
            echoRespnse(200, $response);
        });

一个新手错误,但不幸的是没有任何区别
authenticate()
变量
$dm\u config
作为null传递给构造函数。这是故意的吗?是的,构造函数知道它是否提供了null以使用特定的连接字符串,这是一个新手错误,但不幸的是没有任何区别
authenticate()
变量
$dm_config
作为null传递给构造函数。这是故意的吗?是的,构造函数知道它是否提供了null以使用特定的连接字符串谢谢,它的工作稍微好一点-如果我提供了错误的代码,我会得到正确的身份验证错误,但是如果它是对的,“/Attendes”仍然会得到错误。就好像它没有通过正确的数据库。需要注意的是,我正在分解上述答案并在authenticate中重命名变量。有没有任何方法可以检查身份验证的结果,并将其包装在与会者获得的结果周围,这样他们就不会同时被调用?两种方法中的检查身份验证都有问题,对吗?。。您可以使用全局变量,因为我已经编辑了我的应答器。不幸的是,在您的数据库中,它被声明为null,并围绕着空if,因此在下面的行中调用db时它会出错。因此,我将调用db的所有代码包装在空if中,不出意外,没有返回任何内容,因为db为空谢谢,它的工作稍微好一点-如果我提供了错误的代码,我会得到正确的auth错误,但是如果它是对的,“/attendes”仍然会得到错误。就好像它没有通过正确的数据库。需要注意的是,我正在分解上述答案并在authenticate中重命名变量。有没有任何方法可以检查身份验证的结果,并将其包装在与会者获得的结果周围,这样他们就不会同时被调用?两种方法中的检查身份验证都有问题,对吗?。。您可以使用全局变量,因为我已经编辑了我的应答器。不幸的是,在您的数据库中,它被声明为null,并围绕着空if,因此在下面的行中调用db时它会出错。因此,我将调用db的所有代码包装在空if中,毫不奇怪,因为db为null,所以没有返回任何内容