Jump to content

Christian F.

Staff Alumni
  • Posts

    3,072
  • Joined

  • Last visited

  • Days Won

    18

Everything posted by Christian F.

  1. "." is a directory link for "current directory", ".." is the same for the parent folder. Two items that are present in all directories, as a control structure for utility purposes. Just type "dir" on your Widows computer, or "ls -al" on a *nix-based OS, and you'll see.
  2. Read through what that code does*, out loud in plain English, and you should be able to answer that question yourself. Doing this will help you increase your understanding of the code, and thus become a better programmer. * What it actually does, not what you intend/hope for it to do.
  3. No, that means that readdir can return a number of values, of which some can be evaluated to "false". Check out the type autocasting/juggling section of the PHP manual for more detailed information.
  4. You cannot make it 100% safe, but you can make it a bit more troublesome. The easiest way to do so, is to implement SSH and asymmetric encryption to your application. Note that even if you encrypt all of the communication, you have still given the user the key to said encryption. That means that they can still find this key, and send whatever input they like to the PHP page. Which means that you have to secure the PHP application properly in any way, to ensure that people are not able to attack your site easily. That said: There are no quick fix for anything when it comes to security, and it is an ongoing process during the entire planning, programming and maintenance stages. What steps you need to take depends upon all other choices you've done, and what capabilities you want to have in your code.
  5. The "false !=" is usually implied by the fact that if the loop returns a value that can be cast to boolean false, it usually means that it is false. In this instance, however, the author of the code has opted to using the "identical to" operator, in order to allow files/directories with an empty name or named "0".
  6. That made no sense. The image tag is parsed (it doesn't execute) by the client, the browser. The PHP file is executed at the server, which is at a completely different time than when the browser parses the HTML. HTML is not parsed until PHP has completed the execution, and the PHP file in the image src is a completely different PHP file to the one that generated the image tag in the first place. If you have troubles with the script that generates and outputs the image tags, try visiting it directly. Save the results in a file, or show it directly in the browser. Most likely you've got some PHP errors in there, which causes the image data to become corrupted.
  7. Using variable variables is bad form regardless of the case, and using variables for configuration values (which tend to be constants) is not recommended either. What I do recommend you to do, is to define constants for the configuration settings. That way you can access them wherever you like, and you get them clearly defined as something different from runtime data.
  8. Why the second dimension for the title? You could just as easily have just assigned it to the first dimension, seeing as there is only one element in there. That said, what you have there is a two-dimensional array. How to access element in the second (or n'th) dimension is nicely explained in the PHP manual (see example #6).
  9. *Shrug* Here's a few tips for you though: Make sure you post in the correct section. This isn't maths-related. I recommend that you read this article.
  10. Yes, and no. The code snippet itself is too short, and too specific to be object-oriented on its own. That's not to say that it can be used in some object-oriented code. OOP is more of a design philosophy for an entire project, than for a small code snippet. My advice is to just focus on the basics of PHP and programming for now, and then start to look at OOP once you've gotten a solid understanding of the fundamentals down.
  11. Not accurately, no. You might get the country, and the general vicinity (city) correct most of the time. But, the ZIP-code: No chance to get the correct one with any degree of accuracy. Contrary to Hollywood, an IP-address isn't tied to a geographical location, but to businesses. Internet Service Providers. Whom then, in turn, assign these to their customers based upon various internal rules. For the big national ISPs, this might mean that a single IP-address could be used anywhere in the whole country, depending upon who requested an IP-address first when that particular IP-address was available. Smaller, or more sectioned up ISPs could give you a smaller geographical area, possibly down to city-level. Though, not much more than that, and a city has multiple ZIP-codes after all. That's not even considering the vast areas with a minimal population, such as the countryside. The only way to get the accurate ZIP-code for any given IP-address, is to actually look it up in the ISPs registers. Which means comparing their DHCP logs with their customer registers, something which would be illegal without a court order.
  12. I strongly recommend using a library like PHPmailer to do your mailing for you. If not, then you'd need to read up on the SMTP protocol. If you really want to learn, I recommend the latter. Though, I should not that it's probably only for especially interested people. Like me.
  13. That's explained in the PHP manual. Try it out, and if you have more questions after playing around with it let us know what you've tried and what you're wondering about.
  14. It's explained in the video, at 21:50 and outwards. In short the main reasons are because compatibility layer is easier to use, (slightly) more secure, and has the ability to check whether or not a password needs to be rehashed. There are several minor reasons as well, but I won't go into them. However, if, for some reason, you are still running on PHP < 5.3 then PHPass is your only viable option. It's just showing its age, just like older versions of PHP.
  15. Good morning to you too, and you're welcome.
  16. I just want to comment upon this bit: While it is true that using a loop inside another is required at times, there is one thing you never should be using inside loops: SQL queries. Move that SELECT query outside of the loop, concatenate all of the IDs of shops to delete, and then use a "WHERE id IN()" condition to fetch all rows in one query. In short, this is how your code should look, roughly: // Create the sprintf () template for the SQL query. $SQL = "SELECT {$Fields} FROM `stores` WHERE `store_id` IN(%s)"; // Make sure all of the values we're sending to the query are integers. // (Protection against SQL injections.) $storeIDs = array_map ("intval", $store); // Implode the array of IDs into a comma-delimited string, and add to SQL query. $SQL = sprintf ($SQL, implode (',', $storeIDs)); // Execute the query, and handle any potential SQL errors. if (!$res = DB->query ($SQL)) { trigger_error ("Could not retrieve shops!", E_USER_ERROR); } // Loop through all of the shops returned from the query. while ($row = $res->fetch_array ()) { // Delete the folders here. }This way you only try to delete shops that you actually have in your database, and you're saving a whole lot of server resources from cutting down on the number of SQL queries you need to do. PS: I suspect you should be looking into JOINs as well.
  17. You really should be using the parse_url function, as that RegExp of yours allow for some non-desirable strings to slip through. Strings that might not even be an URI. Not to mention that it's a whole lot easier to read, and thus maintain.
  18. Actually, as with PHP 5.5 the PHPass libraries are outdated. So unless you're forced to work on a PHP version less than 5.3.7, I recommend using either Anthony Ferrara's PHP 5.5 compatibility layer or the native PHP 5.5 password functions. For more information I recommend watching the following video: As for the PHP error your script is complaining about: It tells you exactly what's wrong, and most likely why. You just have to pay attention to the details. Without knowing the exact error message we cannot help you with that, I'm afraid. Also, a slight clarification to cpd's previous post. Whenever you get an error status on a form submission you do not have to redirect the user. I prefer to only redirect the user on a successful submission, in order to prevent the user from having an (erroneous) re-submission by hitting F5/refresh. By avoiding this redirect upon failed submissions, the PHP script has access to all of the data sent by the user, which you can then use to re-populate the form again. Without having to jump through extra hoops, such as saving them into a session or something like that. Here's a quick pseudo-code example of how I recommend forms to be processed: if (!submitted ()) { // Show form return; } // Create an empty error array, for use in validation process. $error = array (); // Validate the input from the form, populating the error array upon failure. $val_1 = validate ($_POST['val_1'], $error); $val_2 = validate ($_POST['val_2'], $error); // Check if any field failed validation. if (!empty ($error)) { // Show error message(s) // Re-populate the field, remember to escape the data. // Show the form. return; } // All validation succeeded, process the data and check for DB errors. if (!$res = db->query ($sql)) { trigger_error ("database error!", E_USER_ERROR); } // If we have some checks based upon the result of the SQL query to do, then do it now. if ($res->num_rows != 1) { // Didn't return the expected number of rows. // Show form submission error, re-populate form and show it. return; } // Else we got a successful submission, redirect to prevent F5-resubmit. header ("Location: {$PHP_SELF}");
  19. str_replace will help you with that. Also, with glob you don't need opendir. I recommend following the links and studying the examples in the PHP manual.
  20. The error message tells you what is wrong, just pay attention to the details. Seeing as it complains about an unexpected colon, I would imagine it is either because a mistyped semi-colon or ending the string prematurely. Pay attention to the quotation marks used, and remember that PHP doesn't know what your intentions are; It only does what you explicitly tell it to do, whether it's intentional or not.
  21. I'm pretty sure that is because you're using round instead of ceil, as you would be stripping the last page off if you have less than 50% of the available rows for the last page. Loading all of the data in one go will have an effect on loading times, server load, initial download size, and possibly memory usage. If you can section it up like you've done above, I would recommend doing so. As for the use of tables: If you have tabular data, like your test data seem to suggest, then using a table is the proper course of action. It is a question about semantics, not style, after all. Also, I'm not convinced the DIV around the table is necessary. As you can apply the styling to the table directly. At least in most instances, so I would look into that first. After all, the less code you have, the less probability for problems.
  22. The only thing the "s" modifier does is make the dot (.) match newlines as well. Since there are no (matching) dots in the Regular Expression, as the only one there is escaped, that modifier won't do anything.
  23. If there are no line breaks, or other markers that you can split on, this RegExp should do it. With a variable number of yes/no fields and any kind of name: // Split the lines after a "name - answers" pattern, where the answers can be one or many "yes" or "no"s. $splitGroups = "/(.*?) ((?:Yes|No\\s?)+)/"; preg_match_all ($string, $splitGroups, $groups); // Loops through all of the lines found in the text blob. foreach ($groups as $line) { $name = $line[1]; $answers = explode (" ", $line[2]); // Do whatever you need to do with the two variables above here. }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.