1

I am currently moving a website over for a client. Their old host must be using an old version of PHP from what I can gather. The site is currently working on their current host. I am working to move this to a new host for them and I am getting the following error:

PHP Strict Standards: Only variables should be passed by reference in /home/parcelt2/core/public_html/loader.php on line 17

Below is a section of code with line 17 marked out

$uri = parse_url($_SERVER['REQUEST_URI']);
if (substr($uri['path'], -1, 1) == '/' && !sizeof($_POST)) {
$new_uri = substr($uri['path'], 0, -1);
if (strlen($new_uri) > 0) {
    $page = reset(explode('/', $new_uri));  //line 17
    if (!in_array($page, $exempt_requests)) {
        if (isset($uri['query']) && strlen($uri['query']) > 0) {
            $new_uri .= '?' . $uri['query'];
        }
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: ' . $new_uri, true, 301);
        exit;
    }
}
}

Would anyone be able to provide a fix or some suggestion on how to fix this? I have tried reading other SO posts about this error but have found them hard to understand.

Thank you

user2864740
  • 60,010
  • 15
  • 145
  • 220
BHWD
  • 123
  • 1
  • 4
  • 13
  • Ok, you're getting an error. What do you want us to do about it? – esqew Jun 26 '14 at 19:34
  • I wonder what `reset` is doing here. There's a brand new array created by `explode`, which has its pointer in a prime position - pointing to the very first element, that is. – raina77ow Jun 26 '14 at 19:35
  • You need to store the result of explode call in a variable then pass the variable to the reset function – Toby Allen Jun 26 '14 at 19:35
  • I do not know what reset is doing here. I am not the developer of the code. I simply am trying to move it from an old host and this is what is showing in an error log. Thank you – BHWD Jun 26 '14 at 19:38
  • 1
    Remember to search for the error message. This is not a new issue. – user2864740 Jun 26 '14 at 19:39
  • @BHWD But you still have to change the code to get rid of this error, don't you think? ) I'd rather have `reset` call removed than introduce another variable just for it to go without an error. – raina77ow Jun 26 '14 at 19:40

1 Answers1

5

reset() takes a reference to an array (variable) so it will not work with the result of a function call.

You need to do something like this:

$arr = explode('/', $new_uri);
$page = reset($arr);

However, the array returned by explode() will already have its internal pointer set to the first element. You shouldn't need to call reset at all.

ಠ_ಠ
  • 3,060
  • 28
  • 43
  • 1
    But what's `reset` for here? – raina77ow Jun 26 '14 at 19:37
  • @raina77ow heh...good point. I'll update the answer. – ಠ_ಠ Jun 26 '14 at 19:39
  • I do not know what reset is doing here. I am not the developer of the code. I simply am trying to move it from an old host and this is what is showing in an error log. Thank you – BHWD Jun 26 '14 at 19:39
  • Would you be able to show me how that code would fit into what I have posted above? Sorry to be a pain. – BHWD Jun 26 '14 at 19:40
  • @BHWD your error was caused by using `reset()` on the result of a function, not a variable reference. However, you don't need to use reset at all. Just remove it on your line 17. – ಠ_ಠ Jun 26 '14 at 19:42
  • I have removed reset so the code looks like '$page = (explode('/', $new_uri));' however I still get the same error in the error logs. – BHWD Jun 26 '14 at 19:45
  • Well, you don't need the extra parentheses there at all, but it will run all the same. – ಠ_ಠ Jun 26 '14 at 19:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56374/discussion-between-bhwd-and--). – BHWD Jun 26 '14 at 19:51
  • Reset() Returns the value of the first array element, or FALSE if the array is empty – GDmac Apr 29 '19 at 08:12