{"id":26871273,"url":"https://github.com/codeadamca/php-env","last_synced_at":"2025-05-07T06:29:23.758Z","repository":{"id":115328422,"uuid":"534764907","full_name":"codeadamca/php-env","owner":"codeadamca","description":"A basic function to load variables from a .env file.","archived":false,"fork":false,"pushed_at":"2025-01-26T21:38:26.000Z","size":23,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T07:18:54.541Z","etag":null,"topics":["environment-variables","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeadamca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-09-09T18:40:31.000Z","updated_at":"2025-03-02T09:19:10.000Z","dependencies_parsed_at":"2023-10-22T14:00:22.943Z","dependency_job_id":null,"html_url":"https://github.com/codeadamca/php-env","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-env","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-env/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-env/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fphp-env/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeadamca","download_url":"https://codeload.github.com/codeadamca/php-env/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252826494,"owners_count":21810121,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["environment-variables","php"],"created_at":"2025-03-31T07:18:57.508Z","updated_at":"2025-05-07T06:29:23.739Z","avatar_url":"https://github.com/codeadamca.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hiding PHP Environment Varaibles\n\nA basic vanilla PHP function to load variables from an `.env` file.\n\nWhen you are creating a basic vanilla PHP project and want to keep your database credentials and/or API key out of your GitHub repository you can use a `.env` file. \n\n1. Create a file called `.env` and place it in your project folder.\n\n2. Put your actual database credentials and API keys in your `.env``file:\n    \n    ```php\n    DB_HOST=\u003cDB_HOST\u003e\n    DB_DATABASE=\u003cDB_DATABASE\u003e\n    DB_USERNAME=\u003cDB_USERNAME\u003e\n    DB_PASSWORD=\u003cDB_PASSWORD\u003e\n    \n    API_SECRET=real_api_key\n    ```\n\n3. Create a second copy of your `.env` file and name it `.env.sample`. Use this file to provide instructions to the next programmer using your project code:\n    \n    ```php\n    DB_HOST=localhost\n    DB_DATABASE=database\n    DB_USERNAME=username\n    DB_PASSWORD=password\n    \n    API_SECRET=api_key\n    ```\n\n4. Make sure your GitHub repo does not include the file named `.env`. In your `.gitignore` file add:\n    \n    ```\n    .env\n    ```\n\n\u003e [!Note]\n\u003e I have excluded this step in this repo so you can view both the `.env` and `.env.sample` files. \n\n5. If your `.env` file is accessible from a browser, use a `.htaccess` file to hide your `.env` file:\n    \n    ```sh\n    \u003cFiles .env\u003e\n    order allow,deny\n    Deny from all\n    \u003c/Files\u003e\n    ```\n\n6. In your database connection file, add this function to load the variables from the `.env` file, and place them in matching constants:\n    \n    ```php\n    $env = file(__DIR__.'/.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);\n    \n    foreach($env as $value)\n    {\n      $value = explode('=', $value);\n      define($value[0], $value[1]);\n    }\n    ```\n\n7. Finally, you can use the contants in your database connection and any API code:\n    \n    ```php\n    $connect = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);\n    ```\n\n***\n\n## Repo Sesources\n\n* [Visual Studio Code](https://code.visualstudio.com/) \n* [PHP](https://php.net)\n\n\u003cbr\u003e\n\u003ca href=\"https://codeadam.ca\"\u003e\n\u003cimg src=\"https://cdn.codeadam.ca/images@1.0.0/codeadam-logo-coloured-horizontal.png\" width=\"200\"\u003e\n\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fphp-env","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeadamca%2Fphp-env","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fphp-env/lists"}