简体   繁体   中英

How to differentiate a request for a resource on a page from the actual page request in PHP

I am requesting an html file from the server A where the server A downloads it from another server(B) and send it to the original client. This html file has images embedded in img tags like <img src="abc.png"> . In order to download those images successfully in the successive requests, I need to add a prefix to the src path like "prefix/abc.png" once server A receives the successive request for the image.

Now my problem is, is there a way I can figure out whether a request is for the page or for a successive request for a resource like image or script so I can add the prefix to the path properly?

Isn't is just as simple as checking if the requested path terminates in .png, .gif, .jpg etc... ? ie

/* $reqPath is for example /the/stuff/i/want/image.png */

if (in_array(strtolower(substr($reqPath, -4)), ['.png', '.jpg', '.gif', '.svg']) {
    /* It's an image! */
} else {
    /* Must be something else */
}

I added the strtolower just in case you have a request for IMAGE.PNG, Image.Png or something silly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM