Jump to content
Larry Ullman's Book Forums

Chapter 11 - Register.Php


Recommended Posts

I'm having a problem with the register.php file in the "Creating Directories" section to the point where I can't continue on with the chapter. I'm using xampp on windows and have PHP 5.4.7 running and I'm pretty sure that my scripts are identical to the ones in the book and after testing out the scripts, I get the exact error that is displayed on pg. 325 example A for the result if the users.txt file is not writable.  It gives me the system error response every time and I'm wondering if I don't have the correct directory mapped or something, but I don't see anything after this in the book that specifically addresses what to do if you actually run into the problem that I'm having, which is the user.txt file not being writable.  Any suggestions?

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
<head>
    <meta http-equiv="content-type" content="text/html"; charset=utf-8/>
    <title>Register</title>
    <style type="text/css" media="screen">
        .error {
            color: red;
        }
 
    </style>
</head>
 
<body>
<h1>Register</h1>
<?php
// Script 11.6 - register.php
/* This script registers a user by storing their information in a text file and creating a directory for them. */
 
// Error Handling
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
 
// Identify the directory and file to use:
$dir = '../users/';
$file = $dir . 'users.txt';
 
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // Handle the form.
    $problem = FALSE; // No problems so far.
 
    // Check for each value...
    if (empty($_POST['username']))
    {
        $problem = TRUE;
        print '<p class="error">Please enter a username!</p>';
    }
 
    if (empty($_POST['password1']))
    {
        $problem = TRUE;
        print '<p class="error">Please enter a password!</p>';
    }
 
    if ($_POST['password1'] != $_POST['password2'])
    {
        $problem = TRUE;
        print '<p class="error">Your password did not match your confirmed password!</p>';
    }
 
    if (!$problem) // If there weren't any problems...
    {
        if (is_writable($file))
        {
            // Open the file.
            // Create the data to be written:
            $subdir = time() . rand(0, 4596);
            $data = $_POST['username'] . "\t" . md5(trim($_POST['password1'])) . "\t" . $subdir . PHP_EOL;
 
            // Write the data:
            file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
 
            // Create the directory:
            mkdir($dir . $subdir);
 
            // Print a message:
            print '<p>You are now registered!</p>';
 
        }
        else
        {
            // Couldn't write to the file
            print '<p class="error">You could not be registered due to a system error.</p>';
        }
    }
    else
    {
        // Forgot a field.
        print '<p class="error">Please go back and try again!</p>';
    }
 
}
else
{
  // Display the form.
 
  // Leave PHP and display the form.
 
?>
 
<form action="register.php" method="post">
    <p>Username: <input type="text" name="username" size="20" /></p>
    <p>Password: <input type="password" name="password1" size="20" /></p>
    <p>Confirm Password: <input type="password" name="password2" size="20" /></p>
    <input type="submit" name="submit" value="Register" />
</form>
 
<?php } // End of submission IF. ?>
 
</body>
 
</html>
Link to comment
Share on other sites

I dont have this book so if my response repeats whats in the book, apologies.

 

It sounds like you need to change the permissions of your file or directory. Look up the php chmod() function and see if that helps.

 

Margaux - I looked it up and am not really sure that is the issue or I'm not really understanding exactly what the php chmod() function was telling me to do as it relates to the actual issue that I'm having.  Thanks for the suggestion though!  I'm still working on it.

 

In what directory is the script that's begin run (register.php)? In what directory is the users text file?

 

Larry - My register.php file is in a folder that I've created for the book and additionally created a folder for each chapter of the book within it and they are all inside of my webroot (htdocs) folder.  The actual location is C:\xampp\htdocs\php_for_the_web\ch11_files_and_directories\register.php and the location for the users text file is C:\xampp\users\users.txt ...I'm thinking that it's something to do with my file paths but I'm not sure.  I'll definitely need to spend a little more time with them as they are not my strong point, even though they should be pretty simple, but I will say they are making more sense and I'm getting better!  Thanks for the help in advance!

 

Scatz

Link to comment
Share on other sites

