A safe technique is by directory indentation.
We can also solve with other techniques such as the definition of a constant or the rules of permission to access a public folder.
This resource depends on the page server used.
I consider the directory indentation more secure because it protects both client and server side.
To better understand, imagine a situation where the site owner has access to FTP. But you don’t want this guy to have access to the system codes. It is recommended not to leave the system so open even for the owner, especially when it is a layman, because fatally one day the subject will touch the codes, causing bugs or something more serious.
How to do directory indentation?
In the public folder you would only have the index.php file.
In this index.php file, you include a file in a private folder.
Example of structure.
/var/www/website.foo/public
/var/www/website.foo/app
/var/www/website.foo/logs
In the above example, the root directory is /var/www/website.foo/public
The index.php file would be inside this directory /var/www/website.foo/public/index.php
All other system . php files, put out public folder.
/var/www/website.foo/app/foo.php
/var/www/website.foo/app/bar.php
Since this directory is not accessible to the public, it is safe. However, a third person with server access via FTP or SSH can still have access to the files.
If you want to enhance security, do not give SSH or FTP access to this directory to unauthorized persons.
For the client who wants FTP access, free it to access only from the public directory.
Still runs the risk of the client running php scripts inside the public folder.
For these cases, it is also possible to block execution of PHP scripts in the public folder. The problem is that index.php would no longer work.
One solution to this is to create a symbolic link where even index.php could stay out of the public folder.
This way we have all the files, including index.php, protected from both visitor and FTP user.
On Linux systems, the symbolic link can be made as follows:
ln -s "/var/www/website.foo/app/index.php" "/var/www/website.foo/public/index.php"
In Windows environment:
mklink /j "c:\www\website.foo\app\index.php" "c:\www\website.foo\public\index.php"
A flaw for using directory indentation or symbolic link is when the system runs in an environment where we are not allowed to run command lines and in many cases where it is not even possible to do directory indentation. This situation is common in shared hosting of outdated structure.
One of the techniques is to put it outside the root of the site: If your site is, for example in /web/httpdocs/index.php you can send it.php in /web/send.php. But this only works with includes. Another solution would be to block direct access with some rules in .htacess. There are several ways, it depends a lot on the page server used. (tou posting as a comment just to advance the subject) , let’s see if anyone posts anything more elaborate or indicates possible existing answers. PS: Very cool you continue using the site, hope we can help better in this case here.
– Bacco
Ah, a hint: if you want to add more details to the question, such as the type of server you use, or more details of the file structure you have (or will need), use [Edit] just below the question. Details usually help in the preparation of answers.
– Bacco
Thanks for the answer, I will wait for more solutions because my project already has a considerable amount of files, it would be difficult to migrate, I think the rules of htaccess will be useful.
– Ana P. Messina