問題描述
我正在使用 Zend 構建 Web 服務,我必須在向用戶發送響應之前對其進行身份驗證.用戶將使用 curl 向服務器頁面發送請求,并以 curl_setopt($curl, CURLOPT_USERPWD, 'key:pass')
;
I am building a web service with Zend and i have to authenticate the users before sending them response. The user will send a request to a server page, using curl, passing his credentials in the form of curl_setopt($curl, CURLOPT_USERPWD, 'key:pass')
;
我使用 Zend 框架,因此服務器端頁面表示為:
Iam using Zend framework and so the server side page is denoted like:
http://www.example.com/app_name/public/controller/action/parameter一個>
這里是用戶請求的總代碼(client.php):
Here is the total code for user's request (client.php):
<?php
$curl = curl_init('http://www.example.com/app/public/user/add/1');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, 'username:password');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Sample Code');
$response = curl_exec($curl);
$resultStatus = curl_getinfo($curl);
if($resultStatus['http_code'] == 200) {
echo $response;
} else {
echo 'Call Failed '.print_r($resultStatus);
}
?>
現在我想要的是,我必須能夠在服務器端(在我的 Zend 控制器中)檢索用戶名和密碼,以便我可以驗證數據庫中的用戶憑據并相應地發送響應.那么如何在另一個頁面中進行身份驗證?
Now what i want is that, i must be able to retrieve the username and password at the server side (in my zend controller), so that i can verify the user credentials from database and send the response accordingly. So how can i do authentication in another page?
推薦答案
Zend Framework 有一個現成的組件來處理 HTTP 身份驗證.
Zend Framework has a ready-made component for handling HTTP authentication.
在
- http://framework.zend.com/manual/en/zend.auth.adapter.http.html
如果你不想使用它,你仍然可以走老派"的方式,如
If you don't want to use that, you can still go the "old-school" way, as described in
- http://php.net/manual/en/features.http-auth.php一個>
并手動檢查 $_SERVER['PHP_AUTH_USER']
和 $_SERVER['PHP_AUTH_PW']
這篇關于服務器端的 CURL HTTP 身份驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!