Reputation: 910
I have the error Only variables should be passed by reference
on my APIController.php
, I already read all the questions about this error but nothing fixed it.
My code :
$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)
Thank you.
Upvotes: 1
Views: 1144
Reputation: 13699
Try to save the end element of array into a variable then pass it into the method like this:
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = end($arr); // <------- This will give you the IP (no need of using array_pop)
'ErrorException' Explanation: The problem is, that
end()
requires a reference, because it modifies the internal representation of the array (i.e. it makes the current element pointer point to the last element).
Hope this helps!
Upvotes: 2