Jump to content
Larry Ullman's Book Forums

Recommended Posts

Please note that I do understand that the needs to be a second call to the strtok() function. But I don't understand that language. Can you find a way to re-word that for me? I am finding that the first strtok() call uses the string argument. After the first call, this function only needs the split argument, as it keeps track of where it is in the current string. To tokenize a new string, call strtok() with the string argument again

 

I don't know "how" to do that... If I apply this line if ($_POST['username'] == strtok($data) ) That really doesn't do anything but repeat the line above it.

Link to post
Share on other sites
  • Replies 64
  • Created
  • Last Reply

Top Posters In This Topic

I am looking into the second call of the function - and I know I'm onto something - but it's still not working. I just don't think I'm completely comprehending the way this function works with the text file.

 

$newdata = file_get_contents($file); //Reads the file contents

$data = file_get_contents($file); //Reads the file contents

$tok = strtok($data, " ");//First call of the strtok function which is putting the data contents into one string

 

while ($tok != false) // This is supposed to take everything that was separted as tokens from the data file

{

$tok = strtok(" ");

}

 

 

if ($_POST['username'] == $tok ) { // compares the indivdual words from the $data file to the what the user entered in the username box and initiates the second call.

$problem = TRUE;

print '<p>That username has been taken, please try again</p>';

}

Link to post
Share on other sites

I am looking into the second call of the function - and I know I'm onto something - but it's still not working. I just don't think I'm completely comprehending the way this function works with the text file.

 

$newdata = file_get_contents($file); //Reads the file contents

$data = file_get_contents($file); //Reads the file contents

$tok = strtok($data, " ");//First call of the strtok function which is putting the data contents into one string

 

while ($tok != false) // This is supposed to take everything that was separted as tokens from the data file

{

$tok = strtok(" ");

}

 

 

if ($_POST['username'] == $tok ) { // compares the indivdual words from the $data file to the what the user entered in the username box and initiates the second call.

$problem = TRUE;

print '<p>That username has been taken, please try again</p>';

}

 

Ok, so you've read the manual and followed the examples - which is a step forward to your solution and your skills.

 

These two lines do exactly the same thing, you only need one.


$newdata = file_get_contents($file); //Reads the file contents
$data = file_get_contents($file); //Reads the file contents

 

This code:


while ($tok != false)  // This is supposed to take everything that was separted as tokens from the data file
 {
  $tok = strtok(" ");
 }

 

So what happens if your code was this:


while ($tok != false)  // This is supposed to take everything that was separted as tokens from the data file
 {
echo "current $tok<br />";
  $tok = strtok(" ");
 }

 

You should see each token (or piece) of the whole big text file string returned one after another on a separate line as the function moves along the string.

 

So its there that you need to check the value of $_POST['username'] against the segment of the string that is currently being returned.

Link to post
Share on other sites

Hey there - I talked to my teacher - and he said that the book is probably looking for the fgetcsv function... So I had to start over this afternoon - and I'm actually pretty far! Everything is working - only the file isn't writing and I think that because it is returning "You could not be registered due to a system error" I have checked my file locations and everything where it should be - but it is writing what the user puts in. Here is my new script... I have hashed it out - and I think the script looks perfect... but its not doing what I want - This is the code that I changed when I took out the code I wrote for the strtok function - so when you look below at the full code you are actually looking at this piece.

 $data = file_get_contents($file); //Reads the file contents
$open = fopen($data,'rb'); 
while ($line = fgetcsv($open, 200, " " )) {
$n = count($line);

if  ($_POST['username'] == ($line[$n]))  { // compares
$problem = TRUE;
print '<p>That username has been taken, please try again</p>';
break;

    }
    fclose($open);

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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. */

 

// 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.

 

 

$data = file_get_contents($file); //Reads the file contents

 

$open = fopen($data,'rb');

 

while ($line = fgetcsv($open, 200, " " )) {

$n = count($line);

 

if ($_POST['username'] == ($line[$n])) { // compares

 

$problem = TRUE;

 

print '<p>That username has been taken, please try again</p>';

 

break;

 

 

}

fclose($open);

 

// 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="registerpursue.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 post
Share on other sites

The book quite possibly is looking for the fgetcsv() function if it's been already mentioned in this chapter. However we started with the strtok() as you mentioned that previously, you can do a lot of things in PHP using various functions. There is often no right or wrong way to go about something.

 

But by switching functions you are again starting from scratch.

 

If you are using fgetscv() then why do you need this line?


$data = file_get_contents($file); //Reads the file contents

 

You are not using fopen() properly, check the arguments it takes and what they should be. You have dreamweaver dont you? So hold Ctrl + SpaceBar and it will bring up function info from php.net. If fopen() fails then you have no chance of getting the rest of the script to work as its the basis of your logic.

 

You were also close to completing this task using the strtok() that you had before.

Link to post
Share on other sites

Thank you for the Dreamweaver tip. That's a great help.

 

I don't know why I need this line? I just thought I did! I thought that was how I was "getting the data" but I guess the fopen is getting the data too right?

  $data = file_get_contents($file);

 

Here is how I see my code going.

 

This line

  $data = file_get_contents($file); //Reads the file contents

 

Is getting the contents from the $file variable which contains this line of code

$dir = '../users';
$file = $dir . 'users.txt';

 

Now my file is being stored in the $data variable.

 

I am using this line now just as the arguments suggest - $data is my file and the 'b' is what the book suggests to use (as does Dreamweaver) Now isn't my open data stored in the $open variable? What arguments are not correct. This is exactly how the book does it AND these are the arguments that Dreamweaver and PHP.net indicate.

 

$open = fopen($data,'b'); 

 

I asked my teacher about the strtok() function and implied that I wasn't close - he said I had ALOT of code left to write.... I just didn't know what to do - I had felt I had hit a wall with that script and I just couldn't see what you were trying to tell me - and when I sought him for help her directed me this way.... I really feel like I 'am trying to understand and that is why I'm being so descriptive to you at "why" I'm writing the code I am writing it - in hopes that maybe if you see where I am coming from you can redirect my way of thinking into the way PHP is understanding it.

 

NOTE: about fopen - it also says that resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )

 

I have no idea what this means [, bool $use_include_path = false [, resource $context ]] ) The book didn't discuss that - but is that the argument you indicate that I have wrong?

