使用文件名获取数据并将 PHP 页面输出为 JPG 扩展名?
好吧,目前我正在使用 GD 创建一个 PNG 图像,根据 GET 数据记录一些不同的内容,例如:http://example.com/file.php ?do=this 将记录一件事,而 http://example.com/file.php?do=that
将记录另一件事。
但是,我想在没有 GET 数据的情况下执行此操作,因此 http://example.com/dothis.php
将记录一件事,并且 http://example.com/ dothat.php
将记录另一个。
但最重要的是,我还想通过 JPG 文件扩展名来访问它。我已经看到这个完成了,但我不知道如何完成。这样 http://example.com/dothis.JPG
将记录一件事,而 http://example.com/dothat.JPG
记录另一件事。
当然,日志记录部分很简单。我只需要知道如何使用文件名代替 GET 数据以及如何将 php 文件设置为可通过 jpg 文件扩展名访问。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文件名可以通过 $_SERVER 数组访问(我希望你可以探索这个数组并找到合适的变量),
而扩展技巧是网络服务器的责任
对于 Apache 模块 API 来说,它可以是
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
如果你不想有重复的脚本/链接,并且网络服务器是apache,那么你需要它的mod_rewrite,例如
C。
If you don't want to have duplicate scripts / links, and the webserver is apache, then its mod_rewrite you need, e.g.
C.
如果您想将 PHP 嵌入到使用扩展名 .jpg 的文件中,您需要指示您的 Web 服务器将 .jpg 文件解析为 PHP。如果您使用 Apache,执行此操作的一种方法是将以下内容添加到文件所在目录中的 .htaccess 文件中:
或者您可以将其添加到服务器配置中。请参阅此页面了解有关 Apache htaccess 的详细信息。
接下来,编写脚本,以便它们记录适当的数据。在您的示例中,看起来您不再需要 GET 数据,并且脚本将仅根据调用它的事实来记录不同的数据。即 dothis.jpg “知道”记录
this
数据,而 dothat.jpg 记录that
数据。dothis.jpg:
dothat.jpg:
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:
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. i.e. dothis.jpg "knows" to log
this
data, whereas dothat.jpg logsthat
data.dothis.jpg:
dothat.jpg: