Live data from Hacker News

PHP: md5('240610708') == md5('QNKCDZO')

3v4l.org

161–170 of 185 posts

Re: PHP: md5('240610708') == md5('QNKCDZO')

#161
post #85

Earlier quoted context omitted.

Once I wrote a little PHP application to manage a clan in a browser game. I used an MD5 hash as session id that I checked with if(session_id) When users started reporting that their logins would sometimes not work at the first time, I found out that strings that start with zero are coerced to 0 and then interpreted as false. Never used PHP for anything important since.

What did you use for the important stuff that was 100% predictable?

zeroes and ones.

Re: PHP: md5('240610708') == md5('QNKCDZO')

#162
post #96

Earlier quoted context omitted.

Ahh PHP, the language where true == false php > if ((true == "foo") && ("foo" == 0) && (0 == false)) echo "yay!"; yay!

I've never seen one, but somewhere there must surely be a PHP version of the infamous 'WAT' talk about JavaScript, full of examples like this and the "2d9"->"2e0"->3 example mentioned by lars.

There is a blogpost: http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/

Re: PHP: md5('240610708') == md5('QNKCDZO')

#163
post #136

Earlier quoted context omitted.

"This sort of thing happens in type conversion languages. You can either use === to stop conversion or you can understand how conversion works." Even JavaScript isn't insane enough to somehow coerce a string to 0.

Yes, JavaScript will convert strings into numbers console.log(5*"12"); 60 console.log(5*"0x0C"); 60

Actually, sometimes type conversion make some code become a little bit handy.

We use Java at the backend and of course Javascript for frontend. When serializing, in Java we should

        String dataRaw = "42";
        int objectId = Integer.parseInt(dataRaw);
Meanwhile, in JS, it is fairly simple:

        dataRaw = "42";
        var objectid = +dataRaw;

Re: PHP: md5('240610708') == md5('QNKCDZO')

#164

I'm not exactly clear on how PHP == works, but you can see the MD5 for yourself: $ echo -n 240610708 | md5sum 0e462097431906509019562988736854 - $ echo -n QNKCDZO | md5sum 0e830400451993494058024219903391 - $ echo -n aabg7XSs | md5sum 0e087386482136013740957780965295 - All of them start with 0e, which makes me think that they're being parsed as floats and getting converted to 0.0. This is why "magic" operators like =…

Type coercion is fine so long as you recognize it as the syntactic sugar that it is. JS and PHP support easy type coercion because HTTP is string-only and it would be a pain in the ass to explicitly cast every value you get over the wire. You just have to be sure that, when you use it, you do so intentionally and not out of laziness.

Here's what I took away::

It's pain in the ass to validate and sanitize your input.

Re: PHP: md5('240610708') == md5('QNKCDZO')

#165
post #98

Earlier quoted context omitted.

Once I wrote a little PHP application to manage a clan in a browser game. I used an MD5 hash as session id that I checked with if(session_id) When users started reporting that their logins would sometimes not work at the first time, I found out that strings that start with zero are coerced to 0 and then interpreted as false. Never used PHP for anything important since.

To be fair, this kind of thing (maybe not exactly this, but type-coercion bugs) can happen in JavaScript, which is all the rage now for "important" stuff.

This is levels worse than what Javascript does though. Most high-level languages have some sort of implicit coercion (even python lets you do truth tests on non-boolean values). The problem here is the programmer isn't confused about types at all. They're comparing two things of the same type: two strings! Nevertheless, given two strings PHP tries to coerce them into ints before carrying out the equality test. Yes, you will have coercion bugs in other languages if you're testing things of different types, but I don't know any other language where a equality test between two things of the same type are automatically coerced into another.

Re: PHP: md5('240610708') == md5('QNKCDZO')

#166
PHP : 1 week is not always 7 days:

  $_1week = new DateInterval("P1W");
  $_7days = new DateInterval("P7D");
  var_dump($_1week == $_7days); // true
  var_dump($_1week);
  var_dump($_1week == $_7days); // false
  var_dump($_7days);
  var_dump($_1week == $_7days); // true
http://3v4l.org/CcAk8

Same result with '$_1week = new DateInterval("P7D");' :-)

Re: PHP: md5('240610708') == md5('QNKCDZO')

#167
How about ==== and ===== and ======?

For security reason, I suggest PHP to implement such operators... :D Example:

"abc" === 'abc'; # ==> true

"abc" ==== 'abc'; # ==> false, single-quote vs double-quote

"abc" ===== 'abc'; # ==> true, this is how it works

j.k :D

Re: PHP: md5('240610708') == md5('QNKCDZO')

#169
post #87

Earlier quoted context omitted.

perhaps ==== operator must reserved

php_real_equivalence_4()

Exactly!

But it must invoke with additional NULL-parameter to achieve real effect and analyse return value for TRUE, FALSE, NULL:

  php_real_equivalence_4($x, $y, null);
Post reply on HN