Forums / General Questions / PHP Warning: strstr(): Empty needle in

PHP Warning: strstr(): Empty needle in

Results 1 to 19 of 19
31 Jan 2015, 07:55
#1
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Posts:
3,427
Plugin Contributions:
2

PHP Warning: strstr(): Empty needle in

Just checked the logs of a client site - ZC 1.5.4 - and found 32 identical (...except for the time) entries in the logs folder - all in the space of about 2 minutes:

[28-Jan-2015 19:56:38 UTC] PHP Warning: strstr(): Empty needle in /home/[account_name]/public_html/includes/functions/functions_lookups.php on line 712

That line reads

[PHP]case (strstr(EXCLUDE_ADMIN_IP_FOR_MAINTENANCE, $_SERVER['REMOTE_ADDR'])): [/PHP]

Checked the admin logs for that date - nothing extraordinary.... site was not down for maintenance.

Never had this error previously. Could anyone shed a light on this please....

Cheers / Frank
31 Jan 2015, 08:58
#2
signtorch avatar

signtorch

New Zenner

Join Date:
Dec 2014
Posts:
32
Plugin Contributions:
0

Re: PHP Warning: strstr(): Empty needle in

$_SERVER['REMOTE_ADDR'] can be empty, which causes the error

I think you can suppress the error with @
case (@strstr(EXCLUDE_ADMIN_IP_FOR_MAINTENANCE, $_SERVER['REMOTE_ADDR'])):


or clarify the case condition
case (!empty($_SERVER['REMOTE_ADDR'])&&strstr(EXCLUDE_ADMIN_IP_FOR_MAINTENANCE, $_SERVER['REMOTE_ADDR'])):
31 Jan 2015, 09:02
#3
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Posts:
3,427
Plugin Contributions:
2

Re: PHP Warning: strstr(): Empty needle in

Server info:

[TABLE="width: 90%"]
[TR]
[TD]Server OS: Linux 2.6.18-408.8.2.el5.lve0.8.61.3 [/TD]
[TD="width: 51%"]Database: MySQL 5.5.40-cll[/TD]
[/TR]
[TR]
[TD]Server Date: 31/01/2015 19:00:33 [/TD]
[TD="width: 51%"]Database Date: 31/01/2015 09:00:33 [/TD]
[/TR]
[TR]
[TD]Server Up Time: 09:00:33 up 794 days, 18:40, 0 users, load average: 3.47, 2.38, 2.09[/TD]
[TD="width: 51%"]HTTP Server: Apache[/TD]
[/TR]
[TR]
[TD]PHP Version: 5.4.36 (Zend: 2.4.0) PHP Memory Limit: 128M

[/TD]
[/TR]
[/TABLE]
31 Jan 2015, 09:04
#4
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Posts:
3,427
Plugin Contributions:
2

Re: PHP Warning: strstr(): Empty needle in

SignTorch:

$_SERVER['REMOTE_ADDR'] can be empty, which causes the error

I think you can suppress the error with @
case (@strstr(EXCLUDE_ADMIN_IP_FOR_MAINTENANCE, $_SERVER['REMOTE_ADDR'])):


or clarify the case condition
case (!empty($_SERVER['REMOTE_ADDR'])&&strstr(EXCLUDE_ADMIN_IP_FOR_MAINTENANCE, $_SERVER['REMOTE_ADDR'])):


Thanks - but suppressing the error is not much good to me, I would like to know the reason for this error.
31 Jan 2015, 09:52
#5
design75 avatar

design75

Totally Zenned

Join Date:
Dec 2009
Posts:
2,862
Plugin Contributions:
5

Re: PHP Warning: strstr(): Empty needle in

frank18:

Thanks - but suppressing the error is not much good to me, I would like to know the reason for this error.


You probably have no value set for you admin ip under configuration->maintenance
31 Jan 2015, 09:56
#6
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Posts:
3,427
Plugin Contributions:
2

Re: PHP Warning: strstr(): Empty needle in

Design75:

