0

I want to get the IP address of the product review and insert it into a database. I already have the 'review_ip_address' column in the 'review' table and try to modify Magento_review/Controller/Product/Post.php from

$review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
->setEntityPkValue($product->getId())
->setStatusId(Review::STATUS_PENDING)
->setCustomerId($this->customerSession->getCustomerId())
->setStoreId($this->storeManager->getStore()->getId())
->setStores([$this->storeManager->getStore()->getId()])     
->save();

to

$review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
->setEntityPkValue($product->getId())
->setStatusId(Review::STATUS_PENDING)
->setReviewIpAddress($_SERVER['REMOTE_ADDR'])
->setCustomerId($this->customerSession->getCustomerId())
->setStoreId($this->storeManager->getStore()->getId())
->setStores([$this->storeManager->getStore()->getId()])     
->save();

by adding ->setReviewIpAddress($_SERVER['REMOTE_ADDR']) but it does not seem to work.

2 Answers 2

1

Please try this

$ipAddress = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];

Hopefully, this works for you

Note: In localhost/local machine you will get 127.xx.xx.xx not IP you are expecting

Note2: Please validate or sanitize the IP before saving it

Thanks

3
  • It's work for me thank you. Dec 13, 2021 at 6:38
  • Edit:: $ipAddress = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']); due to Unparenthesized 'a ? b : c ? d : e' is deprecated. Use either '(a ? b : c) ? d : e' or 'a ? b : (c ? d : e)' Dec 14, 2021 at 9:05
  • $ipAddress = (isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'])); Dec 14, 2021 at 12:09
0
$objctManager = \Magento\Framework\App\ObjectManager::getInstance();

$remote = 
$objctManager->get('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress');

echo $remote->getRemoteAddress();
1
  • directly using object manager it's not good practice @surbhi Doshi
    – Devidas
    Dec 15, 2021 at 11:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.