Preventing Direct Access In PHP

Preventing Direct Access In PHP Files

To prevent direct access can be used redirect method. Redirect is actually can be exploited when a user has done filling the form, then redirected to a page that contains information that the form has been filled correctly, and other information. This tutorial discusses the more about the PHP file itself. What I mean here is about accessing a PHP file directly.

For example like this, you create a PHP file, where you do not want this files directly accessed through the URL, but you want the file can only be executed when it’s included by another file. It will display a “Forbidden” warning when the file is accessed directly.

How to do this? Please read this tutorial until the end.

We need at least 2 pieces of PHP files. The first is index.php:
[code]<br />
<?<br />
define(active, "yes");<br />
require_once "page.php";<br />
?><br />
[/code]

Then the second is the page.php :
[code]<?<br />
if (active == "yes"){<br />
echo "i am inside";<br />
}<br />
<br />
else{<br />
echo "You Can't Access this page ... ";<br />
}<br />
?>[/code]

When we access the URL index.php file via the browser, then what happens is, variables and functions in page.php file was on the run in the index.php file, this happens because the constant assistance that exist in index.php. While the appearance of "You Can not Access this page ..." is obtained when we directly access the url of the file page.php, this happens because the constant does not exist or is not defined, so that there is a warning "You Can not Access this page ...".

0 comments:

Post a Comment