4

I don't want to show the real URL of images. So I am using uniqueid() to replace the URL of dynamic images and store the real URL against the uniqueid() in a session. So I will get a load of uniqueids => real-image-path.jpg in the session array. Then using the following code to echo out image:

<img src="getImage2.php?h=<?php echo $uniqueID; ?>" title="" >

That all seems to work. In getImage2.php, I'm wondering how I can use the variables stored in the session to echo out the images. I have got:

session_start();
header("Content-Type: image/jpeg");
while($_SESSION[$uniqueID]){
echo readfile($_SESSION[$uniqueID]);
}

But this looks pretty hopeless and obviously isn't working. What is the best way to echo the images in getImage2.php?

Sample of session array looks like this:

Array ( [start] => 1435057843 
        [start_date] => 23/06/15 
        [start_time] => 12:10:43 
        [b312a3f205176aa006c8712b3aedb2a4] => images/1370322222.jpg 
        [5311d8a77e3889723a61b3768faaa4df] => images/1357323650.jpg 
        [fa14a6a315bf7ddbeb7390af23467f5e] => images/1415737586.jpg 
        [dd624079e982d78e538f873b7719f179] => images/1369865823.jpg 
        [5c4011114f6abbb9ecaf2ffbe4d3936f] => images/1369885151.jpg 
        [d26e3a017ce4851a19511fc0dfedc595] => images/1370317410.jpg
        .............
Oldskool
  • 34,211
  • 7
  • 53
  • 66
user1022772
  • 651
  • 3
  • 14
  • 29
  • In some cases you can do internal redirects, that would save up your PHP from having to serve the file; also, have a look at [this answer](http://stackoverflow.com/a/10596231/1338292) for some inspiration. – Ja͢ck Jun 23 '15 at 11:41

3 Answers3

0

You should not use a while loop or readfile here. Instead, try this:

session_start();
header("Content-Type: image/jpeg");
echo file_get_contents($_SESSION['$uniqueID']);

I'm assuming you are trying to output one image at a time.

Michael
  • 1,247
  • 1
  • 8
  • 18
  • want to display all images on main page but get the real urls from getImage2.php. Do I not need to display all images on getImage2.php? Also - isn't file_get_contents more memory intensive than readfile()? – user1022772 Jun 22 '15 at 20:08
  • As @RiggsFolly mentioned, each img tag on your main page will send a request for one image to getImage2.php, so you only need to output one at a time. Using readfile is also fine, just not in a loop like in your question. – Michael Jun 22 '15 at 20:13
0

You dont want to try and echo all the images at once. Each <img> tag will send an individual request to the server to be served a specific image as part of the page load, so just get the PHP code on the server to return the specific image requested like so :-

getImage2.php script

session_start();

$filename = 'images/missing_image.jpg'; // set a default

if ( isset($_GET['h']) && isset($_SESSION[ $_GET['h'] ]) ) {
    $filename = $_SESSION[ $_GET['h'] ]
}

header("Content-Type: image/jpeg");
readfile( $filename );

NOTE RE Your comment

As you are using this

<img src="getImage2.php?h=<?php echo $uniqueID; ?>" />

which will get written to the browser as something like this

<img src="getImage2.php?h=b312a3f205176aa006c8712b3aedb2a4" />

The $_GET array will look like this when getImage2.php received it.

$_GET [
         h => b312a3f205176aa006c8712b3aedb2a4
      ]

So $_GET['h'] will be what you are calling the uniqueID and you use that to address the correct file path by using it as the key to the $_SESSION array

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • isn't 'h' just the uniqueID(). The real image path is contained in session as uniqueID =>real-image-path.jpeg. So dies $_SESSION[$_GET['h']] = real image path? – user1022772 Jun 22 '15 at 20:50
  • See my NOTE re your comment – RiggsFolly Jun 22 '15 at 21:05
  • Thanks for your help and sorry for being dense here. How do I get the real image path which is = to the uniqueID()? Does the line readfile($_SESSION['GET['h']]); look at the current session and find the value of the uniqueID (taken from the img source) and then echo the real image path? – user1022772 Jun 22 '15 at 21:21
  • Yes it should, assuming I have made all the right assumptions about how you built the $_SESSION array data. Do a `print_r($_SESSION)` and edit your question and add a sample of that output so I can see what your $_SESSION actually looks like – RiggsFolly Jun 22 '15 at 21:32
  • Thanks RiggsFolly. Have edited question with sample of session array. – user1022772 Jun 23 '15 at 11:17
  • So all that remains is for you to try out the suggested code. – RiggsFolly Jun 23 '15 at 11:48
  • Have always had broken images on main page. But since changing code to include a default image as you suggest all images on main page now show the default image. Feels like a step closer so thank you. Looks like variable not set or not being passed through to getImage.php or that the real image path from session array not being picked up? – user1022772 Jun 23 '15 at 15:52
0

This should get you started along the right path:

session_start();
// get passed 'h' parameter and look it up in the session
$handle = isset($_GET['h']) ? $_GET['h'] : null;
$image = isset($_SESSION[$handle]) ? $_SESSION[$handle] : null;

if (file_exists($image) {
    // the file can be found
    header('Content-Type: image/jpeg');
    header('Content-Length: ' . filesize($image));

    session_abort(); // close session without changes
    readfile($image);
} else {
    // file was not found
    if ($image !== null) {
        // we should invalidate the session
        unset($_SESSION[$handle]);
    }
    header('HTTP/1.0 404 Not found');
}

Though, typically a better approach to this is by using X-Sendfile or X-accel headers; you can read more about this from my older answer.

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309