Link to post
Share on other sites

Thank you for the Dreamweaver tip. That's a great help.

 

I don't know why I need this line? I just thought I did! I thought that was how I was "getting the data" but I guess the fopen is getting the data too right?

  $data = file_get_contents($file);

 

Here is how I see my code going.

 

This line

  $data = file_get_contents($file); //Reads the file contents

 

Is getting the contents from the $file variable which contains this line of code

$dir = '../users';
$file = $dir . 'users.txt';

 

Now my file is being stored in the $data variable.

 

No, now the contents of the $file are being stored in $data as one big string.

 

fopen takes these arguments ( string $filename , [string $mode [, bool $use_include_path = false [, resource $context ]]] )

 

The first 1 is required (in PHP 5, the first 2 are required in PHP 4 ), the last two are optional as they are in square brackets. The first argument expects a file name, you've passed it the whole contents of your text file.

 

http://www.w3schools...tem_fgetcsv.asp

 

I would also argue that as you're not using a .csv file its just plain text that fgetcsv isn't the only option.

Link to post
Share on other sites

You can use either, they're are often several functions that are appropriate to a developer, its just whatever you prefer to use. Just make a decision and stick with one function. You've moved onto fgetscv() so just use that one, especially if your teacher wants that one.

Link to post
Share on other sites


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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. */

// Identify the directory and file to use:
$dir = '../users';


/*
* This is the location of the text file
*/
$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.

/*
* You do not need this line any more
$data = file_get_contents($file); //Reads the file contents
*
*/

/*
* $data does not exist anymore so replace
* $data in the fopen() call with something else
* that could represent your file location
* (Maybe $file)
*/
$open = fopen($data,'rb');

while ($line = fgetcsv($open, 200, " " )) {
$n = count($line);

if ($_POST['username'] == ($line[$n])) { // compares

$problem = TRUE;

print '<p>That username has been taken, please try again</p>';

break;


}
fclose($open);

// 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="registerpursue.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>

 

I have pointed you in the right way in terms of how to open the file.

Link to post
Share on other sites

According to the book - the fgetcsv() function breaks the string into parts using the marked separator and returns an array. So the way I see it - fopen has opened my file as one string and teh fgetcsv is separating it out and my while loop is looping through the file until the end. So what have I done wrong again?

 

 

echo out the $data variable and you'll see how that is not the filename of your 'users.txt' file.

Link to post
Share on other sites

Jonathon - that's exactly what I've been trying - and it still doesn't work. Did you run the script? I removed the line item, I changed the variable in the fopen() and it doesn't work. Still keeps telling me "

You could not be registered due to a system error

 

In my while loop I changed it to "\t" too because as I looked further into the script I see that is the delimiter. FYI - so here is my entire script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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. */

// 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.


ini_set('auto_detect_line_endings', 1);
$open = fopen("$file",'a+'); 

while ($line = fgetcsv($file, 200, "\t" )) {
$n = count($line);

if  ($_POST['username'] == ($line[$n]))
if($_POST['password1'] == ($line[$n]))
if($_POST['password2'] == ($line[$n])) 
{

$problem = TRUE;


print '<p>That username has been taken, please try again</p>';
break;


 }
 fclose($open);



   // 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="registerpursue.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 post
Share on other sites

Jonathon -

 

Here is my error now - I've been working on it non - stop . Found out that users directory was missing a slash. Argh...

Anyway. I now have an error to work with -

Warning: fgetcsv() expects parameter 1 to be resource, string given in C:\xampp\htdocs\registerpursue.php on line 54

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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_reporting(E_ALL);
// 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.


ini_set('auto_detect_line_endings', 1);
$open = fopen('$file', 'a+');  //opens my data file
while ($line = fgetcsv($file, 200, "\t" )) {  //breaks my string from $file into parts
$n = count($line);

if  ($_POST['username'] == ($line[$n])) //checks to see if user entered data matches what's in the text file
if($_POST['password1'] == ($line[$n])) //checks to see if user entered data matches what's in the text file
if($_POST['password2'] == ($line[$n]))  //checks to see if user entered data matches what's in the text file
{
$problem = TRUE;

print '<p>That username has been taken, please try again</p>';   //if above is true, print this statement

break;

 }
 fclose($open);

   // 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="registerpursue.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>

 

I am stumped still. You had indicate I wasn't using my function right. What did you mean by that?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...