PHP gethostbyname() doesn't work in Linux with xampp

Problems with the Linux version of XAMPP, questions, comments, and anything related.

PHP gethostbyname() doesn't work in Linux with xampp

Postby smidh » 15. June 2014 19:27

0 down vote favorite


Hi I want to obtain my ip number in php, I'm using ubuntu and apache XAMPP for linux as webserver.

Code: Select all
<?php

    echo $gethostname = gethostbyname(gethostname());

?>


When I execute the code it returns 127.0.0.1 What I want is like i.e 10.151.32.123. This code works in windows with xampp, why this code doesn't work in Linux?
smidh
 
Posts: 1
Joined: 15. June 2014 19:20
Operating System: Ubuntu

Re: PHP gethostbyname() doesn't work in Linux with xampp

Postby Nobbie » 16. June 2014 10:53

It has nothing to do with Linux or Windows. It is determined by your server configuration (where Apache does listen) and how and from where you access the Sever.

Anyway, you may also simply evaluate the server variable "SERVER_ADDR" to find out the Server IP:

Code: Select all
<?php
    echo $_SERVER["SERVER_ADDR"];
?>


And if you want to echo the IP of the client, use "REMOTE_ADDR" instead:

Code: Select all
<?php
    echo $_SERVER["REMOTE_ADDR"];
?>
Nobbie
 
Posts: 8769
Joined: 09. March 2008 13:04


Return to XAMPP for Linux

Who is online

Users browsing this forum: No registered users and 7 guests