0

I want to know... If I am use php setcookie and use angular get the cookie. it is possibile? now I setcookie in php and can see the cookie be creacted in php. but I am use angular to get the cookie console.log will show null value. code:

PHP:
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json; charset=UTF-8");

$se='444';
setcookie("TestCookie",$se, time()+3600*24);
$aa=$_COOKIE["TestCookie"];
$value=array('cookie'=>$aa);
$rows=array();
$rows[]=$value;
print_r (json_encode($rows));

angular get cookie:

 getco()
  {
    this.dataService.getcookie(this.cat).subscribe(response =>
      {
        this.cook = response.map(item =>
        {
          return new cookies(
            item.cookie,
          );
        });
        console.log(this.cook);
      });
  }

     getcookie(id): Observable<cookies[] > {
        return this.httpClient.get<cookies[]>(this.baseUrl + '/setcookie.php');
      }

When I use console.log(this.cook)it will display null value.

enter image description here

IMSoP
  • 89,526
  • 13
  • 117
  • 169
jason
  • 47
  • 1
  • 5

1 Answers1

0

It might be a duplicate of : Cookie not setted or not working the first time

When you use setcookie(), you're setting a cookie, but the $_COOKIE array contains only existing cookies (it's created on the page load).

Try to:

setcookie("TestCookie",$se, time()+3600*24);
$_COOKIE["TestCookie"] = $se;
Lenny4
  • 1,215
  • 1
  • 14
  • 33