Web Design Forum: TUTORIAL: Learn how to prevent direct linking to your php files - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: Learn how to prevent direct linking to your php files Rate Topic: ***** 1 Votes

#1 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 17 May 2007 - 08:24 PM

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:
<?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
0

#2 User is offline   Ben 

  • Web Designer Forum Admin
  • View gallery
  • Group: Root Admin
  • Posts: 2,840
  • Joined: 24-August 06
  • Reputation: 103
  • Gender:Male
  • Location:Essex, UK
  • Experience:Intermediate
  • Area of Expertise:Web Designer

  Posted 17 May 2007 - 08:30 PM

Very handy indeed!

Thanks for the tutorial Wildo! :)
0

#3 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 17 May 2007 - 08:32 PM

could be shorter, wrote it ages ago, litteraly I think :p

I used

echo "message":
exit ();


while I could have done

exit ("message");


little shorter :)

Wildo
0

#4 User is offline   BenG 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 766
  • Joined: 20-March 07
  • Reputation: 0
  • Location:Bradford, West Yorkshire
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 17 May 2007 - 08:33 PM

Excellent.

You rock wildo
0

#5 User is offline   Cats Corporation 

  • Dedicated Member
  • PipPip
  • View gallery
  • Group: Members
  • Posts: 197
  • Joined: 28-October 07
  • Reputation: 0
  • Gender:Male
  • Location:Metro Manila, Philippines
  • Experience:Intermediate
  • Area of Expertise:Entrepreneur

Posted 17 February 2008 - 11:55 AM

Pardon my ignorance, but is there really anyway anyone (aside from the webmaster) can access the php files of a website?

I tried typing something like www.mydomain.com/sidebar.php, and all I get is a blank page. I look at the code and it is also blank.

Sorry if this question sounds not-so-intelligent. I started learning php only last week.
0

#6 User is offline   Tucker 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 63
  • Joined: 29-November 07
  • Reputation: 0
  • Location:Somerset, UK
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 19 February 2008 - 11:51 AM

Can't see how anyone accessing one of my config files would be a problem. If you're not outputting anything then there's nothing to worry about!
0

#7 User is offline   php_penguin 

  • richthegeek
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,471
  • Joined: 06-August 07
  • Reputation: 7
  • Gender:Male
  • Location:Liverpool
  • Experience:Web Guru
  • Area of Expertise:Coder

Posted 19 February 2008 - 01:48 PM

this is to do with using the include() function across domains.

For example if your config file is at:
http://www.mysite.com/config.php

I could do:
<?php 
include( "http://www.mysite.com/config.php" );
print_r( get_defined_vars() ) );
?>

and print out your (probably define()'d) mysql passwords.
0

#8 User is offline   Tucker 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 63
  • Joined: 29-November 07
  • Reputation: 0
  • Location:Somerset, UK
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 19 February 2008 - 01:50 PM

Ahh yes of course! Feel a tad embarassed now, will investigate..
0

#9 User is offline   Cats Corporation 

  • Dedicated Member
  • PipPip
  • View gallery
  • Group: Members
  • Posts: 197
  • Joined: 28-October 07
  • Reputation: 0
  • Gender:Male
  • Location:Metro Manila, Philippines
  • Experience:Intermediate
  • Area of Expertise:Entrepreneur

Posted 21 February 2008 - 07:27 AM

He he he! It seems I really need to study the deeper and more arcane concepts of php for this to make sense. :D

Bye! Gotta grab a book
0

#10 User is offline   Tucker 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 63
  • Joined: 29-November 07
  • Reputation: 0
  • Location:Somerset, UK
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 21 February 2008 - 12:05 PM

Hey Penguin, I tried hacking one of my own config files and whatever I did, i couldn't read the variable like $dbusername etc.

Presumably it's not possible as I have register globals off?
0

#11 User is offline   mandy 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 04-April 08
  • Reputation: 0
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 15 April 2008 - 06:21 AM

it seems very interesting ..
i will definitely try..
thanks for sharing the coding with us ..
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users