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
May 6, 2012

Changing ip address of apache server using php

Question by apurv nerlekar

I am trying to create a settings page(for the clients) where in they can view the current up address,change the ip address etc. I have a php file to view the ip address

 <?php
  $res=shell_exec("ifconfig");
  echo $res;
 ?>

This code works just fine and displays the expected result.
However the code to change the ip address of the server is not working properly.

 <?php
 shell_exec("ifconfig eth0 192.168.163.136");
 ?>

After running this code when i check the ipaddress on the terminal using ipaddr i don’t see any change in the ipaddress.
Pls point out where i am going wrong. I think its a problem of apache not being a super/root user. If that is the case i don’t know how to make apache run as a root user.

Answer by Starx

IP address are configured in Network Layer of an Network Protocol, not in application layer where PHP runs. Simply, PHP does not have access to it and cannot changed them.

Just imagine the vulnerabilities it could create if this was possible.

May 11, 2011

Can't put IP-address in database

Question by Bjorn Seigers

I’m trying to put an ip-address in my database.
If I do an echo like this:

echo $_SERVER['REMOTE_ADDR'];

But if I’m trying to put it in a variable or in the database it gives nothing, so in my database it says: NULL
The commands I used for that:

$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("UPDATE users SET last_ip='".$ip."' WHERE id=".$row['id']) or die(mysql_error());

I don’t know what I’m doing wrong.
Can someone help me with this please?

Thanks!

Answer by Starx

Your code is correct and should work. Unless as commented by @wallyk, the data type of the ip field is unsupported one.

However, just to make sure wrap the WHERE condition in ' (Single Quote) and try.

...

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