Jump to content

Using PHP in a cache page


allycatM

Recommended Posts

2 minutes ago, Viajero Perdido said:

Nope.

 

But you could, instead, load an image from a server you control, a server that would serve different versions of the image depending on the time of day (or whatever criteria).  I know of a puzzle done that way...

Would that server be a website? Thanks for the quick response, but I'm not really an expert on this stuff...

Link to comment

Doesn't have to be a full website.  But if you have access to a server that can run scripts and can serve up at least parts of a web page, you could do something like this:  In your cache page, embed an image like

https://my-company-name.com/side-projects/GC12345.jpg

 

As far as the cache page is concerned, it's just a simple image.  But your server script would serve different versions of the image, both with the same purported name, depending on criteria like time of day.

 

For security reasons, Groundspeak can't allow active content in cache pages, so no PHP, no Javascript, and even creative CSS (busting outside the box) is frowned upon.

 

Edited by Viajero Perdido
Link to comment

Here are a couple of discussions on how it may be done as a PHP script:

https://arstechnica.com/civis/viewtopic.php?f=20&t=284571

http://www.dynamicdrive.com/forums/archive/index.php/t-15561.html

 

I used to check out various sites that have ready-made scripts for download, and build on them.  They of course require a server to run them.

 

Edited by kunarion
Link to comment

<?php
$h = date('G'); //set variable $h to the hour of the day
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.

 if ($h < 20) $img = 'frogs.jpg';
else $img = 'potatoes.jpg';

//if it's before 8pm, use frogs
//otherwise, potatoes
?>

 

So this is all I need to put in my server, plus the HTML code that would run it?

Link to comment

Would I just have to do this?:

 

<html>

<title>

mypage

</title>

<body>

<?php
$h = date('G'); //set variable $h to the hour of the day
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.

 if ($h < 20) $img = 'frogs.jpg';
else $img = 'potatoes.jpg';

//if it's before 8pm, use frogs
//otherwise, potatoes
?>

</body>

</html>

 

 

But then, how would I put it in to my cache page?

 

Link to comment
1 hour ago, allycatM said:

Could I adapt this for use with text instead of an image?

 

You must convert your text to image first. The result must be an image because only images can be served from external sources. There are some mystery caches which displays dynamic text content that way. I have one mystery which combines current geochecker results with real time weather forecast to a single image. You can do anything you want with this kind of dynamic images.

Link to comment
30 minutes ago, Viajero Perdido said:

I don't think you can simply pull in a bit of text from another server (willing to be corrected; I retired before HTML5).  An image, yes, but text?  Doubt it.

 

Sorry to keep bearing bad news...

 

Maybe it would work if that part that changes was done by a script on the other server.  It's unusual, but a cache could be published like that.  Ask the reviewer.

 

I also wonder if there's text software that can edit the cache page on a schedule.  There used to be a lot of web editing software that ran on a PC but did fancy things to a "static" web site.  Even then, it seems better if it were a paragraph on some site and that's the text that may change, with a link to it from the cache page.

 

Link to comment
22 minutes ago, kunarion said:

Maybe it would work if that part that changes was done by a script on the other server.

 

I wouldn't try that solution because it is guaranteed to fail when the cache editing tool changes. It is also against the terms of use.

Quote

 

You agree not to:

  1. Use any robot, spider, scraper or other automated means to access our services for any purpose without our express written permission.

 

 

Edited by arisoft
Link to comment
37 minutes ago, arisoft said:

 

I wouldn't try that solution because it is guaranteed to fail when the cache editing tool changes. It is also against the terms of use.

 

 

The part you quoted is what I said is on another server, the site would go get it by request (yeah, maybe it only works with an image, and a different image at different times), and that's absolutely 100% OK, just unusual.  Yeah, I'm not excited about editing the cache page, and said so.  Have the mysterious text entirely on the other server, with a link to it.  Is there an echo?  I said exactly that in that exact same post. :unsure:

 

Edited by kunarion
Link to comment

Ok, so if I stick to an image (that seems like what you are suggesting), would I link the image into my cache page, or just link my server to the cache page and have it display a different picture at a different time? Not sure if you explained this already, but it is still unclear to me.

Link to comment
3 hours ago, allycatM said:

Ok, so if I stick to an image (that seems like what you are suggesting), would I link the image into my cache page, or just link my server to the cache page and have it display a different picture at a different time? Not sure if you explained this already, but it is still unclear to me.

You would have your cache listing display an image. The URL of that image would actually be a server-side program. That server-side program would return different images, depending on the time or whatever else you want it to depend upon.

 

