0

How to get cookie in php send from Node.js application? I am not fimiliar to php. How to get this cookie creted in nodejs in php?

    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
    
        res.setHeader("Set-Cookie","type-test");
        res.setHeader("Year",new Date().getFullYear);
      res.send('Hello World!')
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Anuj Todankar
  • 168
  • 11

1 Answers1

0

Example -

<?php
$cookie_name = "type-test";

if(!isset($_COOKIE[$cookie_name])) {
  echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
  echo "Cookie '" . $cookie_name . "' is set!<br>";
  echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
Ammar Aslam
  • 560
  • 2
  • 16
  • Hello @AmmarAslam i have no idea about php i used your code and i got an error : Warning: Undefined variable $cookie_name in C:\xampp\htdocs\test\index.php on line 2 Warning: Undefined variable $cookie_name in C:\xampp\htdocs\test\index.php on line 3 Cookie named '' is not set! – Anuj Todankar Nov 17 '21 at 13:03
  • $cookie_name is a variable, that will store the name of the cookie, let me modify the example – Ammar Aslam Nov 17 '21 at 13:11
  • Your code removed err but i am not getting the value printed. This is Actual Result i am getting : Cookie 'type-test is set! Value is: – Anuj Todankar Nov 17 '21 at 13:24
  • res.setHeader("Set-Cookie","type-test=whatever_value"); – Ammar Aslam Nov 17 '21 at 13:29
  • Still Value is coming empty in Developers tool under Request Header : I am getting Cookie: type-test; type-test=whatever_value – Anuj Todankar Nov 17 '21 at 13:40
  • The problem is definitely in the cookie set in your node code, the cookie value you're setting in node probably is empty https://stackoverflow.com/questions/3393854/get-and-set-a-single-cookie-with-node-js-http-server – Ammar Aslam Nov 17 '21 at 13:44