Jump to content

Passing data from browser to server?


Takuhi
 Share

Recommended Posts

Hello, Apologies if this is the wrong place, is common knowledge or just a stupid question.As part of a project I'm working on, I need to find a way to pass data from the browser back to the server. Specifically, I'm trying to find a way for the browser to confirm that it has received an image so that the server can then delete it.So far I've used a PHP script to copy a specific image and CSS file from a protected folder into the unprotected folder, renaming it to match the users IP and then to delete it after a two second interval. In most cases, where the internet connection is fast enough, this allows users to view the image but when they try to backtrack to the CSS file or the image they'll find it's not there. In addition to this, there will be the usual "image protection" tricks such as placing an empty repeating .gif file over the top and disabling right click etc. In general this works pretty easily. I use three browsers to test my code regularly, and each has a different way of downloading the picture. Google Chrome is by far the easiest to deal with. It doesn't seem to cache the CSS file, and the lack of "save background image" feature means that most users won't be able to get said image. Firefox does cache the CSS, so when you look at the source code and click on the CSS file, it'll end up showing the path to the image. Once you click on that though, the user just gets pointed to a 404 because the image isn't there. Whereas IE is the only browser that lets you "save background" image, which is where the disable right click comes in.The issue with this is that if the internet connection is slow, or the browser is slow, the image gets deleted before the browser is able to retrieve it. In addition to this the two second interval stops parts of the page from being rendered until the image is delete. I've tried to get around that by using ob_flush and instead of the sleep function I've tried doing a loop which only executes the delete function after two seconds by comparing start time to current time. Neither really solve the problem though.So, with that in mind, is there any way of passing a variable from something like a Javascript back to PHP? I know this can present a bit a of security problem because it could potentially allow people to execute a PHP script directly from their browser, so if possible I'd like to avoid that too. Is Java even the way to go with this?Thanks in advance for any help, tips or suggestions.Cheers,Takuhi

Edited by Takuhi
Link to comment
Share on other sites

You can pass data from browser to server without forcing a page reload by using AJAX. You can determine the image's state in this way:

var im = new Image();im.onload = function () {   // trigger AJAX communication here}im.src = "path/to/background/image.jpg";

The source should be the same URL that you use in your CSS. You don't have to do anything with the image object. It's only point is to fire the load event so that you can alert the server that the image has arrived and can safely be renamed.And don't worry about hogging memory. Browsers are smart enough to cache only one copy of the image no matter how many times it's referenced.You'll want to use PHP sessions so that you know the message is authentic.I hope you don't expect a lot of traffic. That could really mess with the whole plan.

Link to comment
Share on other sites

Thanks for the reply, that's probably exactly what I'm looking for.I'm not familiar at all with AJAX, but I do have a book on it. I'll read up on it and give it a go, and hopefully it works.I'm hoping that by the time the site gets a lot of traffic I'll be able to migrate to a better server. Would I be right in thinking that the major problem is the PHP script hogging up all the CPU time? Or is there a major problem that I've overlooked?Thanks for your help,Takuhi

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...