Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
KnowledgeWoods Consulting
5,607 PointsRestrict Direct Access to .swf Via URL
I have a php file which embeds a swf inside it. It checks for condition if the user is allowed to access that .swf or not. But when someone directly type the URL to that swf it runs. Can i prevent direct access of .swf. I am using IIS as my webserver.
5 Answers
Robert Bojor
Courses Plus Student 29,439 PointsHi Bhaskar,
You can do something similar to this...
$realSWFPath = '/some/path/location/for/flash.swf';
$cachePath = '/some/cache/folder/';
$newFileName = md5(microtime()).'.swf';
copy($realSWFPath, $cachePath.$newFileName);
All you would have to do now is use the new path and new file name inside the embed object. Please be careful to use absolute paths for the $realSWFPath and $cachePath and use a relative path inside the embed.
I haven't developed any pages that run on Windows until now, so I don't know how IIS handles pathing but I will give you an example with Linux pathing and you can adapt it.
Lets assume your website is hosted in "/var/www/site.com" and the .swf file is located inside a folder called "secret", and it is called "file.swf". I am also going to assume that the cache folder is called "cache".
The script would change like so:
$realSWFPath = '/var/www/site.com/secret/file.swf';
$cachePath = '/var/www/site.com/cache/';
$newFileName = md5(microtime()).'.swf';
copy($realSWFPath, $cachePath.$newFileName);
$embedPath = 'cache/'.$newFileName;
Hope it helps and you can adapt it to your Windows pathing.
Robert Bojor
Courses Plus Student 29,439 PointsHi Bhaskar,
Because when you used $cachePath = $_SERVER['DOCUMENT_ROOT']."/cache/"; you obtained an actual file system path to that folder, and the other one is just a web address. When dealing with paths to your files you should always use file system paths like that one.
You can also use the DIR constant which is always pointing to the folder in which your script is running.
Robert Bojor
Courses Plus Student 29,439 PointsHi Bhaskar,
Normally if the file is in your website's path it is accessible through direct linking. One thing you can try and do is generate a random name for the file each time the page is loaded, place it in a cache folder and keep it there for like 1 hour.
This way if someone comes to your page they get to see the .swf with a strange name, which most of the users won't even check, and be happy. After an hour the file will be gone and if they refresh the page a new name will be created and so on.
Keep the original file somewhere in your website's path, don't link against it, just copy from it all the time to the new files. Depending on the number of visits you have, you might also have a lot of the same file generated, but the hourly script will clean that up.
Another option would be, and this one isn't tested yet, to link instead to a .swf file to a .php file in your website's path, that .php file would first check the HTTP_REFERER to see if it comes from your own domain, and if it does, issue a header function with the .swf mimetype and echo the contents of the actual .swf file.
The script I am talking about would be similar to the script below...
if (!preg_match('/my_domain/', $_SERVER['HTTP_REFERER'])) {
// Direct visitors will be redirected some/place/else
header('Location:/some/place/else');
}
$realSWFPath = '/some/path/location/for/flash.swf';
header('Content-Type: application/x-shockwave-flash');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($realSWFPath);
exit;
KnowledgeWoods Consulting
5,607 PointsCan you explain how can I generate a random file name automatically.
KnowledgeWoods Consulting
5,607 PointsIt worked perfectly when i took $cachePath = $_SERVER['DOCUMENT_ROOT']."/cache/"; but it was not working when i took path like $cachePath= "http://www.site.com/cache";
Any Idea????
KnowledgeWoods Consulting
5,607 PointsThank you so much for your help Robert....!!!!
KnowledgeWoods Consulting
5,607 PointsKnowledgeWoods Consulting
5,607 PointsI followed the steps but my swf is not running. When i viewed the page source it gives embed width="1152" height="680" src="cache/0ac7252348377d2fac55d94757ab247f.swf" and this filename changes everytime i refresh the page but there is no file with this name in my cache folder.
KnowledgeWoods Consulting
5,607 PointsKnowledgeWoods Consulting
5,607 PointsPlease provide me the script or something which deletes the cache directory after every one hour
Robert Bojor
Courses Plus Student 29,439 PointsRobert Bojor
Courses Plus Student 29,439 PointsYou actually need to delete the files from the cache folder. The cache folder will remain so other files might be written there.
Since you are running on a Windows system and I have no idea on how cron-like processes are scheduled to be executed regularly I would say trying a search on Google for "crontab on windows" might get you some answers.