我的Only variables should be passed by reference
上有错误APIController.php
,我已经阅读了有关此错误的所有问题,但没有修复它。
我的代码:
$ip = $_SERVER['REMOTE_ADDR'];
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
$ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = array_pop(end($ip));
}
Error.log:
[2016-12-06 15:43:00] production.ERROR: exception 'ErrorException' with message 'Only variables should be passed by reference' in /var/www/app/Http/Controllers/Api/ApiController.php:33
Stack trace:
#0 /var/www/app/Http/Controllers/Api/ApiController.php(33): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2048, 'Only variables ...', '/var/www/app/Ht...', 33, Array)
谢谢。
答案 0 :(得分:2)
尝试将数组的end元素保存到变量中,然后将其传递给方法,如下所示:
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = end($arr); // <------- This will give you the IP (no need of using array_pop)
'ErrorException'解释:问题是,
end()
需要引用,因为它修改了 数组的内部表示(即它产生电流 元素指针指向最后一个元素。)
希望这有帮助!