Help - Search - Members - Calendar
Full Version: Limit access to your php files
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Joablen
As you might be wondering what do i mean by direct access, by direct access i mean, to access directly to the file (example: www.yoursite.com/includes/file.php)...

Step 1
------------------------------------------
Now, for limiting direct access to those files you wanna protect from being access without being on your index for example, what we have to do is add a define on our index by adding this at the start of your code:
CODE
<?php
define("ON_OUR_SITE", true);
?>

where it says ON_OUR_SITE you can change it for whatever you want...


Step 2
------------------------------------------
after completing step 1, at the start of the code of each page you want to protect from being directly accessed you have to add something like this:
CODE
<?php
if (!defined("ON_OUR_SITE"))
{
   die ("Don't waste your time trying to access this file");
}
?>

where it says ON_OUR_SITE you have to change it to whatever you put on your index...


so that is all you have to do... that will say "Don't waste your time trying to access this file" to who ever tries to access your protect files directly, but it will show it's content on your index without any problem.
. Adam .
Very simple, but nice smile.gif

- Adam bigwink.gif
bblincoe
Hey nice job stealing this tutorial.

http://www.webdesigndev.com/tutorial/45

Go steal some more why don't you. Loser.
fedekiller
well i made this tutorial before that one
http://php4all.org/nav-tutorials-showtut-true-tid-90.html

everybody uses that, including phpbb i dont know if its a rip
syphon
QUOTE(bblincoe @ Oct 14 2006, 08:52 PM) *
Hey nice job stealing this tutorial.

http://www.webdesigndev.com/tutorial/45

Go steal some more why don't you. Loser.


That was very childish. For such a generic tutorial, there are bound to be copies. Just because your site has one version does not mean that others cannot. While there are copyrights on content, just because content appears to be duplicated does not mean it has been.

du
cheerio
Alot of people use this. All he's trying to do is help spread the idea. It's not like there's only 1 tutorial on making a stylish layout in photoshop is there? It's not ripped [Period]
bblincoe
My apologies then. If the intention was to just inform users of the concept, by all means go for it. It's not my site anyways, I just passed over it the other day. Once again, sorry about this misconception.
cheerio
QUOTE(bblincoe @ Oct 14 2006, 10:23 PM) *
My apologies then. If the intention was to just inform users of the concept, by all means go for it. It's not my site anyways, I just passed over it the other day. Once again, sorry about this misconception.

Everyone makes mistakes.
Victor
That's a method used by all programmers. Even IPB forums has it. Nice job spreading the idea!
faulk
really cool idea I never even thought about it. When I'm done on "vacation" (no web coding for a few weeks hah) I am going to update my latest site include files with this method.
Joablen
QUOTE(faulk @ Oct 15 2006, 09:04 PM) *
really cool idea I never even thought about it. When I'm done on "vacation" (no web coding for a few weeks hah) I am going to update my latest site include files with this method.

great! it's good to hear that this tut is useful
Alex_
PLEASE HELP

I've made a test an does not work
In a folder I've created two files Index.php & SecondPage.php

CODE
<?php

// INDEX.PHP FILE

define("ON_OUR_SITE", true);
echo "<a href='SecondPage.php'>SecondPage.php</a>";

?>


CODE
<?php

// SECONDPAGE.PHP FILE

if (!defined("ON_OUR_SITE"))
{
   die ("Don't waste your time trying to access this file");
}

echo "Page Loaded !!!! ";

?>


Like Im accesing the secondpage by clicking a link and not by typing the address in the browser, think it have to work, but it seems "ON_OUR_SITE" is no passed and not defined to the SecondPage, Why?
What is the error?

Please Im new here and working PHP, can anyone help me

Thanks in advance
Matthew.
For that to work index.php would have to be included in secondpage.php like so:
CODE
include "index.php";
above your conditional.
Alex_
Thanks Matthew it seems to work now, it returns me the page loaded when I click on the link, but it really does not work cause when I type the address on the browser it returns me the page loaded too.

Anyone could access the page directly.

Any idea?

CODE
<?php
define("ON_OUR_SITE", true);
echo "<a href='SecondPage.php'>SecondPage.php</a>";
?>

CODE
<?php

include "index.php";   // <------- ?????????????

if (!defined("ON_OUR_SITE"))
{
   die ("Don't waste your time trying to access this file");
}

echo "Page Loaded !!!! ";

?>
Matthew.
I would assume it has a link to secondpage.php on secondpage.php right? and that is not wantedi guess so you will need to put
CODE
define("ON_OUR_SITE", true);
in another file (seperate to index.php) and include it with secondpage.php and index.php
d4o2
nice and simple.. thanks victory.gif
smart-coder
Very interesting tutorial =), Its quite useful. But could'nt you just use .htaccess?
Arutha
QUOTE(Victor @ Oct 15 2006, 09:59 AM) *
That's a method used by all programmers. Even IPB forums has it. Nice job spreading the idea!


phpbb use it as well smile.gif

QUOTE(smart-coder @ Jan 30 2007, 01:33 AM) *
Very interesting tutorial =), Its quite useful. But could'nt you just use .htaccess?


the define command doesn't just need to be used for passwords, it could be used in multiple part forms to make sure the users done the previous part. It could be used to define a global variable even though sessions variables would be a better idea in that case.

Arutha
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.