Welcome Guest, Not a member yet? Register   Sign In
What is the best place to store app variables?
#1

Hi,
I am wondering what is the best practice to store app variables? For example API keys.
  • Is it in the app/Config/Constants.php file?
  • Or directly into the Controller where you use them?
  • Or is it in the .env file and then you can access it using getenv() where you need it?

I am working on implementing the Stripe payment and I have found tutorials that either create a constant, or put the keys directly into the controller and I wonder if there is a reason for choosing one or the other? 
The idea to put them into .env is an added thought for the case when for instance, you need a different value depending on your environnement. I use in my projects env-staging and env-production to store some already, and depending on where I deploy, I create the .env file from one or the other. 
Thanks for sharing your thoughts
Reply
#2

You should set the secret keys in Environment Variables.

If you set in app/Config/Constants.php or Controllers, all the people who can access the source code
can get the secret keys.
Reply
#3

(10-08-2022, 02:49 AM)kenjis Wrote: If you set in app/Config/Constants.php or Controllers, all the people who can access the source code
can get the secret keys.
Oh that's indeed a good point. Thanks Smile
Reply
#4

(10-08-2022, 02:49 AM)kenjis Wrote: You should set the secret keys in Environment Variables.

If you set in app/Config/Constants.php or Controllers, all the people who can access the source code
can get the secret keys.

But won't the same hacker have access to the .env file?
Reply
#5

(10-14-2022, 12:47 AM)jetspeed Wrote:
(10-08-2022, 02:49 AM)kenjis Wrote: You should set the secret keys in Environment Variables.

If you set in app/Config/Constants.php or Controllers, all the people who can access the source code
can get the secret keys.

But won't the same hacker have access to the .env file?

If CI has been installed the correct way a .env file is just as save, the main difference is if the .env is publicly available it's readable PHP cannot be read by default (still possible with wrong configuration on server) it's processed.
Reply
#6

You don't need to use .env file. You need to set environment variables.
If you don't use .env file, the hacker cannot read .env file.
Reply
#7

@kenjis how do you do that without using .env file? I am a bit confused with that option
Reply
#8

Set environment variables on your server.

For example, on heroku you can set them from CLI command or Dashboard:
https://devcenter.heroku.com/articles/co...onfig-vars

Environment variables were used before .env file was invented.
.env file is a way to set environment variables easily for developers.
Reply
#9

I see. Thanks Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB