2015-11-02 69 views
2

我爲登錄authenrication實現PHP的API angularJS代碼遠程服務器:

http://somerestsertver.net/sampleapi/auth-login此設置登錄會話ID

(例如驗證用戶憑證後) http://somerestsertver.net/sampleapi/auth-check此檢查,如果會話ID設置或不

http://somerestsertver.net/sampleapi/auth-logout,這破壞了會話,需要註銷登錄是有效的...

我設置登錄與$ _SES SION [「id」] = 1當代碼 中的auth-login登錄時,那麼auht-check就可以,否則auth-check會包含錯誤。

當我在瀏覽器或ReST客戶端 中調用這些URL時可以,但在我的angularJS代碼中使用它們會返回http://somerestsertver.net/sampleapi/auth-check的錯誤!

看來會話集在客戶端無法通過PHPSESSID獲得,並且無法正常工作 它與沙箱或CORS或html頭部請求有關嗎?

+0

它與Access-Control-Allow-Origin – Ebrahim

回答

1

您好我解決了這個問題最後是這樣的:

客戶端,在angularJS我把這個在我的路由配置,以適用於所有請求REST的API

$httpProvider.defaults.withCredentials = true; 

我想我應該已經主要在.htaccess中使用Web服務器:

Header add Access-Control-Allow-Credentials "true" 

,但對我們的關注,我終於更新了整個.htaccess文件到以下幾點:

Header add Access-Control-Allow-Origin "http://localhost:3000" 
Header add Access-Control-Allow-Credentials "true" 
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type" 
Header add Access-Control-Allow-Methods "GET, POST" 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php [QSA,L] 
<FilesMatch "\.php$"> 
Order Allow,Deny 
Deny from all 
</FilesMatch> 
<FilesMatch "index[0-9]?\.php$"> 
Order Allow,Deny 
Allow from all 
</FilesMatch> 

我還在php中使用下面的JSON響應: $ response =「所需的JSON響應OBJECT」; $ status ='確定或未經身份驗證或...'; $ status ='200或403或...'; header(「Content-Type:application/json」); header(「HTTP/1.1 $ status $ status_message」); echo json_encode($ response); exit();

希望這個問題對您有所幫助