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
Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
Having coded in PHP with little abstract knowledge of language standards, I'm guessing what y'all are saying is, if it returns an integer, it should always return an integer rather than switching to a boolean? Or throw an exception, which as we know PHP just doesn't do for its internal functions.

Adbot
ADBOT LOVES YOU

Optimus Prime Ribs
Jul 25, 2007

It could gently caress up for anyone who doesn't know about PHP's quirks:

PHP code:
<?php
$mystr = "butts are awesome";

if (strpos($mystr, "butts") != false)
{
    echo "Won't ever reach me!";
}
?>
"butts" is at 0, which, without strict comparisons, is the same as false, so the if statement fails. :toot:

KuruMonkey
Jul 23, 2004

Golbez posted:

Having coded in PHP with little abstract knowledge of language standards, I'm guessing what y'all are saying is, if it returns an integer, it should always return an integer rather than switching to a boolean? Or throw an exception, which as we know PHP just doesn't do for its internal functions.

Ideally yes; from a logical point of view, a function should always return a value of a single type. (because logically, mathematically and "properly" a function maps a domain onto a co-domain, and those "should" be defined sets, and "integer and also sometimes FALSE" isn't a sensible set definition).

But you're right; PHP does not do this. (and its far from alone in that behaviour) Its actually "quite consistent" in that PHP returns "Value or FALSE" all over the place.

(rest of comment is not a direct reply to any particular post!)

As Optimus Prime Ribs points out, and Scaramouche fell into the trap of, that makes this the ONLY safe construct in such cases:

php:
<?
$x = "butt";
$y = "my butt";
// construct for test "is string x in string y?"
if(strpos($y, $x) !== FALSE) {
  // butt you say?
}
?>
And note that x === TRUE is NOT the logical inverse of x !== FALSE...

Its only fair to note that PHP is absolutely NOT AMBIGUOUS about this;

http://php.net/manual/en/function.strpos.php posted:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

And that is followed by a big red box with a warning sign, a title of "Warning" and bold text warning you about sometimes-boolean returns, and how to handle them properly.

On the one hand, yep; mixed return types are bad. On the other hand, the problem disappears if you read the loving manual!

Edit:

Basically if you're a lazy coder regarding types, then PHP will let you skip along happily for quite a while, before holding you down and publicly doing horrible things to you (by which I mean by breaking your code in a seemingly obscure and certainly humiliating way).

On the other hand a strict typed language like C will simply shout endlessly "you suck! gently caress off!" before deigning to run any of your code at all. But on the plus side; its doing that in the privacy of your own home, not 6 months after you published the code to the world!

The common defense, of course, is to not be lazy regarding types.

KuruMonkey fucked around with this message at 15:39 on Jul 20, 2012

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

KuruMonkey posted:

Ideally yes; from a logical point of view, a function should always return a value of a single type. (because logically, mathematically and "properly" a function maps a domain onto a co-domain, and those "should" be defined sets, and "integer and also sometimes FALSE" isn't a sensible set definition).

Why not? If we assume non-integer mathematical objects "true" and "false", then {integers} union {false} seems like a perfectly well-defined set (in the mathematical sense) to me. It is the set of integers, augmented with an extra element that is not an integer.

Perhaps it would be a good idea to define('NOT_FOUND', false), because then you can do if (search_function(...) === NOT_FOUND) { ... and the intention is clear. Of course, this does not solve the problem that you have to check for === equality. You could define a function not_found() to do it for you, that returns true if passed false, false if passed an integer, and throws an exception or whatever otherwise. Then you could do if (not_found(search_function(...))) { ..., at the cost of some overhead.

-------------------

I recall that "reset.css" files, to ensure that Internet Explorer displays pages similarly to other browsers, are or were popular. I wonder why nobody has made a "reset.php" to try to streamline some of the more visible oddities of PHP. By this I mean you could write a PHP script that for the most part just defines a set of functions as wrappers around the PHP builtins, but with a consistent naming scheme, argument order and so on and so forth, and with an attempt to logically collect together similar pieces of functionality rather than having eighteen hundred billion billion slightly different array functions. You could even have the search functions return -1 on not found, if you wanted.

Obviously this would be inefficient (because it would add the step of recompiling this reset file to every single page request, and because calling user-defined wrapper functions adds some overhead to your script). But it seems like it could smooth the user experience to some small degree.

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

I realize that a lot of it is my own expectation about what something should return; the fact that the function returns differing types based on the input is a bit strange but not 'OMG CODING HORROR'. I think what bothers me more is the loosy goosy nature of the comparisons.

MrEnigma
Aug 30, 2004

Moo!

Scaramouche posted:

I realize that a lot of it is my own expectation about what something should return; the fact that the function returns differing types based on the input is a bit strange but not 'OMG CODING HORROR'. I think what bothers me more is the loosy goosy nature of the comparisons.

Yeah, lots of languages do the -1/false response that will equate to a loose type match that is the same. Javascript has the same issue. It's not the method returning this that is the issue so much (the alternative is to throw an exception?), but the language that enables this to be an issue.

As long as you know about it, and can read the giant warning on php.net, you should be good.

putin is a cunt
Apr 5, 2007

BOY DO I SURE ENJOY TRASH. THERE'S NOTHING MORE I LOVE THAN TO SIT DOWN IN FRONT OF THE BIG SCREEN AND EAT A BIIIIG STEAMY BOWL OF SHIT. WARNER BROS CAN COME OVER TO MY HOUSE AND ASSFUCK MY MOM WHILE I WATCH AND I WOULD CERTIFY IT FRESH, NO QUESTION

Scaramouche posted:

I realize that a lot of it is my own expectation about what something should return; the fact that the function returns differing types based on the input is a bit strange but not 'OMG CODING HORROR'. I think what bothers me more is the loosy goosy nature of the comparisons.

I agree completely - I think that's why I was a bit confused about what you were saying. Even though the return type is mixed, I think it's okay in this instance because it does make sense to return a FALSE. The problem lies in the fact that 0 will still compare as false, I guess it pays to always use the strict comparisons - sucks for people just getting into the language though as it's a frequent gotcha that will continuously crop up.

Quote-Unquote
Oct 22, 2002



When learning PHP it confused the hell out of me that 0, false, "" and null all mean the same thing if you're comparing them with each other using == because I failed to read the manual. I realised this because of strpos, too, which I'm sure a lot of people did. And also:
"banana" == 0
"banana" != false
"banana" != null
"banana" !
"banana" !== 0

This caught me out once and I didn't think it was obvious in the documentation. A string that doesn't have a decimal point, an e or an E in it == 0, but obviously !== 0 since it's a different type. Easy to make a mistake on this.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

President Anime 2008 posted:

When learning PHP it confused the hell out of me that 0, false, "" and null all mean the same thing if you're comparing them with each other using == because I failed to read the manual. I realised this because of strpos, too, which I'm sure a lot of people did. And also:
"banana" == 0
"banana" != false
"banana" != null
"banana" !
"banana" !== 0

This caught me out once and I didn't think it was obvious in the documentation. A string that doesn't have a decimal point, an e or an E in it == 0, but obviously !== 0 since it's a different type. Easy to make a mistake on this.

The best part is that "==" is not transitive.

quote:

"php" == 0 => true
0 == null => true
null == "php" => false
To which I must again respond: :catstare:

Fluue
Jan 2, 2008
I'm almost done transitioning my hobby project to PDO after the announcement that mysql_ queries are being depreciated.

I'm having one problem with a certain query, though. Basically I'm keeping track of the number of generated items by doing

code:
UPDATE stats SET total_generated=total_generated+:totalgen
Where :totalgen will be a variable that contains the number of generated items for this particular instance.

I'd really like to keep it like this to reduce the number of SQL queries, but if PDO doesn't want to cooperate, I'm open to suggestions. Here's the specific code block I'm talking about for reference:

php:
<?
    $total_gen_update = $db->prepare("UPDATE stats SET total_generated=total_generated+:totalgen");
    $total_gen_update->bindValue(':totalgen', $stats_generated, PDO::PARAM_INT);
    $total_gen_update->execute();
?>

McGlockenshire
Dec 16, 2005

GOLLOCKS!
What, exactly, is going wrong?

It's been a while since I used named placeholders, but that looks perfectly correct...

Fluue
Jan 2, 2008
When I run the page it doesn't submit the updated count. I'll check over it again to make sure I didn't make some typo, though.

PleasantDilemma
Dec 5, 2006

The Last Hope for Peace
Doesn't that query need a WHERE clause?
Also, PDO provides error reporting. you can add something like this:

php:
<?
    $total_gen_update = $db->prepare("UPDATE stats SET total_generated=total_generated+:totalgen");
    $total_gen_update->bindValue(':totalgen', $stats_generated, PDO::PARAM_INT);
    $r = $total_gen_update->execute();
    if( $r === false ) {
        print_r( $total_gen_update->errorInfo() );
    }
?>

Sab669
Sep 24, 2009

Unfortunately I can't get XAMPP running on my machine at the moment, but a group mate is having some issues with this project we're working on.

I downloaded some SQL script that creates a table for zipcodes, and it has a text file which the SQL file tries to perform a bulk insert from. When he tries to run the bulk insert command in phpmyadmin, it fails. Is this because it's trying to import from C:\zip.txt? To me, I don't think it would be able to access a local file like that, but I have no idea.

Also the table the SQL script creates are all varchars, but none of the values in the text file have quotes around them. Could that be an issue?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
It's probably doing a LOAD DATA INFILE. I'm pretty sure phpMyAdmin has a way to "import" csv files where you can upload the csv and then set the LOAD DATA INFILE parameters, I would just use that.

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

This sounds like the MaxMind geoip database, is it? If so this might help:
http://bartomedia.blogspot.ca/2007/11/maxmind-geoip-setup-tutorial-using.html

Izanagi
May 25, 2012

There are people who do and people who wish they had.

Sab669 posted:

Unfortunately I can't get XAMPP running on my machine at the moment, but a group mate is having some issues with this project we're working on.

I downloaded some SQL script that creates a table for zipcodes, and it has a text file which the SQL file tries to perform a bulk insert from. When he tries to run the bulk insert command in phpmyadmin, it fails. Is this because it's trying to import from C:\zip.txt? To me, I don't think it would be able to access a local file like that, but I have no idea.

Also the table the SQL script creates are all varchars, but none of the values in the text file have quotes around them. Could that be an issue?

Sab you need to ensure that you have told phpmyadmin which character denotes a new line. You could also load the txt file into excel or your favourite spreadsheet maker and save it as a csv.

OR

INSERT INTO yourttable (columnname) VALUES (Value1), (Value2), (Value3)

Sab669
Sep 24, 2009

It was just a thing I downloaded off Google, can't seem to find it anymore but I know the script included the line delimiter. If he's still having an issue when I see him next week however, yea, I'll probably just throw it into excel and CSV it.

zorch
Nov 28, 2006

I'm using json_decode on a json file that contains both empty values and null values, does anyone know why php would read both of them as null?

Running the empty() function on both of them returns true and if I do if($variable == null) they're both match the condition.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

emoltra posted:

I'm using json_decode on a json file that contains both empty values and null values, does anyone know why php would read both of them as null?

Running the empty() function on both of them returns true and if I do if($variable == null) they're both match the condition.

It's because PHP. Try to see if isset() works for you.

Mister Chief
Jun 6, 2011

The documentation for empty clearly states that null and "" are deemed to be empty. Why do you have values that way to begin with? Try isset.

Deus Rex
Mar 5, 2005

Mister Chief posted:

The documentation for empty clearly states that null and "" are deemed to be empty. Why do you have values that way to begin with?

Null and the empty string are semantically different things.

McGlockenshire
Dec 16, 2005

GOLLOCKS!

emoltra posted:

Running the empty() function on both of them returns true and if I do if($variable == null) they're both match the condition.

== is a similarity construct, and as has been noted by others '' == null. You probably want to use either === for identity here or to use is_null()

Ashex
Jun 25, 2007

These pipes are cleeeean!!!
This is a bit of a stretch, but does anyone have experience with multi-domain authentication against AD-LDAP? The forest shares a common root domain so I can hit all of them with the global catalog.

The problem we're running into is we're trying to use the supplied user credentials to authenticate, however because we don't know what domain the user is in we can't just append it (@blah.blah.com) to the user value.

I could use a ldap service account is look up the UPN from the username then use it with the supplied credentials to authenticate but that seems a bit messy and I suspect there would be some lag.

DarkLotus
Sep 30, 2001

Lithium Hosting
Personal, Reseller & VPS Hosting
30-day no risk Free Trial &
90-days Money Back Guarantee!

Ashex posted:

This is a bit of a stretch, but does anyone have experience with multi-domain authentication against AD-LDAP? The forest shares a common root domain so I can hit all of them with the global catalog.

The problem we're running into is we're trying to use the supplied user credentials to authenticate, however because we don't know what domain the user is in we can't just append it (@blah.blah.com) to the user value.

I could use a ldap service account is look up the UPN from the username then use it with the supplied credentials to authenticate but that seems a bit messy and I suspect there would be some lag.

If you're in a multi-domain environment, why not just present the user with a select box to choose the domain their user account is part of? I've seen this method used many times even for AD integrated services and portals.

IT Guy
Jan 12, 2010

You people drink like you don't want to live!
I'm going to be implementing user authentication for the first time and while I could write my own, I really don't want to. What are some of the more popular user auth libraries out there?

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.

IT Guy posted:

I'm going to be implementing user authentication for the first time and while I could write my own, I really don't want to. What are some of the more popular user auth libraries out there?

Well, to whore out my own product a bit, you could use my API: https://accthub.com. It's for storing user accounts, addresses, and performing authentication against them. It's geared towards mobile developers, but there's really no reason why you couldn't use it for a web app (ironically, it was originally built for web apps).

As for actual libraries you can install yourself, sorry, can't help you there.

IT Guy
Jan 12, 2010

You people drink like you don't want to live!

musclecoder posted:

Well, to whore out my own product a bit, you could use my API: https://accthub.com. It's for storing user accounts, addresses, and performing authentication against them. It's geared towards mobile developers, but there's really no reason why you couldn't use it for a web app (ironically, it was originally built for web apps).

As for actual libraries you can install yourself, sorry, can't help you there.

I appreciate your opinion. However, I'm looking for something tried and true. Your product looks to be in a beta which I'm hesitant about. In addition, I want to store user's credentials in my own database (from what I see your product means I'd be storing stuff in the cloud?), preferably with some type of bcrypt hash.

Sorry, I'm sure your product is fine and I don't mean to offend you, I'm just looking for something I can implement into my current database.

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.

IT Guy posted:

I appreciate your opinion. However, I'm looking for something tried and true. Your product looks to be in a beta which I'm hesitant about. In addition, I want to store user's credentials in my own database (from what I see your product means I'd be storing stuff in the cloud?), preferably with some type of bcrypt hash.

Sorry, I'm sure your product is fine and I don't mean to offend you, I'm just looking for something I can implement into my current database.

Thanks for checking it out and completely understood. Just thought I'd throw it out there.

If you do find a solution, let me know. I haven't seen any open source auth system that uses bcrypt.

IT Guy
Jan 12, 2010

You people drink like you don't want to live!

musclecoder posted:

Thanks for checking it out and completely understood. Just thought I'd throw it out there.

If you do find a solution, let me know. I haven't seen any open source auth system that uses bcrypt.

I ended up just writing my own and storing passwords using a per application (not per user) salt and SHA256 hash. My site is an internal web application that isn't available to the WAN so it should be fine for our needs.

Mister Chief
Jun 6, 2011

IT Guy posted:

I ended up just writing my own and storing passwords using a per application (not per user) salt and SHA256 hash. My site is an internal web application that isn't available to the WAN so it should be fine for our needs.

Check out PHPass. It uses bcrypt and is used by a lot of major CMSs to hash their passwords.

McGlockenshire
Dec 16, 2005

GOLLOCKS!
Just be sure to kick it out of "compatible" mode, during which it falls back to md5.

IT Guy
Jan 12, 2010

You people drink like you don't want to live!

Mister Chief posted:

Check out PHPass.

Awesome, looks like what I was after, thanks.

Sab669
Sep 24, 2009

I think choosing PHP for our language of choice for my senior project was not a good idea. I am not strong enough to carry this group :suicide:

Rahu
Feb 14, 2009


let me just check my figures real quick here
Grimey Drawer
I'm having some trouble with unicode-related nonsense in a PHP project I'm working on. I have a mysql database storing a bunch of stuff, including unicode strings. Using phpmyadmin or the command line mysql utility display these strings correctly.

When I try to pull them from the database to display on a page, all of the non-ascii characters are replaced with �.

I've tried making sure PHP is set to use a unicode character set and is actually sending utf-8 by putting this at the beginning of the page:
code:
header("Content-Type: text/html; charset=utf-8");
ini_set("default_charset", 'utf-8');
I'm also not manipulating this string in any way before displaying it, just echoing exactly what statement->bind_result()/statement->fetch() is returning.

Thanks for any assistance :)

Rahu fucked around with this message at 06:57 on Aug 2, 2012

McGlockenshire
Dec 16, 2005

GOLLOCKS!
MySQL? Chances are that you need to set the database connection character set to match that of the data in the tables. Also double-check that the character set on the tables and columns matches what you expect.

If that still isn't doing the trick, echo the string using urlencode. This'll give you the actual bytes that you're pulling down from the database, which you can compare to what the expected unicode code points should be.

Mister Chief
Jun 6, 2011

You could try something like htmlspecialchars. http://php.net/manual/en/function.htmlspecialchars.php

Rahu
Feb 14, 2009


let me just check my figures real quick here
Grimey Drawer

McGlockenshire posted:

MySQL? Chances are that you need to set the database connection character set to match that of the data in the tables. Also double-check that the character set on the tables and columns matches what you expect.

If that still isn't doing the trick, echo the string using urlencode. This'll give you the actual bytes that you're pulling down from the database, which you can compare to what the expected unicode code points should be.

That was it, thanks. I never considered the character set of the connection :downs:

Rahu fucked around with this message at 07:31 on Aug 2, 2012

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.

Mister Chief posted:

Check out PHPass. It uses bcrypt and is used by a lot of major CMSs to hash their passwords.

PHP 5.3 supports bcrypt natively with the crypt() method. I'd use that instead and wrap it around a small class. PHPass is nice, but it's some very old PHP4 code.

Adbot
ADBOT LOVES YOU

Mister Chief
Jun 6, 2011

Here's a tutorial which demonstrates how to do the same things that PHPass does except with the new crypt functions.

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