I've been trying to retrieve cookie from php for awhile.

from php I have this method:
public function created()
{
$datetime = date_create()->format('Y-m-d H:i:s');
$json =file_get_contents('php://input');
var_dump($json);
$decoded = json_decode($json, true);
$cookie="";
$obj=new \stdClass();
if($decoded['cookie']==""){
$cookie=$random = rand(1,1000);
setcookie( "cookie", "$cookie", time() + (10 * 365 * 24 * 60 * 60), "/", "http://localhost" );
}
else{
$cookie=$decoded['cookie'];
}
$user=[
'name'=>$decoded['name'],
'surname'=>$decoded['surname'],
'email'=>$decoded['email'],
'review'=>$decoded['review'],
'time'=>$datetime,
'cookie'=>$cookie,
];
$obj->cookie=$cookie;
DatabaseModel::newuser($user);
//return response()->json($cookie);
}
which creates cookie if none exists and I see it in my response headers. (Don't pay attention to how I create cookie for now, I now rand() function is not the right way, it's just for testing)
In my angular side I have :
function PostReview(JSONObject) {
if (JSONObject != null) {
debugger;
$http({
url: 'http://localhost:8000/creation',
method: "POST",
data: JSONObject,
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8;",
"Accept": "application/json"
},
})
.then(function (data) {
console.log(data.headers('Set-cookie'));
}, function (data) {
console.log(data.headers('Set-cookie'));
});
}
}
I would like to post my JSON object and recieve cookie with response. However I am quite stuck. I tried with .success() but it's deprecated, so I am using .then() , but then I print console.log(data.headers('Set-cookie')); I get null result. Does anybody ever had similar problem?
I also tried:
.then(function (data, status, headers, config) {
console.log(headers('Set-cookie'));
}, function (data, status, headers, config) {
console.log(headers('Set-cookie'));
});
and:
.then(function(response) {
if (response.data == 'ok') {
$cookies['X-AUTH-TOKEN']=response.headers('X-AUTH-TOKEN');
// below put,put,putObject Cookies value is valid for Angular version >= 1.4
var $cookie= $cookies.putObject('X-AUTH-TOKEN',response.headers('X-AUTH-TOKEN'));
console.log($cookie);
debugger;
}
});
but none of them worked. What problem could it be?
My client side:
function PostReview(JSONObject) {
if (JSONObject != null) {
debugger;
$http({
url: 'http://localhost:8000/creation',
method: "POST",
data: JSONObject,
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8;",
"Accept": "application/json"
}
})
}
}
in my controllers:
$scope.Cookie = $cookies.get('cookie');
$scope.$watch('Cookie', function() {
$scope.Cookie = $cookies.get('cookie');
});
WHhat about cross-domain cookies?
$cookiesin your controller and then use$cookies.get("cookie");...you need to post exactly how your client-side looks like and where you're using the$cookies.get("cookie");so it will be easier to help you$cookies.get("cookie");in various places, though I suppose i should be watching it. But nothing seems to workconsole.logon the controller to print$cookies.get("cookie");make the request and then refresh the page? probably you'll see the value. I'm afraid you cannot access the value the way you're trying