Installing $ _SESSION (php) from a remote server using angularJS code does not work

I have api with php for login authentication:

http://somerestsertver.net/sampleapi/auth-login sets the login session identifier (for example, after checking user credentials)

http://somerestsertver.net/sampleapi/auth-check , this checks that the login is valid if the session identifier is set or not

http://somerestsertver.net/sampleapi/auth-logout and this destroys the session and requires logout ...

I set the login with $ _SESSION ["id"] = 1, when auth-login in the code, then auht-check will be fine, otherwise authentication will contain errors.

this is normal when I call these URLs in a browser or ReST client, but using them in my corner JS code returns errors for http://somerestsertver.net/sampleapi/auth-check !

it seems that the session set is not accessible through the PHPSESSID in the client and it is not working properly Is this related to sandboxes or CORS or html header requests?

+2
source share
1 answer

Hi, I finally decided to solve this problem:

Client side, in angularJS I put this in my config-route to apply to all requests in the ReST-API

$httpProvider.defaults.withCredentials = true; 

I think that basically I should use in .htaccess for the web server:

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

but for your attention, I completely updated the entire .htaccess file:

 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> 

I also use the following for JSON response in php: $ response = "desired OBJECT JSON response"; $ status = 'OK or unauthenticated or ...'; $ status = '200 or 403 or ...'; Title ("Content-Type: Application / JSON"); header ("HTTP / 1.1 $ status $ status_message"); echo json_encode ($ response); Output();

Hope this question and answer help you

+1
source

Source: https://habr.com/ru/post/1386189/


All Articles