Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Finite
Jan 9, 2003

What do you mean mullets aren't fashionable?!?
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

Adbot
ADBOT LOVES YOU

Finite
Jan 9, 2003

What do you mean mullets aren't fashionable?!?

duz posted:

It looks like you're using a database abstraction layer. Use its built in prepared query functions instead of string concatenations.
It also looks like you're using PEAR::DB, any chance you can upgrade to MDB2?

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.

Also your example seems really strange since you are mixing oop and procedural function calls.

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

Finite
Jan 9, 2003

What do you mean mullets aren't fashionable?!?

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'
?>
I'm not sure if this is the best way, but eval is something that is generally frowned upon.
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:
$colour = $data['colour'];
$string = "The lazy $colour fox jumps over the I can't remember the rest of this."
But I decided that was a little stupid the other day and I'm trying to use sprintf instead.

Finite fucked around with this message at 20:15 on Apr 4, 2008

Finite
Jan 9, 2003

What do you mean mullets aren't fashionable?!?

duz posted:

You're supposed to use braces when you want the contents of a variable.

php:
<?
$string = "The quick {$data['colour']} fox jumps over the lazy dogs."
?>

...

...

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.

Finite
Jan 9, 2003

What do you mean mullets aren't fashionable?!?
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:
INSERT INTO
x	(a, b)
VALUES	(1, 'one'),
	(2, 'two'),
	(3, 'three');
But with a prepared statement I don't see how to do this, as the code below (shamelessly stolen from PHPs docs) doesn't allow for more that one group of values:

php:
<?
$stmt = $mysqli->prepare("INSERT INTO test VALUES (?, ?)");
$stmt->bind_param('ds', $number, $text);

$number = 1;
$text = 'one';

$stmt->execute();
$stmt->close();
?>
As much as I could rewrite this for 2 or 3 rows, anything more than that just gets daft. How should I be going about this?

Finite
Jan 9, 2003

What do you mean mullets aren't fashionable?!?
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)?

Adbot
ADBOT LOVES YOU

Finite
Jan 9, 2003

What do you mean mullets aren't fashionable?!?

Sebbe posted:

This isn't GD2, but if you have access to ImageMagick, perhaps this script could be useful: autotrim

I have no idea how much processing time it requires, however.

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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply