r/PHPhelp • • 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 🤍

8 Upvotes

26 comments sorted by

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.

1

u/Code1Light Oct 22 '22

Okay, thank you very much. Is there websites/videos that you recommend to learn these types of things?

2

u/BetaplanB Oct 22 '22

Most PHP frameworks use a public directory and call a “bootstrap” php file outside of the directory that route your http request to the correct http controller depending on the URL you’re requesting.

You can look at Laravel for example: https://laravel.com/docs/9.x/deployment#nginx

Hopefully this is helpful, May Allah and Sauron guide you to the correct solution.

1

u/Code1Light Oct 22 '22

Thank you, I appreciate your help! 😁

1

u/BetaplanB Oct 22 '22 edited Oct 22 '22

Once you’re somewhat fluent with OOP PHP, it’s common to use a framework to solve/reuse generic building blocks so you don’t need to reinvent the wheel every time you build an application.

A framework will help you with easily setup common things that many applications use:

  • Http controllers

  • Service container/dependency injection

  • routing

  • CSRF protection

  • requests

  • views

  • templates

  • URL generation

  • session

  • logging

  • cache

  • mail

  • languages

  • queues (background processing)

  • task scheduling

  • rate limiting

  • authentication

  • encryption

  • Database ORM

  • testing

(I just copied the Laravel documentation menu)

0

u/[deleted] Oct 22 '22

[deleted]

2

u/BetaplanB Oct 22 '22

I hope so, I don’t like spaghetti in my code

1

u/safesploit Oct 22 '22

I do agree with the point of placing the config.php file outside the web directory, which has been solved here https://stackoverflow.com/a/7863072/4443012.

include("$_SERVER[DOCUMENT_ROOT]/../config.php");

However, I don't see this as the only option, because of my Apache/PHP configuration when I download my config.php an empty file is returned. But, I agree best practice should involve placing config.php outside the web directory.

5

u/BetaplanB Oct 22 '22

Everything should be placed outside the public directory, except the index.php and static assets.

4

u/mds1256 Oct 22 '22

You are missing the point that if your web server is misconfigured for a moment then a php file which is accessible via the web root would be delivered to the user in plain text, meaning that the user details would be exposed. Do not rely on the web server sending a blank output!

0

u/safesploit Oct 22 '22

I didn’t miss the point, I agreed with your point, as stated ‘best practice’, I am pointing Apache takes measures to protect certain files/directories.

Also worth noting that MySQL server will require the username/password credentials have a hostname specified, which is advised to be localhost. In which case MySQL will then reject a login not originating from that host.

The safeguards mentioned above are by no means an excuse for poorly written PHP code.

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

u/Code1Light Oct 22 '22

Thank you! 😁

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

u/[deleted] 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 public directory that's exposed to the web server (e.g. /var/www/public/img/foo.jpg is mapped to https://example.com/img/foo.jpg) and .env is 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

u/[deleted] 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

u/[deleted] Oct 22 '22

[removed] — view removed comment

1

u/Code1Light Oct 22 '22

Okay, Thank you 😁

3

u/dabenu Oct 22 '22

don't disable error reporting, just disable display_errors.