0

I am trying to get the domain from the request in nodejs, the request originates from PHP and when I get origin in Nodejs I get undefined.

This is my code in PHP: (domain1.com)

    $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://myNodedomain.com/sendAlert',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HEADER => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('token' => '3242342332'),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

This is my code in NodeJs: (mynodedomain.com)

app.post('/sendAlert', async (req, res) => {
   console.log(req.get('host'));
   console.log(req.get('origin'));
});

The desired result is: https://domain1.com/

Any solution?

3
  • 1
    If the request is coming from a PHP script, there is no way to reliably associate a domain with that. That's just some random client somewhere at some IP address. That client could tell you what domain it was at by setting a referrer header on the request or you could try to do a reverse DNS lookup on its IP address and see if that IP address is associated with a particular domain, but neither of those is guaranteed to work and cannot be relied upon unless the requesting host is cooperating with you and can be trusted. Commented Jun 3, 2022 at 0:48
  • Your PHP code is not sending the origin header. Therefore it is not possible to get the origin domain due to how TCP/IP and HTTP works Commented Jun 3, 2022 at 3:01
  • Thanks for your answers, I'll implement it in another way instead of origin. Commented Jun 3, 2022 at 22:31

1 Answer 1

2

I'm not super familiar with PHP, but adding something like this might work:

curl_setopt($curl, CURLOPT_HTTPHEADER, ['Origin: domain1.com']);
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.