Page 1 of 1

Need Help with Undefined Variable error messages :(

Posted: Mon Jul 18, 2005 7:18 pm
by jjfletch
I'm using the script (posted below) and am receiving the following errors:

An error occurred in script /home/virtual/site64/fst/var/www/files/config.inc on line 7: Undefined variable: des_salt

An error occurred in script /home/virtual/site64/fst/var/www/files/config.inc on line 7: fputs(): supplied argument is not a valid stream resourceCould not write new passwd file

An error occurred in script /home/virtual/site64/fst/var/www/files/config.inc on line 7: fclose(): supplied argument is not a valid stream resource


I've tried putting "isset" in various places, but I still get error messages. Can anyone help me out?

Code: Select all

<?php
 
$AllowAddNewUser = "TRUE";
$htpasswd = "/path/.htpasswd";
 
if (isset($_POST['submit']))
 
        {
        function generateString($user,$pass)
        {
 
                $saltchars = array(
 
                   'a','b','c','d','e','f','g','h','i','j','k','l','m',
                   'n','o','p','q','r','s','t','u','v','w','x','y','z',
                   'A','B','C','D','E','F','G','H','I','J','K','L','M',
                   'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                   '1','2','3','4','5','6','7','8','9','0','.','/'
                );
                srand((double)microtime()*1000000);
                $saltcount = "2";
 
                for ($i=0; $i<$saltcount; $i++)
                {
                    $des_salt .= $saltchars[rand(0,count($saltchars))];
                }
 
                $encrypted = crypt($pass,$des_salt);
                $return = $user . ":" . $encrypted . "\n";
                return $return;
                }
 
        if ($_POST['p'] != $_POST['conf_p'])
        {
            echo "Passwords do not match. Killing script...<br /><br />";
            die;
        }
 
        $Fp = fopen($htpasswd, "r");
        $Contents = fread($Fp, filesize($htpasswd));
        fclose($Fp);
        $String = explode("\n", $Contents);
 
        for ($i=0; $i<count($String); $i++)
 
        {
            $chkString = explode(":", $String[$i]);
            if($chkString[0] == $_POST['u'])
            {
                $Str[$i] = $String[$i] . "\n";
                $newContents = str_replace($Str[$i], "", $Contents);
            }
        }
 
        $InsContents = $newContents;
        if (!$newContents)
        {
            if (isset($_POST['AddNew']))
            {
                echo "Could not locate specified user so creating new account<br /><br />";
                $InsContents = $Contents;
            } else
 
            {
                echo "User could not be found. Killing script...<br /><br />";
                die;
            }
        }
 
 
        $insString = generateString($_POST['u'], $_POST['p']); // Generate the user:pass string
        $newHtpasswd = $InsContents . $insString; // Set the new contents of the file
        $Handle = fopen($htpasswd, "w");
        if (!fputs($Handle, $newHtpasswd))
        {
            echo "Could not write new passwd file";
        } else
        {
            echo "New passwd file written successfully";
        }
        fclose($Handle);
        } else
        {
            echo "<form name=\"changePass\" action=\"$PHP_SELF\" method=\"POST\">
            <table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" bgcolor=\"#DDDDDD\">
            <tr><td bgcolor=\"#FFFFFF\">Username:</td><td bgcolor=\"#FFFFFF\"><input type=\"text\" name=\"u\" /></td></tr>
            <tr><td bgcolor=\"#FFFFFF\">New Password:</td><td bgcolor=\"#FFFFFF\"><input type=\"password\" name=\"p\" /></td
></tr>
            <tr><td bgcolor=\"#FFFFFF\">Confirm New<br />Password:</td><td bgcolor=\"#FFFFFF\"><input type=\"password\" name
=\"conf_p\" /></td></tr>";
 
            if ($AllowAddNewUser == "TRUE") // If the variable is TRUE then show the checkbox
            {
                echo "<tr><td bgcolor=\"#FFFFFF\">Create new user<br />if this user does<br />not exist?</td><td bgcolor=\"#
FFFFFF\"><input type=\"checkbox\" name=\"AddNew\"></td></tr>";
            }
            echo "<tr><td bgcolor=\"#FFFFFF\" colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"
Change Password\"></td></tr>
    </table>
    </form>";
        }
?>
Thanks!

Re: Need Help with Undefined Variable error messages :(

Posted: Mon Jul 18, 2005 8:03 pm
by harrisonad
Undefined variable: des_salt
initialize variables first before use, especially when concatenating to itself, '.='.

Code: Select all

$des_salt = ''; // <-- here, or anywhere before the loop
for ($i=0; $i<$saltcount; $i++)
{
    $des_salt .= $saltchars[rand(0,count($saltchars))];
}

Posted: Tue Jul 19, 2005 3:20 am
by shiznatix
when checking if somthing isset then you should do

Code: Select all

if (false !== isset($_POST['var']))
that will get you away from the undefined variable stuff whatnot