Jump to content
Larry Ullman's Book Forums

AprilSwenby

Members
  • Posts

    85
  • Joined

  • Last visited

Everything posted by AprilSwenby

  1. I too had this question and I thank you for your answer - I am trying to find a way to answer the first review question on chapter 414. Do you mean - how would we re-write rather than how would the the function be called?
  2. Part of my problem was that on page 394, the written instructions did not say to type lines 22 and 23.... so I wasn't included the database connect file. I have done that now, and Now I am getting a message that says... Could not store the quote because: No database selected. The query being run was: INSERT INTO quotes (quote, source, favorite) VALUES ('test', 'test', 0) That's why I think I either have something typed wrong in my table or things aren't stored in the right directories.... I have debugged and I am connected to the database…. it still seems to be having a problem selecting the database….. I have attached my add_quotes script and my mysql_connect.php script and following is my table. Right now, I have my mysql_connect stored in the xamp folder, my add_quotes stored in the htdocs folder and I have a myquotes.php (which is my table) in both the xamp folder and in the htdocs folder, as I was trying to figure out if I had it in the right directory…. add_quotes.php <?php // Script 13.7 - add_quote.php /* This script adds a quote. */ // Define a page title and include the header: define('TITLE', 'Add a Quote'); include('templates/header.html'); print '<h2>Add a Quotation</h2>'; // Restrict access to administrators only: if (!is_administrator()) { print '<h2>Access Denied!</h2><p class="error">You do not have permission to access this page.</p>'; include('templates/footer.html'); exit(); } // Check for a form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form. if ( !empty($_POST['quote']) && !empty($_POST['source']) ) { // Need the database connection: include('../mysql_connect.php'); // Prepare the values for storing: $quote = mysql_real_escape_string(trim(strip_tags($_POST['quote'])), $dbc); $source = mysql_real_escape_string(trim(strip_tags($_POST['source'])), $dbc); // Create the "favorite" value: if (isset($_POST['favorite'])) { $favorite = 1; } else { $favorite = 0; } $query = "INSERT INTO quotes (quote, source, favorite) VALUES ('$quote', '$source', $favorite)"; $result = mysql_query($query, $dbc); if (mysql_affected_rows($dbc) == 1){ // Print a message: print '<p>Your quotation has been stored.</p>'; } else { print '<p class="error">Could not store the quote because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>'; } // Close the connection: mysql_close($dbc); } else { // Failed to enter a quotation. print '<p class="error">Please enter a quotation and a source!</p>'; } } // End of submitted IF. // Leave PHP and display the form: ?> <form action="add_quote.php" method="post"> <p><label>Quote <textarea name="quote" rows="5" cols="30"></textarea></label></p> <p><label>Source <input type="text" name="source" /></label></p> <p><label>Is this a favorite? <input type="checkbox" name="favorite" value="yes" /></label></p> <p><input type="submit" name="submit" value="Add This Quote!" /></p> </form> <?php include('templates/footer.html'); ?> table as instruction on page 390 called myquotes <?php $dbc = mysql_connect('localhost', 'root', 'spring'); $query = 'CREATE TABLE quotes ( quote_id INT UNSIGNED NOT NULL AUTO_INCREMENT, quote TEXT NOT NULL, source VARCHAR(100) NOT NULL, favorite TINYINT(1) UNSIGNED NOT NULL, date_entered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (quote_id))'; ?> My mysql_connect.php file which has been debugged and I am connected. <?php $dbc = mysql_connect('localhost', 'root', 'spring'); mysql_select_db('myquotes', $dbc); ?>
  3. Yes - thank you I did fix that error right away - that is not my problem.
  4. Hi Larry - I am following along in the book - and I am on page 393 when we begin to add quotes. When I add a quote, the page returned sends back these errors: Notice: Undefined variable: dbc in C:\xampp\htdocs\add_quote.php on line 18 Warning: mysql_real_escape_string() expects parameter 2 to be resource, null given in C:\xampp\htdocs\add_quote.php on line 18 Notice: Undefined variable: dbc in C:\xampp\htdocs\add_quote.php on line 19 Warning: mysql_real_escape_string() expects parameter 2 to be resource, null given in C:\xampp\htdocs\add_quote.php on line 19 Notice: Undefined variable: dbc in C:\xampp\htdocs\add_quote.php on line 28 Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\add_quote.php on line 28 Notice: Undefined variable: dbc in C:\xampp\htdocs\add_quote.php on line 30 Warning: mysql_affected_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\add_quote.php on line 30 Notice: Undefined variable: dbc in C:\xampp\htdocs\add_quote.php on line 33 Warning: mysql_error() expects parameter 1 to be resource, null given in C:\xampp\htdocs\add_quote.php on line 33 Could not store teh quote because: . The query being run was: INSERT INTO quotes (quote, source, favorite) VALUES ('', '', 0) That makes me think I do not have my table set up properly.... Page 380 didn't give a whole lot of details as to how to set up but here is what my table looks like: <?php $dbc - mysql_connect('localhost', 'root', 'spring'); if ($dbc) { $query = 'CREATE TABLE quotes ( quote_id INT UNSIGNED NOT NULL AUTO_INCREMENT, quote TEXT NOT NULL, source VARCHAR(100) NOT NULL, favorite TINYINT(1) UNSIGNED NOT NULL, date_entered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (quote_id))'; } mysql_close($dbc); ?> Any ideas why this isn't working?
  5. Yes! That worked for me too! So it seems I had it right at one point - but just had it in the wrong place! How frustrating! I've been working on this for over a week now.... Thank you so much for pointing that out to me.
  6. Hi Larry - I took a break like you said, and I feel like I'm so close! But...... This script returns about 100 lines of But that's because I've been entering 'april' for the last 100 times I've been testing the script. I tried a new name and the first time I entered the name it said that I've been registered, and the second time it told me This username is already used. Please enter a unique username! You are now registered! So it's returning two of my statements that contradict each other. This is further than I've ever gotten before - because I could never get it to return that at all - but why is it repeating? I have the break; for the while Also I did this print "<p>line = -{$line}-</p>"; and found that line = -Array-. I don't even know what that means - but I think that's good - because then my text file is now in an array, but it's not split by the delimiter like I want. So to sum it up - it seems to be returning the message This username is already used. Please enter a unique username! for as many times as that name is entered and it also contradicts that message at the end by saying You are now registered! <!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, 'r');//Check if username is duplicate: while($line = fgetcsv($open, 500, "\t")) {//Loop through the file: for($n=0; $n < count($line); $line++) { break; }//End of For if( ($line[0] == $_POST['username']) && ($line[0] == $_POST['username'])){ print '<p class="error">This username is already used. Please enter a unique username!</p>'; } }//End of while 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>
  7. I am a little closer think. I used this code ini_set('auto_detect_line_endings', 1); $open = fopen($file, 'r'); //opens my data file while ($line = fgetcsv($open, 200, "\t" )) { //breaks my string from $file into parts if ($_POST['username'] == ($line[0])){ $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement } break; } fclose($open); This code was debugged and I ran the form, I rec'd this as the return... That was telling me that it wasn't looking throughout the text file and was only looking at just the first word in the text file. Then Larry I have been practicing your debugging techniques and I read through the chapter on arrays again. Here is the code I have written... ini_set('auto_detect_line_endings', 1); $open = fopen($file, 'r'); //opens my data file while ($line = fgetcsv($open, 200, "\t" )) { //breaks my string from $file into parts for($n=0; $n <= count($line); $n++) { if ($_POST['username'] == ($n[0])){ $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement } } break; } fclose($open); The problem is that - it is not recognizing unique usernames. I can enter april in as my username 100 times and it will always tell me that I've been registered. When i debugged this using this line print "<p>line[n] = -{$line[$n]}-</p>"; My output was this. (the notice is a repercussion of this line print "<p>line[n] = -{$line[$n]}-</p>"; so I'm not worried about that. So from this debugging - I am getting the feeling that it's not scanning my entire text file. It's only scanning the first line. Register line[n] = -April- line[n] = -2a2d595e6ed9a0b24f027f2b63b134d6- line[n] = -13214649081347- Notice: Undefined offset: 3 in C:\xampp\htdocs\registerpursue.php on line 59 line[n] = -- You are now registered! So my question is - how do I get this script to read and scan every word in the text file? that is what I thought i was doing with the for loop - but it's not doing that.... It only took the first 3 words. I really don't mean to keep bothering the forum lines, but I really do want to understand this.....
  8. Hi Larry - Thank you for your advice. I did apply those debugging techniques. I found that username is correct. I also found that $line is returning the first word in my text file - so it really not "looping" through the text file to find matches. That is helpful information. So the question is - how do I get things to loop through - I thought that is what my count() was doing. Can you direct me that way?
  9. Well - that didn't make sense either! I don't want the username compared to the passwords... So then I tried this... And my script worked without errors, but I am still able to enter duplicate usernames.... HELP! ini_set('auto_detect_line_endings', 1); $open = fopen("$file", 'r'); //opens my data file while ($line = fgetcsv($open, 20, "\t" )) { //breaks my string from $file into parts $n = count($line); if ($_POST['username'] == ($line[0])){ $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement } break; } fclose($open);
  10. or maybe this would be better...Still gives me an error Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\registerpursue.php on line 59 - but is this more on the right track? ini_set('auto_detect_line_endings', 1); $open = fopen("$file", 'r'); //opens my data file while ($line = fgetcsv($open, 20, "\t" )) { //breaks my string from $file into parts $n = count($line); if ($_POST['username'] == ($line[0] $line[1] $line[2])){ $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement break; } } fclose($open);
  11. Thank you lingolatz! This helped immensly - however, I am still a little unclear when trying to follow your direction on the Here is what I have based on what you stated.... <!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", 'r'); //opens my data file while ($line = fgetcsv($open, 20, "\t" )) { //breaks my string from $file into parts $n = count($line); if ($_POST['username'] == ($line[0]){ $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement if($_POST['password1'] == ($line[1]) { $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement if($_POST['password2'] == ($line[2]) {//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> You also said I have an unneeded parentheses to surround '$line[$n]'.... ($_POST['username'] == ($line[$n])) Isn't that correct syntax? The parantheses surrounds the whole thing and then opening and closing ones for ($line[$n])?
  12. Yes - that is exactly what the problem was! Thanks! I still can't get the script to recognize a duplicate user name though! I just don't know what I am doing wrong.... I feel like I've tried every method.....
  13. Hello all - I am having a huge stumper. I have have been diligently working on the Pursue question for chapter 11 that requires the script to guarantee unique usernames in register.php Here is my script. I just can't figure out what I am doing wrong and why this won't work for me. I have modified it so many times! Here is what I have right now. This is what is being returned with this script: Register You are now registered! Warning: fgetcsv(): 3 is not a valid stream resource in C:\xampp\htdocs\registerpursue.php on line 55 <!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($open, 200, "\t" )!== FALSE) { //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 $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>
  14. 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?
  15. 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>
  16. I know this is pretty basic - but when I write echo $data All I get returned You could not be registered due to a system error. I can't even echo things out! But from your last statement I feel you think I should change my variable $open = fopen("$data",r'); to something else? I tried $file and I even tried the raw file path and name. I still got the same errors. I am sorry I am not getting this, but I feel like you are talking to me "encrypted" I just don't understand what you are trying to tell me....
  17. 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?
  18. Okay - but how do I get my $data out of one string? You said it's in one string now? I'm just at a loss. I don't know what to do next....
  19. Okay - but how do I get my $data out of one string? You said it's in one string now? I'm just at a loss. I don't know what to do next....
  20. Would you say that I needed to use the strtok function to break up the string then?
  21. 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?
  22. So then....I followed the instructions after I got logged in and I changed the password and I then went to the config file and changed that and now I can't get in with either url! It says this: phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server. What did I do wrong and how do I fix it so I can continue with the chapters?
  23. I am trying to assign the password to the root user via phpMyAdmin as explained in Appendix A on page 432 - I referred back to page 428 to gain access to use phpMyAdmin. When I try to open the link in my browswer - I get this error #2002 - The server is not responding (or the local MySQL server's socket is not correctly configured) Connection for controluser as defined in your configuration failed. I googled the error and someone said to replace the http://localhost/phpMyAdmin with http://127.0.0.1/phpmyadmin/ I did that and it worked. Why?
  24. 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>
×
×
  • Create New...