Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

Preventing Direct Access with PHP

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

Eureka
Newbie


Posts: 23
Group: Registered
Joined: Dec 2007
Status: Away
Reputation: 0
Points: 17 (Donate)
Post: #1

Preventing Direct Access with PHP


Okay, here's a tutorial on how you can prevent people from directly accessing certain PHP files. Wink Useful for methods like require_once and include()

Let's start out basic with a template. It's always good to have a template. Doing things in a structured way is always good. I had a lot of success doing thing structurally. Smile

PHP Code:
<?php

/* Codes go here */

?>


Okay, let's put some HTML content the person would get if they were to approach this site directly.

PHP Code:
<?php

echo "<b>404 Error - File Not Found.</b>";

?>


Yes, you can modify the message. That's not a big deal.

So now we need to do a condition with an 'if' statement to secure it. To do this, we would just ask if a variable is defined. If not, then display that message. The key to this is that the variable shouldn't be defined.

PHP Code:
if (!defined("RAWRZ")){
   echo 
"<b>404 Error - File Not Found.</b>";
}
else {

/* PHP codes here */



Okay, now it's protected from direct access. This is useful to prevent people from accessing your include() and require(). Just make sure that before you include() or require() the page, you define the variable, which in this case is RAWRZ.

Example: (take the top php file to be called rawrz.php and this file, which includes rawrz.php to be called rawr.php)

PHP Code:
<?php

define 
("RAWRZ"true);
include(
"rawrz.php");

?>


Hope this helps. If anything isn't clear, let me know. Smile

Enjoy ^_^


Edsgar Dijkstra (a dutch computer scientist who invented the concept of structured programming) Wrote:
"The competent programmer is fully aware of the limited size of his own skull. He therefore approaches his task with full humility, and avoids clever tricks like the plague."

"Program testing can be used to show the presence of bugs, but never to show their absence!"

"APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums."

This post was last modified: 19-12-2007 09:04 AM by Eureka.

19-12-2007 09:01 AM
Find all posts by this user Quote this message in a reply
walsh
Senior Member
****


Posts: 401
Group: Registered
Joined: Oct 2006
Status: Offline
Reputation: 0
Points: 680 (Donate)
Post: #2

RE: Preventing Direct Access with PHP


Wonderful tutorial. I think you are very good with PHP. I nevver thought like this. Thanks for the tutorial.


22-12-2007 04:35 AM
Find all posts by this user Quote this message in a reply
Eureka
Newbie


Posts: 23
Group: Registered
Joined: Dec 2007
Status: Away
Reputation: 0
Points: 17 (Donate)
Post: #3

RE: Preventing Direct Access with PHP


Eh, thanks. It was a very basic thing and I use it in all my PHP mods to prevent people from accessing them directly. Surprised it wasn't created here so I posted it up.


Edsgar Dijkstra (a dutch computer scientist who invented the concept of structured programming) Wrote:
"The competent programmer is fully aware of the limited size of his own skull. He therefore approaches his task with full humility, and avoids clever tricks like the plague."

"Program testing can be used to show the presence of bugs, but never to show their absence!"

"APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums."

24-12-2007 04:22 AM
Find all posts by this user Quote this message in a reply
ivenms
Administrator
*******


Posts: 2,179
Group: Administrators
Joined: Sep 2006
Status: Offline
Reputation: 14
Points: 4389 (Donate)
Post: #4

RE: Preventing Direct Access with PHP


Eureka Wrote:
Surprised it wasn't created here so I posted it up.


This part is not avoidable for any kind of scripts. MyBB also included this part and only allowed files are accessible for the visitors.


Read: General Rules & Policies before posting.
Make Money By Posting | Earning and Exchanging Points | Add Your Links
24-12-2007 04:48 AM
Find all posts by this user Quote this message in a reply
Eureka
Newbie


Posts: 23
Group: Registered
Joined: Dec 2007
Status: Away
Reputation: 0
Points: 17 (Donate)
Post: #5

RE: Preventing Direct Access with PHP


MyBB or forums in general Wink

Aside from forums, just any scripts really.


Edsgar Dijkstra (a dutch computer scientist who invented the concept of structured programming) Wrote:
"The competent programmer is fully aware of the limited size of his own skull. He therefore approaches his task with full humility, and avoids clever tricks like the plague."

"Program testing can be used to show the presence of bugs, but never to show their absence!"

"APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums."

This post was last modified: 24-12-2007 06:17 AM by Eureka.

24-12-2007 06:16 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
Cool PHP Database access bomber 0 534 18-09-2006 11:56 PM
Last Post: bomber

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 29-08-2008, 11:22 AM


Copyright © 2002-2008 MyBB Group
Powered By MyBB