Forum Moderators: coopster
file1.htm contains the link: <a href="/picture-show.htm?image1.jpg">Pic1</a>
file2.htm contains the link: <a href="/picture-show.htm?image2.jpg">Pic2</a>
etc.
The script would be on picture-show.htm, and display the image requested via the link info. The pics will be in different directories, so the script must be able to handle sub-directories in the link:
<a href="/picture-show.htm?images/folder1/image3.jpg">Pic3</a>
Basically, this is a thumbnail viewer that points to a full-size image displayer. As I say, I already have a script that does everything, except that it's set up to point to a unique URL for each full-size image. This means I have to copy my image display template page over & over for each image (plus edit the filename for the image that is to display). It also means that the user's browser must load a new display page for every image, which adds to download time. A single dynamic display page only has to load once, and the only new content is the pic.
(No, I don't want to just point to the image URL itself; I want it to display on a template page with other content.)
I don't know PHP, but I thought this would be a simple script. Here's all it has to do:
[ Assume our display page is named pic-display.php, and a typical link it may receive is: /pic-display.php?/images/red/pic1.jpg ]
1) Read the appended value on the link, identifying the image: /images/red/pic1.jpg
2) Print the image via a template (say, pic-display.tpl)
That's it. I've already got the template -- all I would need is the variable name used by the PHP script, e.g. {IMAGE}.
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
// You should likely do some editing here
// to make sure the query string is legit,
// security first you know ;-)
if (the QUERY_STRING value looks legit) {
$myImage = $_SERVER['QUERY_STRING'];
}
} else {
// Some kind of default pic or something
// for when the QUERY_STRING isn't there
} <img src="<?php print $myImage; ?>" />
Do you mean to insert your PHP code & variable string into my HTML template?
I tried that; the page loads and displays fine, but no image shows. Here's what I added to my HTML (I ignored the security issue for now; just want to get it working first):
[in the <head> section]:
<?php
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$myImage = $_SERVER['QUERY_STRING'];
}
?>
[in the <body> section]:
<img src="<?php print $myImage;?>" />
==========
I couldn't get it to work in separate files either. First I just used the above PHP code in a file named picviewer.php, and made a template (pic-show.tpl) with the variable $myImage in it.
When I went to the URL: [mysite.com...] I got a blank screen -- obviously we need to call up the template (which you probably assumed I'd know how to do!) I don't know PHP, but here's what I pieced together from a working PHP part of my website:
[picviewer.php]
<?php
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$myImage = $_SERVER['QUERY_STRING'];
}
// Load template
$template->set_filenames(array(
'body' => 'pic-show.tpl')
);
// Send variable to template
$template->assign_vars(array(
'IMAGE' => $myImage
)
);
// Display template
$template->pparse('body');
?>
When I went to the URL as before, I got this error message:
Fatal error: Call to a member function on a non-object in this line - $template->set_filenames(array(
That's about as far as I can go on my own.
I don't know what the source shows for the image src in the separate file method either, because I'm getting the error message that I indicated; when I go to view source, all that's there is <br />, then the error message.
From the error message, something's wrong with my line:
$template->set_filenames(array(
'IMAGE' => $myImage
)
);
I pulled that from a file that sends multiple variables to the template, hence the 'array'. I tried simply deleting the array reference (and associated parentheses); that seemed to take care of that issue, but then I got this error message:
Parse error: parse error, unexpected T_DOUBLE_ARROW in the next line:
'IMAGE' => $myImage
[that whole code section reads as follows]:
// Send variable to template
$template->assign_vars(
'IMAGE' => $myImage
);
There are a couple of ways to dump variables, one of which is var_dump() [php.net]. Another that comes in handy when dumping arrays is print_r() [php.net]. I think you'll find both much easier to read if you enclose them in html <pre> elements when you dump them. Example:
<?php
print '<pre>';
var_dump($myImage);
print '</pre>';
exit;
?>
After:
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$myImage = $_SERVER['QUERY_STRING'];
}
Result:
'string(29)' then the CORRECT IMAGE URL in quotes "" -- (yeah!)
After:
// Load template
$template->set_filenames(array(
'body' => 'pic-show.tpl')
);
Result:
Fatal error: Call to a member function on a non-object in this line: $template->set_filenames(array(
That's the same error I got before. When I deleted the array reference, I got this error:
Parse error: parse error, unexpected T_DOUBLE_ARROW in this line: 'body' => 'pic-show.tpl'
In the PHP manual, it says the T_DOUBLE_ARROW applies to arrays, so I tried a simple = sign. That gave a similar error: parse error, unexpected =
So this is the section that's hanging things up:
// Load template
$template->set_filenames(array(
'body' => 'pic-show.tpl')
);
I assume I don't need the array reference in there, but I don't know how to code it without that.
Barring that, I got the whole thing to work a different way, but I'm not sure if this is good design practice; let me know. Here's what I did:
I have one file, called pic-show.php. It opens with your code:
<?php
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$myImage = $_SERVER['QUERY_STRING'];
}
I then added:
print("
Then I pasted my entire HTML code, and after that, closed the whole page with:
") // to close the print() function
?>
I also escaped all quotation marks " in the HTML using \"
For the image variable, I simply used:
<img src=\"$myImage\">
If this is an acceptable way of doing things, I only have one glitch. This page will open in a new window when the user clicks on a thumbnail. In the HTML, I have a simple button that the user clicks to close the window (to return to the thumbnails). The code is (without the backslash escapes):
<input type="button" value="Close Window" onClick="window.close()"></form>
Now, in the PHP script, when you click the button, Internet Explorer comes up with an alert message that says, "The web page you are viewing is trying to close the window. Do you want to close the window?"
Is there a php command I can use instead to avoid that annoying message each time?
Also, now that I have a working script, I'd like to take care of any security issues. My image directories are already protected, and the true image URL's are disguised to the viewer. If there are other security issues, please give me a quick rundown or point me to a place I can read up on them.
Thanks.
<a href="/picviewer.php?image=/pic1.gif">Pic1</a>
and picviewer.php will automatically assign
$image = /pic1.gif
Is this better than using:
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) {
$myImage = $_SERVER['QUERY_STRING'];
}
It seems simpler. Is the security issue the same, less, or more?