Solved

Dynamic part of PHP include URL not functioning properly

Posted on 2008-10-30
13
451 Views
Last Modified: 2011-10-19
Hello Experts!

I have defined a php include in my (MySQL) database as follows:
<?php include ('includes/travel_pic_thumbnails.php?poi_id=163'); ?>
...which i'm trying to use on my main site (www.globexposure.net/index.php) to display photos in a Google Maps Infowindow in conjunction with the Javascript thumbnail viewer, Highslide.
This can be seen by clicking on the Norwich (SE England) pushpin marker > arrow marker > home icon (hardcoded images) vs airplane icon (dynamic pictures loaded by php include).

The php script isn't being included however, so i have seperated the Google API code from the Highslide code to make this problem a little easier to follow. The current test site, www.globexposure.net/example-mini-gallery.php, displays a single image (as it should).
When this image is clicked, Highslide loads it and displays a controlbar for cycling through multiple images. The problem is the other images aren't being loaded by the php include, so it's not possible to cycle through them!

Initially i thought the php include itself was working well and the Highslide javascript was at fault, when in fact the Highslide forum has helped to establish that it's the other way round:
http://highslide.com/forum/viewtopic.php?t=2600&postdays=0&postorder=asc&start=0

The Highslide developer who has been helping me has got as far as diagnosing the problem as:
"Got to the cause, it's the dynamic part of the include url which seems to be the issue"
and...
"Can you try to find a different method of outputting the image links, I'm not happy with travel_pic_thumbnails script. There's a delay before images appear (the page loads first followed by the images). So I think what is happening is the include is working but it's pulling in a blank page."

This is as far as we have got - so i was hoping one you could help better explain and hopefully resolve this problem?


Many thanks for any help you can give me
0
Comment
Question by:Daniish
  • 8
  • 5
13 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22844257
> I have defined a php include in my (MySQL) database as follows:
does this mean that you generate the PHP code dynamically per request from that db?
0
 

Author Comment

by:Daniish
ID: 22848521
Yes i think so.

Though i have to hardcode that php include into each database record where i would like to display multiple images, because the rest of the site is written in client-side javascript, this was the only way i could think of doing it. So in this respect it's not too dynamic.
0
 

Author Comment

by:Daniish
ID: 22848563
MX3Design from the Highslide forum has managed to get the development site working:
www.globexposure.net/example-mini-gallery.php

BUT - he has had to drop the dynamic include. Instead his workaround is to place an index file in each photo sub-directory of /travel_pics/ - (see code snippet).

Obviously, if you can see a way that i can maintain the dynamic include that would be very much appreciated.

<?php

if ($handle = opendir('your/full/relative/path/to/includes/travel_pics/163')) {

    while (false !== ($file = readdir($handle))) {

        if ($file != "." && $file != ".." && $file != "index.php") {

            echo "<a href=\"includes/travel_pics/163/$file\" onclick=\"return hs.expand(this, { thumbnailId: 'thumb1' })\" class=\"highslide\"><img src=\"includes/travel_pics/163/$file\" alt=\"\" /></a><br />";

        }

    }

    closedir($handle);

}

?>

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22848576
just to be sure that I understand what you do:
  the request from the browser goes to a php page/script which queries a database which returns php code, and you want to execute that php code in your script and return the result to the browser

Is this correct?
0
 

Author Comment

by:Daniish
ID: 22848654
So just to clarify, the dynamic php include (see attached), which is called as follows:
<?php include ("./includes/travel_pic_thumbnails.php?poi_id=163"); ?>

has been dropped in favour of a simpler, albeit more laborious solution of using the following include:
<?php include ('includes/travel_pics/163/index.php'); ?>

...which requires the index file, as detailed in my last post, to be manually placed in each photo directory.
travel-pic-thumbnails.php.txt
0
 

Author Comment

by:Daniish
ID: 22848663
Yes that's right ahoffmann.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 22849649
Then you have to find a web/application server which "post processes" the result of your first php script again before it's passed to the browser.
IIRC there does not exist such a web server.

