Gethostbyaddr


#1

I piggy back on another domain and when they switched to Dreamhost, they had to change how my website was handled. It went from www.xxx.com/mywebsite to mywebsite.xxx.com. As a result of this the gethostbyaddr command in one of my web pages generates the following error:

Warning: gethostbyaddr() [function.gethostbyaddr]: Address is not a valid IPv4 or IPv6 address

Does anybody have any suggestions as to a solution.

Thanks.


#2

I suspect the IP address has changed?

–rlparker


#3

The short answer is yes, the IP address has changed.

The long answer is: I am a newbie at this, so please excuse my ingnorance. I did not write the code for this website nor am I familar with PHP, so I am not sure what the gethostbyaddr command is exactly for. However, I assumed the function was used so the website would work if the IP address changed. The website has been moved before and it continued to work fine.


#4

That’s no problem, and there is no reason you need to be excused; we are all learning stuff here everyday! :slight_smile:

I don’t know what the author of the program was using the function for, so it is hard to guess exactly what he is doing with the value returned by the function without seeing the code.

It is possible that the problem you are having is the result of DreamHost running PHP-CGI (certain environment variables return different values that the author might have anticipated), and possible a simply code tweak could solve the problem.

Can you provide an excerpt of the relevant code surrounding the use of the function?

–rlparker


#5

Thanks so much for taking the time to look at this.

The purpose of the code is to update another web page dynamically.

If you want to see it in action, go to stthaddaeus.etdiocese.net/news.php and click on the fleur-de-lis at the bottom of the page.

Here is the code (The error occurs in line 35 (the echo command) and keeps the code from executing, but the gethostbyaddr function is used several times):

<?php if((!$_POST || !$_POST['pass'] || strcmp($_POST['pass'], "snick") !== 0) && (!$_POST['items']) || (!strstr($HTTP_SERVER_VARS["HTTP_REFERER"], "update.php"))) $count = 1; else if($_POST && $_POST['pass'] && strcmp($_POST['pass'], "snick") !== 0) $count = 1; else $count = 0; include('header.php'); ?> <?php

if($_POST && $_POST[‘pass’] && strcmp($_POST[‘pass’], “snick”) !== 0) {
$filename = “log.txt”;
$handle = fopen($filename, “r+”);
rewind($handle);
$logged = date(“M d Y, H:i O”, strtotime(“now”)) . " — " .
gethostbyaddr($HTTP_SERVER_VARS[“REMOTE_ADDR”]) . " — " .
""" . $_POST[‘pass’] . “”\n";
$oldfile = fread($handle, filesize($filename));
ftruncate($handle, 0);
rewind($handle);
fwrite($handle, $logged);
fwrite($handle, $oldfile);
fclose($handle);
}

echo date(“M d Y, H:i O”, strtotime(“now”)) . “
\n”;

echo gethostbyaddr($HTTP_SERVER_VARS[“REMOTE_ADDR”]) . "

\n";

if((!$_POST || !$_POST[‘pass’] || strcmp($_POST[‘pass’], “snick”) !== 0) &&
(!$_POST[‘items’]) ||
(!strstr($HTTP_SERVER_VARS[“HTTP_REFERER”], “update.php”))) {
echo “\n”;
echo " \n";
echo “\n”;
echo "

A password is required to access this page. All access attempts are logged.\n";
$fleur = 0;
}
else {
echo "

The current pageload count is: ";
echo “”;
echo “\n”;
echo "
Statistics on the last 100 hits can be seen ";
echo "here.

";

echo "From this page, you can update the newsletter listings using the form below. Or, you can edit the online calendar.

\n";

echo “Format
\n”;
echo “(filename : link text)
\n”;
echo "e.g., (documents/temp.pdf : Week of January 1, 2005)

\n";

echo “\n”;

$filename = “data.txt”;
$handle = fopen($filename, “r+”);
rewind($handle);

if($_POST && $_POST[‘items’]) {
ftruncate($handle, 0);
rewind($handle);
fwrite($handle, $_POST[‘items’]);
$_POST[‘items’] = NULL;
$_POST = NULL;
}

rewind($handle);
echo stripslashes(fread($handle, filesize($filename)));

echo “
\n”;

echo “\n”;
echo “<input type=‘button’ value='View password error log’
onClick=” window.open(‘log.php?pass=acceptable’,‘winwin’,
‘toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizeable=0,width=750,height=250’)">\n";

echo “<input type=‘button’ value=‘Log out’ onClick=“window.location=‘news.php’”>\n”;

echo "

";

rewind($handle);

$buf = stripslashes(fread($handle, filesize($filename)));
$buf = str_replace("(", “

  • ”, $buf);
    $buf = str_replace(")", “
  • ”, $buf);

    echo $buf;

    }
    ?>

    <?php $blueimg = "blueflowerd.jpg"; include('footer.php'); ?>

    #6

    No Problem! I think I see the problem - it appears that the code expects to be running mod_php, as it uses HTTP_SERVER_VARS in several places (including the gethostbyaddr function) and on DreamHost you are running PHP-CGI. The short version is that some of these variables doe not exist when running PHP-CGI (the apache environment variables are just not available).

    You can see this by inspecting the phpinfo() outputs of the mod_php and the PHP-CGI examples available at http://php.dreamhosters.com.

    There are two ways to approach this situation to resolve the problem with your script:

      • You can modify the script to replace the variable(s) used as parameter(s) in the affected function with similar variables that are available in the PHP environment (you can use the phpinfo() outputs for a guide). While this may be a preferable way to approach the problem, it does require a certain amount of programming understanding and knowledge of PHP, and there are likely to be many such changes that need to be made across numerous files to get this to work.
      • You can try to run the program on DreamHost under mod_php. While it is no longer “officially” supported, there are so many such “older” applications around that DH still has mod_php available on their servers (at least on all of their servers I have encountered).

    This is actually pretty easy to do, as it really only involves the addition of an .htaccess file with the appropriate directives in it to cause PHP to be run as mod_php…

    Instructions on how to do this are available on the php.dreamhosters.com site, in the article on “End of PHP as an Apache Module”. Be sure to read through all the comments at the end of the article, as that is where the instructions regarding .htaccess are included (as well as some other useful links).

    Give that process a try, and you should have your scripts running mod_php and the problem you posted about (as well as others that exist in your code that have not yet presented themselves!) should be solved. :wink:

    All that said, if you have problems getting that set up or working properly, post back and I’m sure I, and others, will try to help you further. :slight_smile:

    –rlparker


    #7

    Thanks again for your kind help. It’s getting a little late here on the East Coast, so I am going to try your suggestion tomorrow.


    #8

    You’re welcome, and I hope that the mod_php setting puts everything straight for you. If not, post back, and we’ll “attack” the actual code on your page. :wink:

    –rlparker