|
I'm writing some authentication code for something I'm mucking around with, while at the same time trying to improve my coding habits. With the example below, CheckPassword() throws an exception if the password is incorrect. For readabilities sake, should CheckPassword() have a boolean return and form an if block around the rest of the code, even though it won't change anything? php:<? public function SetPassword($oldPassword, $newPassword, $newPassword2) { if ($newPassword != $newPassword2) { throw new PasswordMismatchException(); } $this->CheckPassword($this->name, $oldPassword); // Better? // if ($this->CheckPassword($this->name, $oldPassword)) // { $salt = self::CreateSalt(); $password = self::CreateSaltedHash($newPassword, $salt); $sql = 'UPDATE user SET password = "' . DB::Escape($password) . '" salt = "' . DB::Escape($salt) . '" WHERE name = "' . DB::Escape($this->name) . '"'; return $this->db->Execute($sql); // } // return false; } ?> Finite fucked around with this message at 11:04 on Mar 29, 2008 |
# ¿ Mar 29, 2008 02:28 |
|
|
# ¿ May 8, 2024 21:05 |
|
duz posted:It looks like you're using a database abstraction layer. Use its built in prepared query functions instead of string concatenations. It's not, it's just a database object I wrote which does some of the tedious work for me. I know that I need to look at it, but it works fine at the moment and I hadn't planned on looking at it again just yet as I'm already working on a few other things. bt_escm posted:What you want to use is a try/catch around the the code after the newPassword to newPassword2 check. Ok. The OOP/Procedural crap is fixed. That'll teach me to post before testing for obvious errors... I didn't want to bother using a try/catch as it's not really necessary. I want the exception to be thrown as it comes out as an InvalidPasswordException, which I think speaks for itself. My problem was whether the code was readable in it's current state, or if the call to CheckPassword() is confusing as it has no obvious way to fail. Then again, I'm not sure if that's the best way to do this either, and it should just return true/false and I can work from there. It's more object design than PHP Finite fucked around with this message at 11:06 on Mar 29, 2008 |
# ¿ Mar 29, 2008 11:03 |
|
OlSpazzy posted:So it's needed to get the variables within the templates to actually parse. Thanks for the suggestion on table selections. I built a simple templating function and I used extract instead, so you get things like this: php:<? $templateData = array('variable1' => 'value goes here', 'variable2' => 'another value here'); extract($templateData); print ($variable1); // Outputs 'value goes here' ?> Also, put "rendering" code like that inside a function so you don't accidentally write over any variables you need. fletcher posted:I might be alone on this, but I don't understand the point of doing that. It adds lines to the code and makes it less readable, in my opinion. I do it in a few places to do things like... code:
Finite fucked around with this message at 20:15 on Apr 4, 2008 |
# ¿ Apr 4, 2008 20:10 |
|
duz posted:You're supposed to use braces when you want the contents of a variable. ... ... I had no idea you could do that. fletcher posted:Sorry, that was a bit too simple of an example. I usually do it when I'm trying to put several variables in a string, I find it more readable.
|
# ¿ Apr 4, 2008 21:27 |
|
I'm teaching myself how to use mySQLi and prepared statements. I'm having difficulty finding out how to insert a varied amount of rows in one statement, and Google isn't helping. I used to do something like this:code:
php:<? $stmt = $mysqli->prepare("INSERT INTO test VALUES (?, ?)"); $stmt->bind_param('ds', $number, $text); $number = 1; $text = 'one'; $stmt->execute(); $stmt->close(); ?>
|
# ¿ Jun 12, 2008 16:12 |
|
I'm in the process of finishing up a website for an estate agent, and a small issue has cropped up during testing. My client has the ability to upload floorplans to their website, which get resized automatically by the code I've written. However, a lot of these files come with a large amount of whitespace around the edge. I'm using GD2 to do the resizing, and I can't find any obvious way to detect large areas of whitespace and crop it outside of imagecolorat(), a loop, and probably too much processing time. Is this going to be a plausible solution? Is there a better way of doing it? Should I just advise they crop off large areas of whitespace in Photoshop before uploading (they'd be comfortable doing this)?
|
# ¿ Feb 1, 2010 13:56 |
|
|
# ¿ May 8, 2024 21:05 |
|
Sebbe posted:This isn't GD2, but if you have access to ImageMagick, perhaps this script could be useful: autotrim This was a bit too vicious for what I had in mind, so after some tests I wrote my own to see if I could make it better. Ended up as far too intensive for what I had in mind, so pre-cropping the images turns out to be the more sensible solution aside from writing a little client-side uploader to help them out. Too much work for a small problem, so I've abandoned the idea. Thanks for the link though.
|
# ¿ Feb 4, 2010 10:46 |