how to block direct access to a .php file

Started on the old forum in July 2010 · view the original in the archive · old links redirect here

#block-direct-access

7 replies · 3,204 views · started 16 years ago · 1 min read

3 participants
wap4ads Developer · 68 posts #1

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

example: if any one type directly this http://mydomain.com/test.php in browser it must redirect to home page

is there any code for this

Free Advertising @ wap4ads.com

Advertise your site freely @ http://wap4ads.com

sanju Developer · 59 posts #2

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

wap4ads Developer · 68 posts #3

i think u didn't get my point .....i asked to block test.php when it is visitors directly type in browser like http://mydomain.com/test.php 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 ...now test.php must be open if it clicked from my.php .....otherwise it should redirect to home page

Free Advertising @ wap4ads.com

Advertise your site freely @ http://wap4ads.com

wap4ads Developer · 68 posts #4

SetEnvIf REFERER "www.mydomain.com/my.php" linked_from_here SetEnvIf REFERER "^$" linked_from_here

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

is this correct

Free Advertising @ wap4ads.com

Advertise your site freely @ http://wap4ads.com

something else Fellow · 2,296 posts #5

I take it the php file is included in another script so you would probably add something like:

if($_SERVER['REQUEST_URI']=="/test.php"){
header("Location: /index.php");
exit;
}
wap4ads Developer · 68 posts #6

what do you mean? can u explain clearly

Free Advertising @ wap4ads.com

Advertise your site freely @ http://wap4ads.com

something else Fellow · 2,296 posts #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

wap4ads Developer · 68 posts #8

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

Free Advertising @ wap4ads.com

Advertise your site freely @ http://wap4ads.com

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close