One thing to keep in mind is that browses don't always retrieve new versions of images. Your server-side program will need to specify expiration headers that tell browsers that they need to retrieve a new version, rather than displaying a cached obsolete version.

Link to comment
6 hours ago, niraD said:

You would have your cache listing display an image. The URL of that image would actually be a server-side program. That server-side program would return different images, depending on the time or whatever else you want it to depend upon.

 

One thing to keep in mind is that browses don't always retrieve new versions of images. Your server-side program will need to specify expiration headers that tell browsers that they need to retrieve a new version, rather than displaying a cached obsolete version.

 

Best answer so far.  The server side program could be written in PHP (as long as your web server is configured to interpret PHP into HTML), perl, java, python, ruby or pretty much any other programming language.  The key is that the web application has to recognize the request to a URL as a request to execute the server side program.  That can be done by configuring the web application server to recognize a specific extension (e.g jpg) as a route to a server side program.  The server side program can do whatever necessary to construct a response (which could be an actual image.   In the cache description one can use an image tag such as this 

<img src="http://myserver.mydomain.org/custom_image.jpg" />

If you want the "image" to render inline on the cache description.  

Link to comment

Just a minor point that might clarify something:

 

The cache listing is solely front-end HTML, and somewhat sanitized for safe content. PHP is a server-side script. Groundspeak absolutely won't let server-side script run via the cache listings, so PHP must be created on an external server you have access to.  HTML in the cache listing can display images fro mmost any other website, so that's why you'd need to run the PHP on an external website you have sufficient access to, to dynamically create the image that can be shown in the cache listing.

 

You could send information to the PHP via the img SRC tag after the ? in a querystring (eg: "http://myserver.com/myimage.jpg?additionalStuff") but that additional stuff can't be dynamic (again since the HTML has to be static)

 

Creating a dynamically changing cache page isn't a quick task :P As mentioned above, you also need to work through the quirks of http headers to make sure no user's browser caches the image so that it's always shown with the content you send it.  It can be finnicky.

 

I've got 2 that make use of serverside dynamic images... GC2ZG24  and GC2P887

Link to comment
29 minutes ago, thebruce0 said:

You could send information to the PHP via the img SRC tag after the ? in a querystring (eg: "http://myserver.com/myimage.jpg?additionalStuff") but that additional stuff can't be dynamic (again since the HTML has to be static)

 

Query url must be static but cookies may have dynamic content to memorize the state what happened earlier. For example to count how many times the user have visited the cache page.

Edited by arisoft
Link to comment
1 hour ago, thebruce0 said:

Creating a dynamically changing cache page isn't a quick task :P As mentioned above, you also need to work through the quirks of http headers to make sure no user's browser caches the image so that it's always shown with the content you send it.  It can be finnicky.

 

If I tried it, I'd probably make an image file name as mentioned previously, and a script that changes the image, naming it the same file name when it does so.  That would all be on a separate server.  I'd have the URL to the image as part of the cache description (or in the cache page Gallery, or both).

 

I've done this kind of thing in the past, not for Geocaches, but static web pages with dynamic images.  As you mentioned, it's kind of a headache to have the desired image show and not just be the old image "cached" in the browser.  It's pretty much different for every browser, and people can change settings that make it worse for this situation.  For Geocaching purposes, I might be super evil and simply let it do whatever people's browsers do, and raise the Difficulty a little.  I'm not good at coding but I'm evil.  So it all works out.

 

Edited by kunarion
Link to comment
3 hours ago, arisoft said:

Query url must be static but cookies may have dynamic content to memorize the state what happened earlier. For example to count how many times the user have visited the cache page.

 

The cookies will only be accessible on geocaching.com.  The external server can set cookies with the image, but they likewise won't be read by gc.com.  The URL to the image on the listing must remain static. But if you're using cookies to store state information with the image, I don't know why you would use a server-side session state, since means only 1 cookie needs to be handled by the browser - the user's sessionid - rather than you dropping all your variables into the browser's cookie collection to be sent and received with every image load.

 

Which raises another point - if you want to retain memory of a user's activity to dynamically change the image shown on the listing, the user has to have cookies enabled. Without cookies, every view of the image will effectively be a new session. No memory. Unless you're tracking the user by something that isn't specifically bound to the browser session. You could, for instance, assume that every access from a single IP address is the same user. But that breaks if you have say a family accessing the listing from different computers and accounts. Or if someone accessing on their mobile device happens to be given a different IP in the middle of their 'playing'.

 

Basically, the user should hvae cookies enabled, and if you want to remember a user's progress you need to use the server's session state to store variables across image requests.  You have to be able to alter the image as necessary with every image load from one single static URL.  (Unless you design the experience with some little tricks, like GC2P887

Link to comment

Ok. I think I'm almost there. I just need one more thing answered: how often does geocaching.com update it's cached images? Can I change that for my cache page? Thanks for all of your help, guys! You've really been helpful. Also, for the code mentioned below, do I have to actually fill in 'G'? Or is it fine as is?

On 9/1/2018 at 4:37 PM, allycatM said:

$h = date('G'); //set variable $h to the hour of the day

 

Link to comment
5 hours ago, allycatM said:

how often does geocaching.com update it's cached images?

 

Newer, because geocaching.com have no access to the cached images, it can not even try to update.

 

Decision, when the image is updated in your browser, depends on HTTP headers received with the image and the URL requesting the image. Easy way to prevent caching is to add a query field to the address. Normally the image server sets appropriate headers to prevent caching if the caching is not desired.

Link to comment
14 hours ago, kunarion said:

For Geocaching purposes, I might be super evil and simply let it do whatever people's browsers do, and raise the Difficulty a little.  I'm not good at coding but I'm evil.  So it all works out.

 

Tapping my evil tendencies I thought about how one might create a puzzle cache with an image on a remote server, that when one clicks on the image it returns an image that looks like a Error 404 or System Error 500 page.  The returned image would use steganography to hide the coordinates.

Link to comment
9 hours ago, allycatM said:

Ok. I think I'm almost there. I just need one more thing answered: how often does geocaching.com update it's cached images? Can I change that for my cache page? Thanks for all of your help, guys! You've really been helpful. Also, for the code mentioned below, do I have to actually fill in 'G'? Or is it fine as is?

 

 

The PHP date function page (http://php.net/manual/en/function.date.php) indicates that the parameter is the "format" for the date and a "G" will return the 24 hour format of an hour without leading zeros.  Basically, it'll return an integer in the range of 0 to 23 depending on the time of day.  

 

I've come across several puzzle caches recently that display a different image depending on the the country from which the user is accessing the page.  The idea is to share results with people from other countries so that the full coordinates can be obtained.  The cache page indicates one can use the logs to state that they have the answer from their country and are looking for the rest.   There is a cache in Ireland close to where I was going to be staying a couple of weeks ago done this way.  I posted a note and two different people sent me the final coordinates rather just their country portion.

 

 

  • Helpful 1
Link to comment
1 hour ago, NYPaddleCacher said:

 

Tapping my evil tendencies I thought about how one might create a puzzle cache with an image on a remote server, that when one clicks on the image it returns an image that looks like a Error 404 or System Error 500 page.  The returned image would use steganography to hide the coordinates.

 

That would be great! :P 

 

I wonder if there's a sneaky way to use the teeny square icon with an X in it, the box you get when an image doesn't load.

 

Edited by kunarion
Link to comment
On 9/2/2018 at 10:15 PM, allycatM said:

Ok. I think I'm almost there. I just need one more thing answered: how often does geocaching.com update it's cached images? Can I change that for my cache page?

 

Nope, image caching is done by the browser not the website that refers to and embeds the remote image.  You need to provide the proper http headers when serving the image from your PHP to tell the browser how long to cache the image (or not at all).

 

On 9/3/2018 at 3:39 AM, arisoft said:

Newer, because geocaching.com have no access to the cached images, it can not even try to update.

 

Decision, when the image is updated in your browser, depends on HTTP headers received with the image and the URL requesting the image. Easy way to prevent caching is to add a query field to the address. Normally the image server sets appropriate headers to prevent caching if the caching is not desired.

 

No, adding the querystring to the IMG tag in the listing still makes it a static request that can be cached by the browser. That trick works if you can dynamically create the IMG tag and change the querystring value every time the HTML loads - but not so here.

Link to comment
1 hour ago, thebruce0 said:

No, adding the querystring to the IMG tag in the listing still makes it a static request that can be cached by the browser. That trick works if you can dynamically create the IMG tag and change the querystring value every time the HTML loads - but not so here.

 

Yes, it seems to work only with some proxy servers which cease to cache content with a query string.

Link to comment
6 hours ago, arisoft said:
7 hours ago, thebruce0 said:

No, adding the querystring to the IMG tag in the listing still makes it a static request that can be cached by the browser. That trick works if you can dynamically create the IMG tag and change the querystring value every time the HTML loads - but not so here.

 

Yes, it seems to work only with some proxy servers which cease to cache content with a query string.

 

Right, so not a guarantee, and not a sufficient solution; more like a quirk that could be taken advantage of sometimes, but not in a controlled manner.

Edited by thebruce0
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...