Session Array Echo URLs

I do not want to show the real URL of the images. Therefore, I use uniqueid()to replace the URL of dynamic images and keep the real URL against uniqueid()in the session. So I get the load uniqueids => real-image-path.jpgin the session array. Then, using the following code for the echo image:

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

It all works. As getImage2.phpI'm wondering how I can use variables that are stored in the session for the echo picture. I have:

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

But it looks pretty hopeless and obviously doesn't work. What is the best way to echo an image in getImage2.php?

An example session array is as follows:

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
        .............
+4
source share
3 answers

. <img> , PHP , , : -

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 );

RE

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

-

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

$_GET , getImage2.php .

$_GET [
         h => b312a3f205176aa006c8712b3aedb2a4
      ]

, $_GET['h'] , uniqueID, , $_SESSION

0

while readfile. :

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

, .

0

:

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');
}

Although, as a rule, the best approach to this is to use X-Sendfile or X-accelerators; You can learn more about this from my older answer .

0
source

All Articles