how to block direct access to a .php file

  • Filter
  • Time
  • Show
Clear All
new posts

  • how to block direct access to a .php file

    hi friends,
    if i want to block direct access to test.php file what should i do,

    example: if any one type directly this in browser it must redirect to home page

    is there any code for this

  • #2
    its too easy.....jast add this code in your .htaccess file
    ErrorDocument 403 /index.php
    ErrorDocument 404 /index.php
    ErrorDocument 500 /index.php


    • #3
      i think u didn't get my point .....i asked to block test.php when it is visitors directly type in browser like if they type this only directly it should redirect to home page ,

      or consider like this i have my.php file and linked test.php in that file test.php must be open if it clicked from my.php .....otherwise it should redirect to home page


      • #4
        SetEnvIf REFERER "" linked_from_here
        SetEnvIf REFERER "^$" linked_from_here

        <Files ~ "test.php">
        Order deny,allow
        Deny from all
        Allow from env=linked_from_here

        is this correct


        • #5
          I take it the php file is included in another script
          so you would probably add something like:
          PHP Code:
          header("Location: /index.php");

          Last edited by something else; 05.07.10, 17:50.


          • #6
            what do you mean? can u explain clearly


            • #7
              my above code is for a php page if it actually exists but you dont want nobody going on it directly ...only way to get on that page is via another page by using include('test.php'); or some other similar way

              if the page doesnt exist then use sanju`s suggestion in .htaccess
              ErrorDocument 404 /index.php


              • #8
                Hmmmmm.....after tried several methods i got solution by using $_SERVER['PHP_SELF']; and $_SERVER['HTTP_REFERER']; ........please close this thread