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

View all comments

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.

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/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)

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.