Save cookie on Image load
Is it possible to save a cookie on a user computer when he loads an开发者_开发技巧 image from my server?
So let's say my image url is: http://www.site.com/image.jpg
If he loads just that jpg, can I save a cookie on his machine?
Thanks,
You will have to serve the image through a PHP script if you want to use PHP for this. This may be done by rewriting the request to go to a PHP file, and then use readfile in that script.
Another solution is to set the cookie directly in your web server. How you do that depends on the web server you are using. This is how you do it in Nginx:
location /image.jpg {
add_header Set-Cookie "cookiename=value";
}
And for Apache:
<FilesMatch "^image\.jpg$">
Header set Set-Cookie: "cookiename=value"
</FilesMatch>
If you are using Apache 2.0.40 (or later) then you can try adding the following code in .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^image\.jpg$ - [co=YourCookieName:YourCookieValue:YourCookieDomain]
More info here
精彩评论