The point of this script, is to take the user input (a website) and turn it into it's IP address. This then is turned into a file, readable later in the script. This is my attempt at having these sites available theoretically forever. A collection of what everyone has searched for. I have no idea which part is wrong seeing as I'm fairly new to PHP, but any help would be awesome!

<?php
    //Old PHP code that worked but I'm doing something slightly different this time.
    //if (isset($_POST['submit'])) {
        //if(isset($_POST['site'])) {
            //$site = $_POST['site'];
            //$ip = "http://".gethostbyname($site);
            //print "<a href=".$ip.">".$ip."</a>" or die ($ip);
        //}
    //}
    $ip = $_POST['site'];
    $myArray = array();
    if (max($myArray) == 0) {
       $newFile = $ip.".txt";
       $myArray = array(0 => $newFile);
       $openWrite = fopen($newFile, 'w') or die("Can't open file");
       fclose($openWrite);
       print "<a href='http://".$myArray[0]."'>".$myArray[0]."</a>";
    }
    elseif (max($myArray) > 0) {
        if ($ip != $newFile) {
            $newFile = $ip.".txt";
            $max = max($myArray) + 1;
            $myArray = array($max => $newFile);
            $maxArray = max($myArray);
            $openWrite = fopen($myArray[$maxArray], 'w') or die("Can't open file");
            fclose($openWrite);
        }
        else {
            print "<script type='text/javascript'>alert('Entered site already exists.')</script>";
            foreach ($myArray as $value) {
                print "<a href='http://".$value."'>".$value."</a>";
                print "<a onClick='".delete()."'> - Delete</a>";
                print "<br />";
                function delete() {
                    //Haven't written this part yet.
                }
            }
        }
    }
?>

Hmm, without knowing what the problem is that you want to correct it's not easy to help! :D

However I can see a few problems:

$newFile is not initialized before its use on line 20
gethostbyname can return some inappropriate results these days on some networks its better to query the DNS servers direct using dns_get_record.

Also use echo rather than print ;)
You can also output data in PHP like this:

echo "<a href='http://$value'>$value</a>";
Member Avatar
diafol

I'm really at a loss as to why you're doing this. Are you unable to use a database?

I'd prefer not to, I have had no luck with learning sql. I've tried many times and cannot figure it out, get a handle on it.

Member Avatar
diafol

Seriously mate, this ain't the answer. Good luck though.