Check where the request came from
on the site, when an image is required, the link is as follows
<img src="getimage.php?id=<?php echo $name ?>" " width="280px" height="70px"> Then on getimage.php it receives an identifier, and from it it finds the desired image and displays it.
$id = $_GET['id']; It works great. How do I know where the request came from. So, on which page was this line below
<img src="getimage.php?id=<?php echo $name ?>" "width="280px" height="70px"> So in the end, I can find out if the image is requested for index.php or about.php, etc.
thanks
You can add an additional parameter to the image URL, which indicates which page it was requested from:
<img src="getimage.php?id=<?php echo urlencode($name); ?>&page=<?php echo urlencode($page); ?>" "width="280px" height="70px"> It works exactly as you did with the id parameter.
The advantage is that it does not depend on some browser headers sent (or not), like HTTP Referer , that you have no control over from the server.
To get the page that links to the image, you can use something like this:
$page = basename(__FILE__); See the basename PHP Manual and __FILE__ PHP Manual .
In addition, the code uses the urlencode PHP Manual function to ensure that the attribute is not violated.
If set, find the HTTP_REFERER key in the $_SERVER :
$_SERVER['HTTP_REFERER']; The address of the page (if any) that linked to the user agent on the current page. This is set by the user agent. Not all user agents will install this, but some provide the ability to modify
HTTP_REFERERas a feature. In short, this cannot be trusted. ( source )
You can check $ _SERVER ['HTTP_REFERER'], but this is not always set by the browser sending the request. You can also set a session variable when sending a response with a page identifier (for example, "about"). If this session variable is set, the requested previous page is in the variable. If it is not installed, the user simply landed on your website for the first time in the session.
You need to add another parameter that defines the source:
<img src="getimage.php?id=<?php echo $name ?>&src=about" "width="280px" height="70px"> In your PHP script, you can now use $ _REQUEST ['src'] to distinguish between different sources.
Do not use the referent as it is not reliable.