gethostbyaddr

gethostbyaddr

Posted by: Pensive
Posted on: 2007-09-01 20:26:00

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.

Re: gethostbyaddr

Posted by: rlparker
Posted on: 2007-09-01 21:21:00

I suspect the IP address has changed?

--rlparker

Re: gethostbyaddr

Posted by: Pensive
Posted on: 2007-09-01 21:35:00

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.

Re: gethostbyaddr

Posted by: rlparker
Posted on: 2007-09-01 21:45:00

That's no problem, and there is no reason you need to be excused; we are *all* learning stuff here everyday! 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

Re: gethostbyaddr

Posted by: Pensive
Posted on: 2007-09-01 22:08:00

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');

?>

<form name="form1" method="post" action="update.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 "<input type='password' size='20' name='pass'>n";
echo " <input type='submit' value='Log in'>n";
echo "<script language='JavaScript'>document.form1.pass.focus();</script>n";
echo "

A password is required to access this page. All access attempts are logged.n";
$fleur = 0;
}
else {
echo "<div>The current pageload count is: ";
echo "<span style='position:relative; top:3px'>";
echo "n";
echo "</span></div> Statistics on the last 100 hits can be seen ";
echo "<a href='http://my.statcounter.com/project/standard/stats.php?project_id=362930&guest=1'>here</a>.


";

echo "From this page, you can update the newsletter listings using the form below. Or, you can <a href='calendar.php'>edit</a> the online calendar.

n";

echo "<b>Format</b>
n";
echo "<b>(</b>filename<b> : </b>link text<b>)</b>
n";
echo "<i>e.g.,</i> <b>(</b>documents/temp.pdf<b> : </b>Week of January 1, 2005<b>)</b>

n";

echo "<textarea name='items' rows='7' cols='46'>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 "</textarea>
n";

echo "<input type='submit' value='Update news'>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("(", "<li><a href='", $buf);
$buf = str_replace(":", "'>", $buf);
$buf = str_replace(")", "</a></li>", $buf);

echo $buf;

}
?>

</form>

<?php

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

?>


Re: gethostbyaddr

Posted by: rlparker
Posted on: 2007-09-01 22:35:00

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:

1) - 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.

2) - 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. smile

--rlparker

Re: gethostbyaddr

Posted by: Pensive
Posted on: 2007-09-01 23:29:00

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.

Re: gethostbyaddr

Posted by: rlparker
Posted on: 2007-09-02 00:24:00

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

Tags: mywebsiteipv6 addressdreamhostipv4web pagesmywebsiteipv6 addressdreamhostipv4web pages