PHP—“Defined or Exit” at the beginning of many files?

前端 未结 3 1583
孤独总比滥情好
孤独总比滥情好 2021-01-14 16:47

I have been inspecting some PHP source codes and I more often than not find files starting with

defined(\'__someconstant__\') or exit();

I

3条回答
  •  借酒劲吻你
    2021-01-14 17:12

    To avoid those (useless) lines at the top of (nearly) each file, your could:

    • Store a public "controller" file (like index.php in a directory called web or public on which your web server's alias or virtual host points to

    • Store in other directories like lib, config, apps... all the files that should not be directly accessed through the webserver by simply typing an URL.

    This is typically the structure of existing frameworks such as Symfony 1.x

    Additionally you can (and certainly will, for URL rewrites) put a .htaccess file, but a server misconfiguration can incidentally disable it, so keeping source files in distinct directories is IMO better.

提交回复
热议问题