12 Replies - 338 Views - Last Post: 02 January 2018 - 10:21 AM Rate Topic: -----

#1 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 57
  • Joined: 24-February 15

Variable environment(php) and .gitignore

Posted 30 December 2017 - 01:40 PM

Can someone help me with creating a variable environment or knows a step by step guide?
If I set up this environment it means that I need put the connection in my main PHP file and connect with
'getenv' and the config.php in this environment map?

This post has been edited by [email protected]: 30 December 2017 - 01:41 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Variable environment(php) and .gitignore

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5121
  • View blog
  • Posts: 13,790
  • Joined: 18-April 07

Re: Variable environment(php) and .gitignore

Posted 30 December 2017 - 01:56 PM

Are you talking about setting variables inside the PHP server environment like putenv() and getenv() or are you talking about creating your own custom configuration file that you can set values and pull from as needed (a config.php file) like they do in Laravel, Joomla, Wordpress etc? Are you building a framework?
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 57
  • Joined: 24-February 15

Re: Variable environment(php) and .gitignore

Posted 30 December 2017 - 02:36 PM

To explain my problem, I wanna put my map with my config file on Github. If I do that people can download my files and access my database I don't want that. But if I remove the config file from my map you can't connect anymore to your database. So I need to store my credentials in a variable environment, that's what I understood. But I'm new to this so I don't really know how to get the job done if you understand what I mean.
Was This Post Helpful? 0
  • +
  • -

#4 benanamen  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 143
  • Joined: 28-March 15

Re: Variable environment(php) and .gitignore

Posted 30 December 2017 - 03:30 PM

If you are developing on your local system the DB credentials are generally localhost, root, and no password which is no problem to publish to github.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5121
  • View blog
  • Posts: 13,790
  • Joined: 18-April 07

Re: Variable environment(php) and .gitignore

Posted 30 December 2017 - 04:26 PM

View Post[email protected], on 30 December 2017 - 01:36 PM, said:

To explain my problem, I wanna put my map with my config file on Github. If I do that people can download my files and access my database I don't want that. But if I remove the config file from my map you can't connect anymore to your database. So I need to store my credentials in a variable environment, that's what I understood. But I'm new to this so I don't really know how to get the job done if you understand what I mean.


Ok understood, but you can do that many different ways. For instance I typically setup a config file with all my settings, create a function to fetch values from config and then commit everything but the config file. So if they download the files they see something like "config(some_setting)" and that is it. This allows others in your group to have their own config file and settings.

Laravel does this using an .env file and an env() function to retrieve settings. So are you talking about something like this? Here is a URL to Laravel's system.

https://laravel.com/...5/configuration

Are you looking to do this?
Was This Post Helpful? 0
  • +
  • -

#6 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 57
  • Joined: 24-February 15

Re: Variable environment(php) and .gitignore

Posted 01 January 2018 - 12:53 PM

Kind of I will see what I can do, still confusing tho.

This post has been edited by [email protected]: 01 January 2018 - 01:06 PM

Was This Post Helpful? 0
  • +
  • -

#7 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 57
  • Joined: 24-February 15

Re: Variable environment(php) and .gitignore

Posted 01 January 2018 - 02:30 PM

Can someone make an example project with a config file and env variables. Can't figure it out my self how to setup it up.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,512
  • Joined: 08-August 08

Re: Variable environment(php) and .gitignore

Posted 01 January 2018 - 05:54 PM

Are you using a framework? If you're using one look at where they store their config file. If not, you could use file_get_contents, file_put_contents, and/or file to read/write from/to a hidden file like .env. If you're going to roll your own, I recommend using a class or array to hold the data, then json encode the information and store it in the file. That way you can more easily read it. It's also the way many config files are stored these days.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,512
  • Joined: 08-August 08

Re: Variable environment(php) and .gitignore

Posted 01 January 2018 - 06:08 PM

Here's a simple example using a class to hold some preferences:
<?php
$prefs = new stdClass();
$prefs->username = "UserOne";
$prefs->password = "useronepass";
$prefs->db = "some_db";

echo "original class:<br>";
print_r($prefs);
$encoded = json_encode($prefs,true);
file_put_contents(".env",$encoded);
echo "<br><br>encoded original saved class:<br>";
print_r($encoded);

$decode = file_get_contents(".env");
$decoded = json_decode($decode);
echo "<br><br>encoded retrieved data:<br>";
print_r($decode);
echo "<br><br>retrieved data decoded:<br>";
print_r($decoded);

My output:
original class:
stdClass Object ( [username] => UserOne [password] => useronepass [db] => some_db ) 

encoded original saved class:
{"username":"UserOne","password":"useronepass","db":"some_db"}

encoded retrieved data:
{"username":"UserOne","password":"useronepass","db":"some_db"}

retrieved data decoded:
stdClass Object ( [username] => UserOne [password] => useronepass [db] => some_db )

Was This Post Helpful? 0
  • +
  • -

#10 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 57
  • Joined: 24-February 15

Re: Variable environment(php) and .gitignore

Posted 02 January 2018 - 06:36 AM

Thanks for the help, but sorry I just don't get it.
If I'm right there is a file on your server which holds the original credentials(dbname, user, and pass) which are called env.php, and you have a file in your local map which you can share on GitHub named config.PHP, which holds fake credentials everybody can see.
Was This Post Helpful? 0
  • +
  • -

#11 benanamen  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 143
  • Joined: 28-March 15

Re: Variable environment(php) and .gitignore

Posted 02 January 2018 - 10:11 AM

It seems this whole thing is about the public not seeing "real" login credentials on Github. All you have to do is what I already posted about. Use the very common generic login credentials on your local system and the "problem" is solved. (localhost, root, no password)

If the project is not for public viewing, you can use BitBucket instead which allows free private repository's.

This post has been edited by benanamen: 02 January 2018 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#12 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 57
  • Joined: 24-February 15

Re: Variable environment(php) and .gitignore

Posted 02 January 2018 - 10:15 AM

View Postbenanamen, on 02 January 2018 - 10:11 AM, said:

It seems this whole thing is about the public not seeing "real" login credentials on Github. All you have to do is what I already posted about. Use the very common generic login credentials on your local system and the "problem" is solved. (localhost, root, no password)


Ye I saw your comment before, and I tried to use no password and when I run it you get an error that there is no passeord set.
Was This Post Helpful? 0
  • +
  • -

#13 benanamen  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 143
  • Joined: 28-March 15

Re: Variable environment(php) and .gitignore

Posted 02 January 2018 - 10:21 AM

You can either look up how to do no password or just use a dummy password for testing and Github publishing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1