Tutorial Categories
» PHP
» PHP User System
» Web Development
» Javascript
» Cascading Style Sheets
» HTML
» Adobe Photoshop
» Adobe Dreamweaver
» Adobe Fireworks
» Microsoft Windows
» Microsoft Office
» Apple
» Linux
Partners
» How To Build A Solar Panel » Affordable Web Hosting » How to make a website » Tech Product Reviews » Web Hosting Reviews » Buy Website Traffic » Linux Training » Mac Pro Memory » Dating » EducationWarning: file() [function.file]: URL file-access is disabled in the server configuration in /home/techt/public_html/index.php on line 203
Warning: file(http://www.tucows.no/external.asp) [function.file]: failed to open stream: no suitable wrapper could be found in /home/techt/public_html/index.php on line 203
Warning: Invalid argument supplied for foreach() in /home/techt/public_html/index.php on line 205
Prevent direct linking to any php file of your choice
using the define and a if/else technique to achieve this!
1
With this tutorial I'll show you people how you can easily protect your php files with just a couple lines of code! Here it goes:
for example: you do not want people to be able to go directly to your config.php file.
lets assume your config.php file looks like this:
That is just a basic mysql connect script. now to protect it! We are adding 4 lines of code, take a look:
Ok, you may think, what in gods name is he doing? Well, hmm ok I understand, let's explain what I just did.
We are using a if statement to check if 'SESAMOPEN' has been defined, if it is not (see, we are using a '!' in front of defined) tell the user that they are lost and should go back.
and last using exit(); to stop and closing the if statement with a }
Now you think you're ready, but your not! we are only 50% done! Because, with that code SESAMOPEN will never be defined and thus your own script wont even be able to get access to config.php!
So whenever you want to include config.php to your script to get mysql content you need to add this above the line where you include config.php:
That line gives SESAMOPEN a value, so it is ‘defined’ and it will pass the if statement in your config.php file!
And KABOOM, you're done, to test, go directly to your config.php and see if it works
if you need help with this tutorial feel free to comment!
Wildo
for example: you do not want people to be able to go directly to your config.php file.
lets assume your config.php file looks like this:
<?PHP $chost = "localhost"; $cusername = "YOURUSERNAME"; $cpassword = "YOURPASSWORD"; $cdb = "YOURDATABASE"; mysql_connect($chost, $cusername, $cpassword); mysql_select_db($cdb); ?>
That is just a basic mysql connect script. now to protect it! We are adding 4 lines of code, take a look:
<?PHP
if(!defined("SESAMOPEN")){
echo "What are you doing here? You're not allowed to be here, be gone you pest!";
exit();
}
$chost = "localhost";
$cusername = "YOURUSERNAME";
$cpassword = "YOURPASSWORD";
$cdb = "YOURDATABASE";
mysql_connect($chost, $cusername, $cpassword);
mysql_select_db($cdb);
?>Ok, you may think, what in gods name is he doing? Well, hmm ok I understand, let's explain what I just did.
<?PHP
if(!defined("SESAMOPEN")){
?>We are using a if statement to check if 'SESAMOPEN' has been defined, if it is not (see, we are using a '!' in front of defined) tell the user that they are lost and should go back.
<?PHP echo "What are you doing here? You're not allowed to be here, be gone you pest!"; ?>
and last using exit(); to stop and closing the if statement with a }
<?PHP
exit();
}
?>Now you think you're ready, but your not! we are only 50% done! Because, with that code SESAMOPEN will never be defined and thus your own script wont even be able to get access to config.php!
So whenever you want to include config.php to your script to get mysql content you need to add this above the line where you include config.php:
<?PHP
define("SESAMOPEN", 1);
?>That line gives SESAMOPEN a value, so it is ‘defined’ and it will pass the if statement in your config.php file!
And KABOOM, you're done, to test, go directly to your config.php and see if it works
if you need help with this tutorial feel free to comment!
Wildo
1