0
votes

I need to block the directly access to some php files. Currently I ussing a constant to prevent this, but when I include the constant from the primary file and include again another php file the third file aren't taken the value of the constant.

Currently I'm running the script on XAMPP with php 7.3. Ordered as follows:

Project/

  • index.php
  • link.php
  • inc/
    • classes.php
    • functions.php

index.php

define( '__PATH__', dirname( __FILE__ ) . '/' )
[...]
include ( 'link.php' );

link.php

if( ! defined('__PATH__') ) die('unatuhorized');
[...]
require( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'inc/classes.php' );

Output

__PATH__ = bool true

/inc/classes.php

if( ! defined('__PATH__') ) die('unatuhorized');

Output

__PATH__ = Undefined constant __PATH__

Expected

__PATH__ = bool true/false

I want to...
Allow the access: to index.php
Block directly access to: link.php, inc/classes.php

1
None of the code you've included in your example would have any output at all, let alone "bool true". Please include the exact code that causes this behavior. - user149341

1 Answers

0
votes

What I have done for blocking direct access is define a constant on index.php then check if the constant is defined on the other files I do not want to give direct access to in my application.

index.php

define('MYAPP',1);

links.php (at the very top of the file)

if (!defined('MYAPP')) {
    die('Please use the front door.');
}

Hope this helps!