What role does .htaccess play in preventing image theft using PHP?
To prevent image theft using PHP, you can use .htaccess to restrict access to the image files from being directly accessed by unauthorized users. By setting up rules in the .htaccess file, you can prevent hotlinking or direct linking to your images from external websites, thus reducing the chances of image theft.
# Prevent hotlinking of images
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourwebsite.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F]