Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save cookie on Image load

Tags:

php

cookies

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,

like image 364
yeeha1 Avatar asked Mar 07 '26 15:03

yeeha1


1 Answers

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>
like image 191
Emil Vikström Avatar answered Mar 09 '26 04:03

Emil Vikström



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!