You probably have no value set for you admin ip under configuration->maintenance


Yes, an IP is set, the field is not empty
31 Jan 2015, 10:14
#7
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Posts:
6,263
Plugin Contributions:
3

Re: PHP Warning: strstr(): Empty needle in

Design75:

You probably have no value set for you admin ip under configuration->maintenance


Nah, it wouldn't be that. The error message is "PHP Warning: strstr(): Empty needle", which means that it's the $_SERVER['REMOTE_ADDR'] variable being set.

Why it's not being set is something I can't answer, but as SignTorch suggested, suppressing the error is probably the easiest 'fix'.

Cheers
RodG
31 Jan 2015, 10:20
#8
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Posts:
6,263
Plugin Contributions:
3

Re: PHP Warning: strstr(): Empty needle in

Addendum:

That should have read:

[COLOR=#333333]It's the[/COLOR][COLOR=#333333]$_SERVER['REMOTE_ADDR'] [/COLOR]variable NOT being set.

Cheers
RodG

ps. Does anyone else experience lockups when trying to edit a post or is it just me? Doesn't happen all the time, but often enough that its become a real headache. :(
31 Jan 2015, 10:33
#9
design75 avatar

design75

Totally Zenned

Join Date:
Dec 2009
Posts:
2,862
Plugin Contributions:
5

Re: PHP Warning: strstr(): Empty needle in

RodG:

Nah, it wouldn't be that. The error message is "PHP Warning: strstr(): Empty needle", which means that it's the $_SERVER['REMOTE_ADDR'] variable being set.


Duh :blush:, I guess i needed more coffee this morning
31 Jan 2015, 10:40
#10
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Posts:
3,427
Plugin Contributions:
2

Re: PHP Warning: strstr(): Empty needle in

RodG:

Why it's not being set is something I can't answer, but as SignTorch suggested, suppressing the error is probably the easiest 'fix'.

Cheers
RodG


Yeah, I guess so - BUT the question still remains: why is the error happening, is it a bug in the coding -- or what??
31 Jan 2015, 11:55
#12
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,506
Plugin Contributions:
173

Re: PHP Warning: strstr(): Empty needle in

Are you able to trigger it on-demand? That would be helpful for proper resolution.

Could possibly be caused by IP address spoofing, or malicious manipulation by the visitor to mask their address such that the $_SERVER array doesn't have a value there, or perhaps crafty DNS registration which reports invalid data.
Or a weird proxy server configuraiton if you're using one.
31 Jan 2015, 12:15
#13
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Posts:
3,427
Plugin Contributions:
2

Re: PHP Warning: strstr(): Empty needle in

DrByte:

Are you able to trigger it on-demand? That would be helpful for proper resolution.

Could possibly be caused by IP address spoofing, or malicious manipulation by the visitor to mask their address such that the $_SERVER array doesn't have a value there, or perhaps crafty DNS registration which reports invalid data.
Or a weird proxy server configuraiton if you're using one.


Thanks

Can't trigger it on demand but found the corresponding server log entry for this error:


[28-Jan-2015 19:55:58 UTC] PHP Warning: strstr(): Empty needle in /home/sunnsand/public_html/includes/functions/functions_lookups.php on line 712


Log entry (adusted for server/UTC time difference):


78.129.250.17 - - [29/Jan/2015:06:55:58 +1100] "GET /index.php?main_page=index&cPath=13_15_23 HTTP/1.1" 200 11389 "-" "Mozilla/5.0 (compatible; aiHitBot/2.9; +http://www.aihitdata.com/about)"


As far as I know my server is not using a weird proxy server configuration.


I am frequently checking the logs of all (virtual) servers and have never seen this particular error before with any of the hosted ZC sites - this is a first in 5 years with the same host.


And - all sites are residing on the same physical server.
31 Jan 2015, 15:30
#14
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Posts:
6,263
Plugin Contributions:
3

Re: PHP Warning: strstr(): Empty needle in

frank18:


As far as I know my server is not using a weird proxy server configuration.


Perhaps not, but the issue could arise if the client is using a proxy and it is their proxy that is mangling things.

frank18:


I am frequently checking the logs of all (virtual) servers and have never seen this particular error before with any of the hosted ZC sites - this is a first in 5 years with the same host.


I have seen it before, but not enough for me to consider it worth worrying about.

I've just been doing a bit of Googling though and found that others have overcome the problem by adding a function to retrieve the IP address rather than simply relying on the $_SERVER['REMOTE_ADDR'] variable.

https://gist.github.com/cballou/2201933

It shouldn't be difficult to incorporate into ZenCart if you are really that concerned about it.

FYI, the $_SERVER['REMOTE_ADDR'] variable has never been 100% reliable, because even if the client is coming in from a correctly configured proxy the IP address returned will be for the Proxy server rather than the end user (To get the end users IP address in this case you'd need to use the $_SERVER['HTTP_X_FORWARDED_FOR'] variable).
If the proxy has been configured to use invalid data for its own IP address there isn't much you can do about it other than to retrieve the data from elsewhere (as per the function in the link above).

Cheers
RodG
31 Jan 2015, 17:34
#15
signtorch avatar

signtorch

New Zenner

Join Date:
Dec 2014
Posts:
32
Plugin Contributions:
0

Re: PHP Warning: strstr(): Empty needle in

frank18:

Thanks - but suppressing the error is not much good to me, I would like to know the reason for this error.


the reason is because the variable $_SERVER['REMOTE_ADDR'] can be empty, when it is empty the function strstr() throws the error, because it can't have an empty needle parameter

my 2nd suggestion should prevent the error because it doesn't run strstr() when $_SERVER['REMOTE_ADDR'] is empty

you could try additional ways to get the visitor's ip address, but nothing is 100% bullet proof, and in this case only administrator IP addresses are relevant, when the store is in maintenance mode, it is not necessary to resolve the IP address of all visitors all the time

zen cart does have a function to check several other environment variables for an IP address

admin/includes/functions/general.php

/**
 * Get and sanitize the current IP address, detecting past proxies where possible
 * @return IP address
 */
function zen_get_ip_address() {
  if (isset($_SERVER)) {
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
      $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
      $ip = $_SERVER['HTTP_X_FORWARDED'];
    } elseif (isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) {
      $ip = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
      $ip = $_SERVER['HTTP_FORWARDED_FOR'];
    } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
      $ip = $_SERVER['HTTP_FORWARDED'];
    } else {
      $ip = $_SERVER['REMOTE_ADDR'];
    }
  } else {
    if (getenv('HTTP_X_FORWARDED_FOR')) {
      $ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif (getenv('HTTP_CLIENT_IP')) {
      $ip = getenv('HTTP_CLIENT_IP');
    } else {
      $ip = getenv('REMOTE_ADDR');
    }
  }

  return $ip;
}


to prevent the error you would still have to avoid running strstr() on an empty string
31 Jan 2015, 17:43
#16
signtorch avatar

signtorch

New Zenner

Join Date:
Dec 2014
Posts:
32
Plugin Contributions:
0

Re: PHP Warning: strstr(): Empty needle in

we've been referring to it as an error, it is actually a warning, suppressing the warning with @ should be viable
31 Jan 2015, 22:33
#18
signtorch avatar

signtorch

New Zenner

Join Date:
Dec 2014
Posts:
32
Plugin Contributions:
0

Re: PHP Warning: strstr(): Empty needle in

That's good, FWIW that is what I suggested in post #2
01 Feb 2015, 00:05
#19
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Posts:
3,427
Plugin Contributions:
2

Re: PHP Warning: strstr(): Empty needle in

SignTorch:

we've been referring to it as an error, it is actually a warning, suppressing the warning with @ should be viable



Point taken, thanks


RixStix:

FWIW, I have not seen the issue since using lat9's suggestion.



Yep, implemented that suggestion on all sites


Thanks to all of you guys for your input and suggestions, much appreciated.


Cheers / Frank