You are invited to Log in or Register a free Frihost Account!

Preventing bad PHP script calls

I've been programming in php for about a year now, recently I just finished a project for my school (a comment manager) that was almost exclusively done in php. Because of that, security with php has always been one of my interests. Recently I realized that sometimes php scripts can easily be called from other sites. Maybe an example would make it clearer.

Say you have a script on your site called sendMoney.php. Also, say you use this script in a html file called example.html. Now a person can simply make a fake web page and call your php script from there just by putting the entire resource location (ex instead of just sendMoney.php), instead of doing whatever they have to do in example.html. This of course is a horrible breach of security.

But the good thing is that it's a very simple problem to fix, however apparently not many people are conscious of it and therefore do not take the 4 lines of code to prevent it.

So without future ado, here's the fix


// get referrer from _SERVER array

if ($ref != "" || $ref != "")
    //bad referrer detected, exit script
    print "Access Denied";

//continue with script

basically what this snippet of code does it that it gets the site that called the script and compares it to a list of allowed sites. If the site that called it is not in the list, the script ends immediately, otherwise it'll go on doing what it should do. Interestingly enough, I've just realized this problem today, so I'll have to go back and update most of my old php scripts. :-/

Have fun using this and remember, no site can be made completely secure, you can only make it as secure as possible.
I believe that "referer" [sic] is spelled incorrectly.

Hence, your script won't work. Wink

it's spelled right.
Related topics
Phoenix free PHP scripts
error in my PHP script
how can i make a php script for enter username and password?
Inserting data to MYSQL with a PHP script
Running a "Hello World" php script
[PHP] Running Cron Jobs Via PHP Script
Simple PHP script not working
PHP Script- How can we install it
I need a VERY simple php script
PHP Script needed
Logging systeem php script
array check box on php script using $_POST
How to change extension php to html but the php script still
php script not loading
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.