Restrict direct access to file

Hey,

I have a remote PHP file that echo's out some javascript on another domain.  The PHP file accepts GET values.  How could I go about preventing users from directly accessing the PHP file from the address bar, but the remote file that is linked still works?

So I want to stop users going to..

http://mydomain.com/out/remote.php

But if the PHP file is called like so...

<script type="text/javascript" src="http://mydomain.com/out/remote.php?user=yarr"></script>

It will continue to work fine.

I'm thinking maybe .htacess?  

Thanks
LVL 6
pD_EOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dancablamCommented:
Hey there,

I recommend putting something like this at the top of your script:

if(empty($_SERVER['HTTP_REFERER'])) exit;

Note that it isn't the most secure or robust thing you could do, but it's by far the easiest.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pD_EOAuthor Commented:
Hi,

Yeah, already thought about doing it that way.  Some people turn of referrers though.

Any other ideas? :)

Thanks
0
pixayaCommented:
Hi,

You can check whether any parameters were passed with the request by doing the following:

if($_SERVER['QUERY_STRING']!==""){
      print('allowed');
}else{
      print('not allowed');
}

You could also check that the user parameter was passed by doing this:

if(isset($_GET['user'])){
      print('allowed');
}else{
      print('not allowed');
}

Its not exactly hacker proof but to stop the everyday user from requesting the page directly it should be enough.

Craig.
0
dancablamCommented:
You could also just encrypt your javascript if you're looking to protect it from others:
http://www.htmlguardian.org/help_main.html

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.