r/PHPhelp • u/Code1Light • Oct 22 '22
Solved PHP server security?
Hello everyone, I pray that you are all doing well.
You might have noticed that the title is a question, that's because I'm not really sure what exactly I'm looking for.
But you might, so here is what I'm specifically asking about.
When we create a database in phpmyadmin, we later go to our coding program and start writing this :
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "whatever the database name is"; $conn = mysqli_connect($servername, $username, $password, $dbname); Now clearly this is not secure. And someone told me once that the username and password should not be like this. But all the tutorials do this, so what's the correct thing to write? Thank you all in advance, have a good day/night đ The Lord be with you all and save you all your families and friends đ¤
1
u/dabenu Oct 22 '22 edited Oct 22 '22
There are ways to eliminate plain text secrets, but they involve running a secrets server. That introduces latency you don't want to deal with on every request. So in practice, you basically always end up with a database password in plaintext somewhere. Just make sure you go about it as safely as possible:
store secrets (or rather, all your code save a bootstrap index.php) outside the public directory of your webserver;
limit read access to said files;
turn off display_errors, use a custom exception handler;
Only allow access to your database from the webserer IPs (at least for this user).
I probably forget something.
1
5
u/32gbsd Oct 22 '22
Your php code is only as secure as the server on which it is stored. Placing your config code outside the document root as others have mentioned is a placebo because anyone who has access to your server has access to all your files and folders. So each file is as secure as any other file. Once your server access is secure and there is no hacks allowing code execution then its secure as any other code on the planet. Making jiberish passwords helps too because they are easily replaced.
2
u/dabenu Oct 22 '22
It's not a placebo. It's eliminating a possible attack surface. There's no one magic bullet to security. You have to follow best practices on every level.
1
u/rivenjg Oct 22 '22
it's not because in order to get the server to display php files as text files, you would have server access to private directories as well. php files are secure. acting like php files aren't secure is stupid and way inconvenient for no reason. you can also just set apache to 404 files that start with "_" or w/e you want if you don't want certain php files to be run at all.
0
Oct 22 '22
[deleted]
2
u/32gbsd Oct 22 '22
One commenter said that if the whole server is misconfigured then all the php files in root would be visible as plain text. bigger problem? not sure
2
u/kAlvaro Oct 22 '22
Tutorials do it for two reasons:
A tutorial needs to focus on the subject it wants to illustrate. Simple programs are simple.
Internet is riddled with terrible PHP tutorials (you can't imagine how bad!).
As almost everything in IT, this subject can get as complex as you want. Some organisations even use an external configuration server where applications grab the values from and authorised users are able to manage the secrets they've been granted access to. On the opposite side, storing credentials with the source code (and probably upload them to GitHub) is wrong in almost every sense even for personal pet projects.
To have something in between, I'd suggest to keep secrets in a text file, call it .env, ensure it doesn't get committed to version control (i.e. add it .gitignore) and use a library like symfony/dotenv or a Composer plugin like helhum/dotenv-connector to have the values available as environment variables.
$conn = mysqli_connect($_SERVER['DB_HOST'], $_SERVER['DB_USER'], $_SERVER['DB_PASSWORD'], $_SERVER['DB_NAME']);2
u/dabenu Oct 23 '22
Be extra careful not to put a .env file in your webservers public directories! It'll just be visible as plain text.
2
u/kAlvaro Oct 23 '22
Good point!
A typical layout has a
publicdirectory that's exposed to the web server (e.g./var/www/public/img/foo.jpgis mapped tohttps://example.com/img/foo.jpg) and.envis one level up (e.g./var/www/.env).
1
u/Matrix009917 Oct 31 '22
Use class for define private access to the credential, create another class to extends the parameters class and create the mysql connection. I advise you to use PDO connection instead of mysql.
0
Oct 22 '22
[deleted]
0
u/dabenu Oct 22 '22
So you moved them to another plain-text file... I don't think that;s much of an upgrade. Although apache config files might have stricter access rights than the project directory, that could be a pre.
8
5
u/mds1256 Oct 22 '22
Place the connection details in a php file that is located outside of the www directory and then just include it in your scripts.