Yes, your file paths are the problem. Your path is ../users/users.txt. That says to go up one directory (../), then go into the "users" directory (users/), then use "users.txt". If register.php is in C:\xampp\htdocs\php_for_the_web\ch11_files_and_directories\, the going up one directory (../) puts you in C:\xampp\htdocs\php_for_the_web\. Hence, the problem.

 

The easiest solution would be to use an absolute reference to the file: C:\xampp\users\users.txt

Link to comment
Share on other sites

Yes, your file paths are the problem. Your path is ../users/users.txt. That says to go up one directory (../), then go into the "users" directory (users/), then use "users.txt". If register.php is in C:\xampp\htdocs\php_for_the_web\ch11_files_and_directories\, the going up one directory (../) puts you in C:\xampp\htdocs\php_for_the_web\. Hence, the problem.

 

The easiest solution would be to use an absolute reference to the file: C:\xampp\users\users.txt

 

It worked!!!  Thanks Larry!  Man I kind of figured that that's where my issue was and for some reason, file paths trip me up a little bit but I'll for sure figure them out and that was a great bit of advice you gave me that I'll definitely hold on to!  

 

On another note, I'm so glad you wrote this book and I knew it was a winner as soon as I started!  I literally have 5 more (PHP and MySQL 4th Ed, PHP Advanced 3rd Ed, Modern JavaScript, Effortless E-Commerce, and The Yii Book for when I get to frameworks) that are on my shelf that I plan to tackle as soon as I finish this one and this summer while off from work as a teacher!  I know it will take me some time but I'm extremely confident that these series of books that you've written will put me on the path to being a full on web developer in no time!  You flat out rock and I just want to let you know that it is appreciated, take care!

 

Scatz 

Link to comment
Share on other sites

  • 1 year later...

Please help me Larry, 

I want to get this so bad so I can move on to databases.  

 

I have the same problem as the last person. I first tried the relative path, exactly in the book and that didn't work. So now I am trying the absolute path and it isn't working either.  I am getting the same mssg as on 325. I am using PHP 5.4. Here's my code:

$dir = 'home/kireaton/users/';
            $file = 'home/kireaton/users/users.txt';
            
            if ($_SERVER['REQUEST_METHOD'] == 'POST')
            {
                // Handle the form.
                $problem = FALSE; // No problems so far.
                // Check for each value...
                if (empty($_POST['username']))
                {
                    $problem = TRUE;
                    print '<p class="error">Please enter a user name!</p>';
                }
                 if (empty($_POST['password1']))
                {
                    $problem = TRUE;
                    print '<p class="error">Please enter a password!</p>';
                }
                  if ($_POST['password1'] != $_POST['password2'])
                  {
                    $problem = TRUE;
                    print '<p class="error">Your password did not match your confirmed password!</p>';
                }
                    if (!$problem)
                    { // If there were not any problems 
                        if (is_writable($file))
                        
                        { // If its writable then write it!
                          // Create the data to vbe written:
                            $subdir = time() . rand(0, 4596);
                            $data = $_POST['username'] . "\t" . md5(trim($_POST['password1']))  . "\t" . $subdir . PHP_EOL;
                          // Write the data:
                            file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
                            
                            // Create the directory
                            mkdir ($dir. $subdir);
                             
                            // Print a mssg:
                            print '<p>You are now registered!</p>';
                        }else{
                            print '<p class="error">You could not be registered due to a system error.</p>';
                        }
                    }else{
                             // Forgot to enter a field
                                print '<p class="error">Please try again.</p>';
                    }
                }else{
                  // Display the form
 
            ?>
            <form action="register.php" method="post">
                <p>Username: <input type="text" name="username" size="20" /></p>
                <p>Password: <input type="password" name="password1" size="20" /></p>
                <p>ConfirmPassword: <input type="password" name="password2" size="20" /></p>
                <input type="submit" name="submit" value="Register" />
            </form>
            <?php } ?>
        </body>
    </html>
 
Link to comment
Share on other sites

 Share

×
×
  • Create New...