RE: [PHP] IP of server?
| From: | Lance Lovette | Date: | Wed, 28 Mar 2001 22:27:06 +0000 |
| Subject: | RE: [PHP] IP of server? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-45952@lists.php.net to get a copy of this message | ||
Thanks, but most variables such as SERVER_ADDR and SERVER_NAME are not set
when you run a PHP script from the command line (or from cron). Write a
script that calls phpinfo() and then use 'php -f' to run it and you'll see
how little environment information you get.
Lance
-----Original Message-----
From: wx [mailto:wx@wxcs.com]
Sent: Wednesday, March 28, 2001 5:09 PM
To: Lance Lovette
Subject: Re: [PHP] IP of server?
Why just not use the built in variables:
$SERVER_ADDR
$SERVER_NAME
not that tough, :)
- Rob
----- Original Message -----
From: "Lance Lovette" <lance-php@dailyrating.com>
To: <php-general@lists.php.net>
Sent: Wednesday, March 28, 2001 11:27 AM
Subject: [PHP] IP of server?
> I want to base the database a PHP script uses on the IP address of the
> server running the script. What is the fastest way to get the IP address
of
> the server from a script? I could use environment variables but they are
> unreliable (when run from cron for example, the environment doesn't have
all
> the variables you would expect). I wrote the following function based on a
> Perl firewall script that uses ifconfig, but it has too much overhead to
> call every time I connect to the database. All I really want to do is add
a
> conf variable to php.ini that gets read at startup and is cached from then
> on. Any ideas?
>
> Thanks!
> Lance
>
> ----
>
> // string gethostaddress([string])
> //
> // Returns the IP address of a network interface for the local host.
> // Returns false on error or if the address cannot be determined.
> //
> // Note: This function shells the ifconfig command so call sparingly.
> //
> function gethostaddress($interface = "eth0")
> {
> $inetaddr = false; // assume failure
>
> $ifconfig =
/sbin/ifconfig $interface;
> if (ereg("inet addr:([.0-9]+)", $ifconfig, $ifconfigparts))
> $inetaddr = $ifconfigparts[1];
>
> return ($inetaddr) ? $inetaddr : false;
> }
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net