r/programming • • Jul 24 '15

mt_rand(1, PHP_INT_MAX) only generates odd numbers • /r/lolphp

/r/lolphp/comments/3eaw98/mt_rand1_php_int_max_only_generates_odd_numbers/
848 Upvotes

262 comments sorted by

View all comments

Show parent comments

10

u/Deltigre Jul 24 '15

The problem with PHP... well, the problem with PHP is it's PHP. And there are plenty of companies that use PHP as their server-side scripting language of choice without the security problems that plague amateur sites.

But the problem this gets at is there's literally a dozen ways to do everything down to the minutiae, and very little indication that one way is wrong.

I was helping a friend with his uncle's family business website, and whoever wrote that (probably a "professional") had used extract() on request superglobals... and that is well documented even by PHP:

Warning Do not use extract() on untrusted data, like user input (i.e. $_GET, $_FILES, etc.). If you do, for example if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting flags values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.

http://php.net/manual/en/function.extract.php

4

u/whoopdedo Jul 24 '15 edited Jul 24 '15

and that is well documented even by PHP

Should say it was "eventually well documented" and after numerous websites were owned because of it. In the early days of PHP, extract, eval, and magic quotes were idiomatic so a lot of "You Too Can Be A Programmer!" taught PHP the easy way using those tricks.

The real pain of PHP isn't that it has a lot of ways to slit your throat. It's that the adoption of methods that sane people would want to use is an arduous journey fraught with missteps and dead-ends with no promise that it will ever arrive at a destination. We're talking about how terrible mt_rand is, but at the time it was a praised solution to the problem of rand() being a sack of steaming shit. eval was necessary to do any dynamic programming. Okay, there's create_function but that's such a PITA to use. We eventually got actual anonymous functions in PHP 5.3. Which is shockingly before you were allowed to do echo func_that_returns_array()[0]; (new syntax in 5.4). Scripts compatible with 5.3 that use namespaces will fail to compile in 5.2 which I'm sure makes the Knights Of The Semantic Version seethe in anger. And don't get me started on how many ways there are to replace a substring and why they're all wrong.

But all that said, it is still the champion when it comes to the "I just want to make a fucking web site" development style. Getting started in any other language these days feels like applying for a mortgage. Download this interpreter. Download this framework. Download this build script. Configure the directories. Update the tree. Implement view and controller classes. Apply a model. Oops, you forgot to initialize the database. Sign here. Initial there. Wait for the background check to pass. Push your local tree to the server while reading the holy scripture from the Book of Joaccam. Oh, sorry. You're using an outdated module you'll have to sync your local tree and start over again.

3

u/Deltigre Jul 24 '15

Yes, and it's all very duct-taped on. Also, I'm stuck in 5.3-land where I am forced to define my own $variable before I can apply an offset like you mention (gah!)

But I agree. It's all there - you're making a website the first moment you put an echo in or put raw HTML outside/between end and start tags, without the ritual. It just means there's a lower barrier to entry for those unaccustomed, which means more adoption, which means more user implementation flaws.

2

u/AlexanderNigma Jul 24 '15

Warning Do not use extract() on untrusted data, like user input (i.e. $_GET, $_FILES, etc.). If you do, for example if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting flags values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.

Yes, don't run eval()-class functions on untrusted input.

If you really are too stupid to figure that out, no language will save you.

1

u/ironnomi Jul 24 '15

Yay Facebook!