简体   繁体   中英

Using the filename for GET data and making the PHP page output as a JPG extension?

Alright, currently I'm using GD to create a PNG image that logs a few different things, depending on the GET data, for example: http://example.com/file.php?do=this will log one thing while http://example.com/file.php?do=that will log another thing.

However, I'd like to do it without GET data, so instead http://example.com/dothis.php will log one thing, and http://example.com/dothat.php will log the other.

But on top of that, I'd also like to make it accessible via the JPG file extension. I've seen this done but I can't figure out how. So that way http://example.com/dothis.JPG will log one thing, while http://example.com/dothat.JPG logs the other.

The logging part is simple, of course. I simple need to know how to use filenames in place of the GET data and how to set the php file to be accessible via a jpg file extension.

filename is accessible via $_SERVER array (I hope you can explore this array and find suitable variable)

while extension trick is web-server responsibility
For the Apache module API it could be

RemoveHandler .jpg
AddType application/x-httpd-php .jpg

If you don't want to have duplicate scripts / links, and the webserver is apache, then its mod_rewrite you need, eg

RewriteEngine On
RewriteRule ^doth/(.*) index.php?page=$1 [L]

C.

If you want to embed PHP inside a file using the extension .jpg, you will need to instruct your web server to parse .jpg files as PHP. One way to do this, if you are using Apache, is to add the following to an .htaccess file in the directory where the files are located:

addtype application/x-httpd-php .jpg .php

Or you can add it to your server configuration. See this page for details on htaccess for Apache .

Next, write your scripts such that they log the appropriate data. In your example, it looks like you won't need GET data anymore, and the script will log different data based simply on the fact it was called. ie dothis.jpg "knows" to log this data, whereas dothat.jpg logs that data.

dothis.jpg:

<?php
    header("Content-type: image/jpeg");
    // log "this" code
    $img = imagecreate($x, $y); // example
    // GD image generation code
    imagejpg($img);
?>

dothat.jpg:

<?php
    header("Content-type: image/jpeg");
    // log "that" code
    $img = imagecreate($x, $y); // example
    // GD image generation code
    imagejpg($img);
?>

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