Post B5eqgEXqHmBucJ7ils by Edent@mastodon.social
(DIR) More posts by Edent@mastodon.social
(DIR) Post #B5eVMliz7I8ah1DElU by blog@shkspr.mobi
0 likes, 0 repeats
You can parse an .env file as an .ini with PHP - but there's a catchhttps://shkspr.mobi/blog/2026/04/you-can-parse-an-env-file-as-an-ini-with-php-but-theres-a-catch/The humble .env file is a useful and low-tech way of storing persistent environment variables. Drop the file on your server and let your PHP scripts consume it with glee.But consume it how? There are lots of excellent parsing libraries for PHP. But isn't there a simpler way? Yes! You can use PHP's parse_ini_file() function and it works.But….env and .ini have subtly different behaviour which might cause you to swear at your computer.Let's take this example: ENV# This is a commentUSERNAME="edent"Run $env = parse_ini_file( ".env" ); and you'll get back an array setting the USERNAME to be "edent". Hurrah! Works perfectly. Ship it!But consider this: ENV# This is a commentUSERNAME="edent" # Don't use an @ symbol here.It will happily tell you that the username is "edent# Don"WTAF?Here's the thing. The comment character for .ini is not # - it's the semicolon ;Let me give you some other examples of things which will fuck up your parsing: ENV# Documentation at https:/example.com/?doc=123DOCUMENTATION=123# Set the passwordPASSWORD=qwerty;789That gets us back this PHP array: PHP[ '# Documentation at https:/example.com/?doc' => '123', 'DOCUMENTATION' => '123', 'PASSWORD' => 'qwerty',];When the .ini is parsed, it ignores every line which doesn't have an = sign. It also treats literal semicolons as the start of a new comment until they're wrapped in quotes.My code highlighter should show you how it is parsed: INI# Documentation at https:/example.com/?doc=123DOCUMENTATION=123# Set the passwordPASSWORD=qwerty;789It gets worse. Consider this: ENV# Set the "official" nameREALNAME="Arthur, King of the Britons"That immediately fails with PHP Warning: syntax error, unexpected '"' in envtest on line 1You can use single quotes in pseudo-comments just fine, but if the ini parser sees a double quote without an equals then it throws a wobbly.I'm sure there are several other gotchas as well. For example, there are certain reserved words and symbols you can't used as a key.This will fail: ENV# Can we fix it? Yes we can!FIX=trueIt chokes on the exclamation point.How to solve it (the stupid way)The comments on an .env file start with a hash.The comments on an .ini file start with a semicolon.So, it is perfectly valid for a hybrid file to have its comments start with #;Look, if it's stupid but it works…What Have We Learned Here Today?There's a right way and a wrong way to do .env parsing.The wrong way works, up until the point it doesn't.You should probably use a proper parser rather than hoping your .env looks enough like an .ini to pass muster.On next week's show - why you shouldn't store your passwords inside a JPEG!#php
(DIR) Post #B5eVMm4xnZibnBUn1U by mirabilos@toot.mirbsd.org
0 likes, 0 repeats
@blog ouch, that’s scary. And WDYM store passwords in a JPEG?! 🙀The Fedi HTML doesn’t render well: it contains tt (or equivalent) but either not pre or something trashes whitespace, see attached photo.
(DIR) Post #B5eng3gENfFj3fGvmC by Edent@mastodon.social
0 likes, 0 repeats
@mirabilos It renders correctly on Mastodon. Which Fediverse service are you using? You might need to raise a bug with them.
(DIR) Post #B5eng3wXP2IRsEtxC4 by mirabilos@toot.mirbsd.org
0 likes, 0 repeats
@Edent hm, this is fun. Some are correct, some not. The one with the PHP => output is correct, the one before that not.I get raw from your instance:[…] your parsing:</p><pre><span class="tempest-highlight-language"><img src="https://shkspr.mobi/blog/wp-content/plugins/tempest-highlight/svg/notepad.svg" width="32" height="32" alt=""><span> ENV</span></span><code><span># Documentation at https:/example.com/?doc=123</span><span>DOCUMENTATION</span>=123<span># Set the password</span><span>PASSWORD</span>=qwerty;789</code></pre><p>That gets […](this is with all newlines etc. intact, straight AP JSON-LD from your instance)As you can see, there are in fact no newlines between the spans for the lines.Similarily here:[…]<p>This will fail:</p><pre><span class="tempest-highlight-language"><img src="https://shkspr.mobi/blog/wp-content/plugins/tempest-highlight/svg/notepad.svg" width="32" height="32" alt=""><span> ENV</span></span><code><span># Can we fix it? Yes we can!</span><span>FIX</span>=true</code></pre><p>It chokes[…]If it renders correctly in Mastodon, that is an accident and not contained anywhere in the AP JSON.
(DIR) Post #B5eqgEXqHmBucJ7ils by Edent@mastodon.social
0 likes, 0 repeats
@mirabilos I'll take a look when I sober. Probably Monday.Thanks for the bug report 🙂
(DIR) Post #B5eqgEorGVnnT55JIG by mirabilos@toot.mirbsd.org
0 likes, 0 repeats
@Edent you’re welcome, and no worries about the timeframe.Access is simply an HTTP client with a suitable Accept header, piped through jq -r .content.