Commit 00e9b58a by halweg

fix : 修复获取真实ip bug

parent aeb545dc
<?php
namespace Joshine\Review\Block\Review\Product\View;
use Joshine\Review\Helper\BlockHelper;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\DataObjectFactory;
use Magento\Review\Model\ResourceModel\Review\Collection as ReviewCollection;
......@@ -31,6 +32,8 @@ class ListView extends \Magento\Review\Block\Product\View\ListView
private $vote;
private $realRemoteIp;
private $formKey;
/**
......@@ -203,7 +206,7 @@ class ListView extends \Magento\Review\Block\Product\View\ListView
{
return $this->voteRepository->getVotesCount(
$reviewId,
$this->remoteAddress->getRemoteAddress()
$this->getRemoteIp()
);
}
public function isPlusVoted($reviewId)
......@@ -213,4 +216,12 @@ class ListView extends \Magento\Review\Block\Product\View\ListView
return $voted['plus'] > 0;
}
public function getRemoteIp()
{
if (is_null($this->realRemoteIp)) {
$this->realRemoteIp = BlockHelper::getRealIp();
}
return $this->realRemoteIp;
}
}
......@@ -4,6 +4,7 @@ namespace Joshine\Review\Controller\Ajax;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Controller\Result\JsonFactory;
use Joshine\Review\Helper\BlockHelper;
class Helpful extends \Magento\Framework\App\Action\Action
{
......@@ -80,7 +81,7 @@ class Helpful extends \Magento\Framework\App\Action\Action
$type = $this->getRequest()->getParam('type');
$reviewId = (int)$this->getRequest()->getParam('review');
if ($reviewId > 0 && in_array($type, ['plus', 'minus', 'update'])) {
$ip = $this->remoteAddress->getRemoteAddress();
$ip = BlockHelper::getRealIp();
if ($type != 'update') {
$type = ($type == 'plus') ? '1' : '0';
......
......@@ -99,4 +99,23 @@ class BlockHelper implements \Magento\Framework\Data\CollectionDataSourceInterfa
return $default;
}
static function getRealIp()
{
if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ips = $_SERVER['HTTP_X_FORWARDED_FOR'];
}elseif(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ips = $_SERVER['HTTP_CLIENT_IP'];
}else{
$ips = $_SERVER['REMOTE_ADDR'];
}
if (strpos($ips, ',') !== false) {
$ipList = explode(',', $ips);
} else {
$ipList = [$ips];
}
$remoteAddress = trim(reset($ipList));
return $remoteAddress ?? '';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment