Welcome Guest, Not a member yet? Register   Sign In
How to set variable in .env file programity
#1

Hello,
I want to set database variable and some custom variable like license key to my .env file programmity. How can I do that?
Reply
#2

(This post was last modified: 04-17-2023, 10:23 AM by Chivinsdev.)

Hello friend,
Use this small snippet I wrote. I hope it will do exactly what you want I am currently using it in my premium script.

PHP Code:
if (!function_exists('set_env')) {
    function set_env($key$value)
    {
        $path ROOTPATH '.env';

        $content file_get_contents($path);

        if (!str_contains($content$key)) {

            $content "\n" "$key = " $value;
        }

        file_put_contents($pathstr_contains($content'# ' $key) ? str_replace('# ' $key$key$content) : $content);

        $content file_get_contents($path);

        if (is_bool(env($key))) {
            $old env($key) ? 'true' 'false';
        } elseif (env($key) === null) {
            $old 'null';
        } else {
            $old env($key);
        }

        $content str_replace("$key = " $old"$key = " $value$content);

        if (file_exists($path)) {
            file_put_contents($path$content);
        }
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB