weedo tech blog

le blog technique de la team weedo

  • Home
  • Ka on twitter
    • Edit
    • Delete
    • Tags
    • Autopost

    Empêcher l'accès direct à un fichier php

    Si vous voulez qu'un fichier php ne soit jamais appelé directement via un navigateur mais seulement par des include par exemple, vous pouvez utiliser la solution suivante :

    Définir une fonction :

    function forbidDirectAccess($file) {
        $self = getcwd()."/".trim($_SERVER["PHP_SELF"], "/");
        (substr_compare($file, $self, -strlen($self)) != 0) or die('Restricted access');
    }

    Appeler la fonction depuis le fichier à protéger :

    forbidDirectAccess(__FILE__);

    Cette solution a l'avantage d'être 100% php, invariable & autonome et compatible http et cli.

    Pour approfondir : http://stackoverflow.com/questions/409496/prevent-direct-access-to-a-php-incl...

    • 6 April 2011
    • Views
    • 0 Comments
    • Permalink
    • Favorited 0 Times
    • Tweet
    • Tweet

    Comments 0 Comments

    Leave a Comment

  • About Karim Cassam Chenaï

    My Google Profile

  • Contributors

    • Ballut Vincent
    • fix
    • Karim Cassam Chenaï
  • Subscribe

    Subscribe to this posterous
    Unsubscribe
    Follow this posterous RSS
    You're a contributor here (Edit)
    This is your Space (Edit)
    Follow by email »
    Get the latest updates in your email box automatically.
  • Follow Me

      Delicious

Theme created for Posterous by Obox