March 16, 2013

Fatal error: Allowed memory size, when using ip2long function

Question by user1941709

I am using the ip2Long function below,

function ip_range($start, $end) {
    $start = ip2long($start);
    $end = ip2long($end);
    return array_map('long2ip', range($start, $end) );
}

$range_one = "86.188.249.48 ";
$range_two = "86.188.249.55";
print_r( ip_range($range_one, $range_two) );

But I get the following error:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to
allocate 32 bytes) in /home/site/public_html/path/checkrange.php on
line 6

Answer by Samuel Cook

$range_one is not considered a valid IP. If you remove the space off the end then this works for me:

$range_one = "86.188.249.48";

To avoid this in the future, you should trim your values:

$start = ip2long(trim($start));
$end = ip2long(trim($end));

Answer by Starx

The is a space at the end of the variable.

$range_one = "86.188.249.48 ";
                        // ^ Remove this

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!