Jump to content
Larry Ullman's Book Forums


  • Content Count

  • Joined

  • Last visited

  • Days Won


lingolatz last won the day on November 14 2011

lingolatz had the most liked content!

Community Reputation

2 Neutral

About lingolatz

  • Rank
  1. Hmm... alright. Turns out the code I made does not work, unless I am not seeing something else in my code. Also, margaux, your code is not working either.
  2. I have been seeing more and more 'mysqli' stuff around instead of 'mysql' commands. I finally searched it and came up with that it is 'improved'. I did not feel the need to know much more after reading that 'mysql' would be deprecated by the release of PHP 6, so I looked into converting to 'mysqli'. In a simple search, I found this converter tool on a wiki page by mysql.com: http://forge.mysql.com/wiki/Converting_to_MySQLi. I backed up one of my directories and tested it locally. I was given this list of results of which files it altered, how many mysql_* functions were found, etc. (what I find ironic is that I put the converter in the directory I converted, and I got only warnings for files in that directory). Anyways, when I went to see how my scripts ran on Firefox, they turned out identical. However, as I check the source, I have yet to find an actual adjustment. Did this 'converter tool' work? How should I convert to mysqli? Thank you in advance, Lingolatz
  3. Thank you for the tip, margaux. Here is what I came up with before coming back to check the thread: if ( empty($_POST['username']) ) { $problem = TRUE; print '<p>Please enter a username.</p>'; } { include('../mysql_connect.php'); $query = "SELECT * FROM credentials WHERE username='{$_POST['username']}'"; $result = mysql_query($query, $dbc); if ( !empty($result) ) { $row = mysql_fetch_array($result); $problem = TRUE; print '<p>Sorry, but the username ' . $_POST['username'] . ' is already in use.</p>'; } else { // Username is free! } mysql_close($dbc); } This bit is in between the other checks that I go through (firstname, lastname, etc.) before creating the registration query
  4. I am using what I have learned from the Intro to Databases chapter of the book to create a table that will hold user credentials: id, firstname, lastname, username, email, hash, timestamp are the columns. For the registration page, I want the user to submit the data firstname, lastname, username, email, and the password which will become hash. (hash being the md5($_POST['password']) string). More to the point, I want a mechanism to check whether or not the user's desired username is already taken or not. I initally went to page 361 (Retrieving Data from a Database). I found this code, thinking I could use it as a solution: SELECT * FROM users WHERE name='Larry' I figure that I can alter it to read: SELECT * FROM credentials WHERE username="{$_POST['username']}" (credentials being the table containing the relevant data). That code would of course be stored in a $query variable. Would I have to place the code in a loop? I do not think I would have to, since there could only be one username in the table that could match the desired username. How would I go about checking whether the query (or loop) returned results or not? Are there any things I can do to make my current system better? Thank you in advance, Lingolatz
  5. Larry, I have seen echo used in some other tutorials, but it does not come up in your book (as far as I remember). What is the difference between it and print ? What are the costs and benefits of using each?
  6. I am having trouble understanding how one would go about solving this problem: Does 'configuration file' translate to 'text file', that acts as a sort of configuration file? I do not see what the purpose of this problem is. Why does one want the login credentials defined, a configuration file to hold the credentials, and then the file included in various pages?
  7. Would any HTML tags inside of JavaScript be stripped? var str = "<br />Are you going to take my br bros? D: <br />";
  8. AprilSwenby function make_date_menus(){ $months=array (1=>'January','February','March','April','May','June','July','August','September','October','November','December'); ... } print'<form action="" method="post">'; make_date_menus(); print'</form>'; phpRob function make_date_menus($n, $v = 10) { $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); ... print '<select name="year">'; for ($y = $n; $y <= ($n + $v); $y++) { print "\n<option value=\"$y\">$y</option>"; } print '</select>'; } print '<form action="" method="post">'; make_date_menus('1980', '20'); print '</form>'; Shouldn't you both be putting something in the quotes for the action attribute? I recommend taking the $months array assignment out of the function, the array is being reassigned each time the function is called. Even though the function is only called once, if it was called multiple times per page, you would be reassigning over and over, even though months do not change. AprilSwenby, please use spaces to separate values of an array when assigning. It is not required, but it makes code look so much more nicer, especially if someone else is going to read it. Use spaces. Like $months=array could use some spaces. This forum, fortunately, also has a feature that allows you to paste your code and let others read it with highlighted syntax and stuff. To utilise it, look to the editor bar for two less than and equal to signs (< >), or use '[' CODE ']' to open and '[' /CODE ']' (without the quotes) to close your code. The code will then be formatted.
  9. First thing I saw was the '!==' for this condition: $open = fopen("$file", 'a+'); //opens my data file while ($line = fgetcsv($open, 200, "\t" )!== FALSE) { As for the rest of your code (put together)... $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])) { $problem = TRUE; print '<p>That username has been taken, please try again</p>'; break; } fclose($open); Break down: line 1: You do not need a+, although Mr. Ullman says it is most forgiving, you can get by with r. If you used a+ to be less restrictive to debug, that is understandable, but if that is the case, then you should have also appended a 'b' (binary). Regardless of what you use, I would recommend appending the 'b'. line 2: Problem mentioned above. Also, if you look at your code $line = fgetcsv($open, 200, "\t" )!== FALSE You are saying that $line is being assigned to fgetcsv($open, 200, "\t"), which is a TRUE statement (all assignments are true). Then you are using a comparative operator (!= or ==, I cannot tell) for a TRUE statement against FALSE. So say you were using !=, you are saying TRUE is not equal to FALSE, a true statement, so the '!= FALSE' would be redundant? And say you were using ==, you are asking if TRUE is equal to FALSE? My working code does not include the != or == FALSE part. Line 3: You assign $n to the amount of values in the array $line (whose values are the username, password, and subdirectories of a user, separated by tabs). Therefore, $n would be equal to three, yes? I will refer to this line while breaking down line four. Line 4: This is where you start checking if the value that you just read from the file is equal to the value submitted by the user, by using an if statement. You compare $_POST['username'] with $line[$n]. $_POST['username'] is pretty much out of our hands; we cannot control what it is equal to. But you are using two variables with $line[$n] - $line and $n. Think about what these values are equal to. To be honest, this was one of the most confusing pieces of the code to me. $line is an array assigned values of 200 bytes (or till the end of the line) of a line. The line may contain something like this: lingolatz 0ea4a0db010efd4ed3ca4ebee723b65c 13217357292811 So $line[0] would equal lingolatz, $line[1] would equal 0ea4a0db010efd4ed3ca4ebee723b65c, and $line[2] would equal 13217357292811. As mentioned earlier, $n is equal to three (unless you included more items in the users.txt file). Therefore $line[$n] is equal to $line[3], and that does not even exist. Can you tell, now, what should be used to be compared to $_POST['username']? You also use extra, unneeded parentheses to surround '$line[$n]'. The rest of your code has no problems as far as I can tell.
  10. The 'was removed' was not there when I was running the code. I am pretty sure the page would not have even loaded if the 'was removed' was present, so that is not the problem.
  11. For the list_dir.php script, line 42 sets conditions to determine if an item should be written or not. The conditions are: if ( (is_file($search_dir . '/' . $item)) AND ((substr($item, 0, 1) != '.') ) I rewrote the condition to fit my preferences, and I will be referring to my version when asking my question, so if my code is wrong that could be the problem. But to the best of my knowledge, they are equivalent. if (is_file($search_dir . '/' . $item) && sub_str($item, 0, 1) != '.') The conditions are quite obvious, and stated in the text. But when I execute the code, no files are shown, and I checked the HTML to make sure. It is the script. I began debugging and discovered that when I remove the second part of the condition && sub_str($item, 0, 1) != '.' it works fine. That makes sense in a way, because it is less restrictive, but none of my file names begin with a period. And what is odd is that I do not encounter a similar problem with directories. When the same 'second part' of the condition is removed for the directory section, the predicted outcome occurs (the parent '..' and current '.' directories appear). I would like to point out that I am running my server off of my Linux (Ubuntu 11.10) computer - implying that I am not running Windows or Mac OS X. My webroot is the 'php' directory with this path: /var/www/php/ Hypotheses: A permissions issue Code in the book is not applicable to my server's operating system The php.ini file is configured differently than the author's I do not know how to test any of the hypotheses, but I imagine I would be able to test the first one by using chmod, but I do not full understand what 'group' and 'other' mean, or what user PHP is under, or what permissions should be sufficient. The entirety of my script follows. <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="default.css" /> <title>Directory Contents</title> </head> <body> <?php // Set directory name and scan $search_dir = '.'; $contents = scandir($search_dir); // List directories print '<h2>Directories</h2> <ul>'; foreach ($contents as $item) { if (is_dir($search_dir . '/' . $item) && substr($item, 0, 1) != '.') { print '<li>' . $item . '</li>' . "\n"; } } print '</ul>'; // Create table for files print '<h2>Files</h2> <table> <tr> <td>Name</td> <td>Size</td> <td>Modified</td> </tr>'; // List files foreach ($contents as $item) { if (is_file($search_dir . '/' . $item)) // && (sub_str($item, 0, 1) != '.') was removed { $fs = filesize($search_dir . '/' . $item); $lm = date('F, j, Y', filemtime($search_dir . '/' . $item)); print " <tr> <td>$item</td> <td>$fs bytes</td> <td>$lm</td> </tr>"; } } print '</table>'; ?> </body> </html> This HTML is the result of including the second part of the condition: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="default.css" /> <title>Directory Contents</title> </head> <body> <h2>Directories</h2> <ul><li>css</li> <li>template</li> </ul><h2>Files</h2> <table> <tr> <td>Name</td> <td>Size</td> <td>Modified</td> </tr> I do not know why it does not print code after the foreach statement, or even after the closing php tag.
  12. I have grown a strong liking to TextMate. It is not free, and is only compatible with Mac as far as I know, but if you ever have the opportunity, I highly recommend using it. I use Scribes on Ubuntu, and it gets the job done. I also have Geany installed in case Scribes acts up. I have never been a fan of Dreamweaver, especially if you are paying for it. It is too cluttered in my opinion, and for a beginner, one can get lost. I do not really like Notepad++ either, but it has been a while since the last time I used it, perhaps updates has made it better. (For the record, unlike Notepad++, I have used a recent version of Dreamweaver recently) TextWrangler is a good one too, as others have mentioned. I am still searching for an editor as good as TextMate, so if anyone else on this thread knows one, please mention it.
  • Create New...