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.

March 2, 2012

How to get OS default encoding?

Question by alex347

What is the proper way of getting default OS encoding? For Linux it can be found here: /etc/sysconfig/i18n

If you think the best way is to read from that file, then can I rely it will work on all modern major Linux distributions? What about Windows?

Answer by Starx

The best way to detect encoding, is from the piece of text you are trying to read from.

Use mb_detect_encoding()[docs here] function

$str = "....."; //use you own logic to get the text
echo mb_detect_encoding($str);

Adding on to @Evert

Encoding happens when characters are displayed on the screen or CLI interface. It is not OS dependent, rather content specific.

February 29, 2012

Can't include file on remote server

Question by user918712

My problem is that I can’t include a file on a remote server.

<?php
  echo "Includingn";
  require_once("http://xx.xxx.xxx.xx:8080/path/to/myfile.inc");
  echo "Done..n";
?>

The script fails at the require_once function.
I’m running the script with: php -d allow_url_include=On script.php
but to make sure I have set allow_url_include and allow_url_fopen to On in php.ini

If I copy http://xx.xxx.xxx.xx:8080/path/to/myfile.inc to the browser I’m served the file.
I have also tried to include other remote files (on standard port 80), but still no luck

Where I get really confused is that everything works from my local computers at my office (mac, ubuntu), but not from our servers. I have tested it on 2 different servers, a virtual and a dedicated.
I can get the file with fopen().

Answer by Starx

This can be done by setting allow_url_include to on on php.ini.

But, as mentioned in comments, this opens a

huge

security hole on your application.

...

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