I guess you need to rethink about your architecture.
IMHO it's a bad idea to rerieve (php or any other) code from the database to be processed.

If you realy insist on your current solution --which I don't recommend-- then you have to change the code stored in the database to be plain php code (without the enclosing <?php ?> tags), and use php's eval() to execute it from within your script.
If you do that, ensure 101% (better 150%) that the code in the database is written by you, as it is very dangerous to evaluate dynamic code. You have been warned.
0
 

Author Comment

by:Daniish
ID: 22851241
Assuming that i currently include the following in my database record:

<?php include ("./includes/travel_pic_thumbnails.php?poi_id=163"); ?>

how would this be coded for using eval() ?
The example code provided on php.net (see attached snippet) is still enclosed by <?php ?> tags
<?php

$string = 'cup';

$name = 'coffee';

$str = 'This is a $string with my $name in it.';

echo $str. "\n";

eval("\$str = \"$str\";");

echo $str. "\n";

?>

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22851541
> Assuming that i currently include the following in my database record:
the database record should look like:

   include ("./includes/travel_pic_thumbnails.php?poi_id=163");

then you get this table cell and in a variable, let's say $str, then do

  eval($str);

BTW, what should the
   ?poi_id=163
do in this path? Remember that this include is a filesystem path and not a URL!
0
 

Author Comment

by:Daniish
ID: 22851700
So for example, a record in my database with the php include currently looks like this:

<div><a id="thumb1" href="/includes/travel_pics/163/Toby Texas Chainsaw Massacre.jpg" class="highslide" onclick="return hs.expand(this)">
<img style="float: left; margin: 0px 10px 8px 0px;" src="/includes/travel_pics/163/Toby Texas Chainsaw Massacre.jpg" width="100" height="100" alt="Highslide JS" title="Click to enlarge"/></a>
My family moved here, from College Road, when I was aged 12, after I spotted the house was for sale when we visited another further down the Close. We couldn't believe our luck when it was in our budget and this is where my folks have been based since 1992...and I dare say for a fair few years to come.
</div>
<div class="hidden-container">
<?php include ('includes/travel_pic_thumbnails.php?poi_id=163'); ?>
</div>

so how would this be encoded into a variable called $str ?

The ?poi_id=163 is passing the record 'id' in the url which is used by the travel_pic_thumbnails.php script to retrieve the relevant photos from a subfolder of /travel_pics/ that is named accordingly.

Will that still work?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22852274
dooh, as I said, you better redesign your architecture

For your example above, you need to parse what you get from the database and extract the parts being php code (those you enclosed in php tags), then eval that code and insert it again in your data.
Ugly, very ugly code ...

>  .. ?poi_id=163 is passing the record 'id' in the url
as I said: you don't have an URL here, it's a filesystem path.

Please read my suggestions carefully.
0
 

Author Comment

by:Daniish
ID: 22857669
OK sorry i see where your coming from now.

Personally, i would much prefer to keep my code seperate from my content and therefore not use a php include in the database, but i'm really not cluded up enough on php to know what alternative structure i could use.

In essence i need to display a single image with text pulled from mysql and when this image is clicked it uses the Javascript thumbnail viewer, Highslide, to open the image. - this i have working!

Highslide has a controlbar, which allows me to cycle through multiple images and i want it to do this for all the images that are in the same folder as the above image. The problem is the client-side Javascript can't read whats on the server, so i was trying to use php to dynamically pass the folder name that is based on the record primary key ('poi_id') back to the Highslide Javascript:
/includes/travel_pics/$poi_id/

I realise this is rather convoluted method but it's all that i could think of. Is there an obvious alternative architecture that i should consider?

Thanks again
0
 

Author Closing Comment

by:Daniish
ID: 31512481
Thank you for diagnosing the issue in my code. I will ask a new question to establish how it would be better to structure my code - please respond if you have any ideas.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now