Page 1 of 2
PHP sessions
Posted: Tue Aug 01, 2006 3:47 am
by Kestas
Hi!
Need some help with PHP sessions(I quess so). My php code should be outputting to browser all images in a specified folder, but for some reasons it outputs always the same image - the last one in folder. It reads files correctly, dislplays their names. It`s just the images that are always the same. I`m stuck here. Has it got to do with the session variables?
index.php
Code: Select all
<? session_start();
header("Content-type: image/jpeg");
$maindir="C:/web/docs/thumbs/";
$mydir = opendir($maindir);
$exclude = array(".", "..", "Thumbs.db") ;
while($fn = readdir($mydir)) {
if (!in_array($fn, $exclude)){
echo"$fn<br>";
$_SESSION["directory"]=$maindir;
$_SESSION["photo"]=$fn;
echo'<img src="next.php"/><br>';
}
}
closedir($mydir);
?>
next.php
Code: Select all
<?php
session_start();
readfile($_SESSION["directory"].$_SESSION["photo"]);
?>
Posted: Tue Aug 01, 2006 3:49 am
by JayBird
That will always just contain the last file in the directory.
You'll need to store all the filenames in an Array for example
Posted: Tue Aug 01, 2006 6:23 am
by Kestas
But everytime $_SESSION["photo"] is being accessed from "next.php" it should have different values. At every iteration $fn changes, so should $_SESSION["photo"]. Why does it not happen so?
Posted: Tue Aug 01, 2006 6:27 am
by JayBird
Kestas wrote:But everytime $_SESSION["photo"] is being accessed from "next.php" it should have different values. At every iteration $fn changes, so should $_SESSION["photo"]. Why does it not happen so?
Not according to the code you posted
Posted: Tue Aug 01, 2006 6:45 am
by Kestas
I am completely lost now:
Code: Select all
echo"$fn<br>"; // outputs image name(different every itteration)
$_SESSION["photo"]=$fn; // $_SESSION["photo"] equals to whatever $fn is, so should be different every iteration too
Please explain me: where am I wrong?
Posted: Tue Aug 01, 2006 6:57 am
by JayBird
On each itteration...
...will output a different value, and on each itteration...that value is stored here...
So, on the last itteration...
...will be set to the value on the last itteration
Posted: Tue Aug 01, 2006 6:58 am
by volka
It's like
Code: Select all
<?php
$a = 1;
$a = 2;
$a = 3;
$a = 4;
echo $a;
?>
What will
echo $a; print?
Posted: Tue Aug 01, 2006 7:14 am
by Kestas
Yes, I know what you mean, but in my code echo'<img src="next.php"/>' is called not after but during itterations. I just thought maybe output to the browser by PHP is being sent only after all iterations had been done, not during them?
Posted: Tue Aug 01, 2006 7:24 am
by volka
Even if so, what would change?
Posted: Tue Aug 01, 2006 7:35 am
by Kestas
If output to the browser is sent after all iterations had been done, then of couse $_SESSION["photo"] will always stay the same(the last value). Otherwise it should be different every time.
Posted: Tue Aug 01, 2006 7:41 am
by volka
Oh, no it wouldn't.
Please try
Code: Select all
<?php
session_start();
header("Content-type: image/jpeg");
$maindir="C:/web/docs/thumbs/";
$mydir = opendir($maindir);
$exclude = array(".", "..", "Thumbs.db") ;
while($fn = readdir($mydir)) {
if (!in_array($fn, $exclude)) {
$_SESSION["directory"]=$maindir;
$_SESSION["photo"]=$fn;
echo $_SESSION["photo"], '<br />';
echo'<img src="next.php"/><br>';
}
}
closedir($mydir);
?>
So you see, there's no way $_SESSION["photo"] is not overwritten in each iteration (or where would echo get it's data from?).
Posted: Thu Aug 03, 2006 6:58 am
by Kestas
I understand that. To explain what I don't understand, I will give you an example:
In "C:/web/docs/thumbs/" we have files : 1.jpg, 2.jpg, 3.jpg.
When itterations start , first itteration sets $_SESSION["photo"] to "1.jpg".
Value of $_SESSION["photo"] is outputed to the browser(up until here everyhting works fine).
Then "next.php" should access $_SESSION["photo"] and putput an image "1.jpg" to the browser and next itteration will be started.
But "next.php" doesn't output current file("1.jpg"), it always outputs the last one("3.jpg").
How it comes? Where am I wrong?
Posted: Thu Aug 03, 2006 7:01 am
by JayBird
Kestas wrote:I understand that. To explain what I don't understand, I will give you an example:
In "C:/web/docs/thumbs/" we have files : 1.jpg, 2.jpg, 3.jpg.
When itterations start , first itteration sets $_SESSION["photo"] to "1.jpg".
Value of $_SESSION["photo"] is outputed to the browser(up until here everyhting works fine).
You are correct there. Now, at this point $_SESSION["photo"] now contains "3.jpg"
.
Kestas wrote:Then "next.php" should access $_SESSION["photo"] and putput an image "1.jpg" to the browser and next itteration will be started.
Now you are on next.php $_SESSION["photo"] still contains "3.jpg" becuase that is what you set it to in the loop on index.php
Kestas wrote:But "next.php" doesn't output current file("1.jpg"), it always outputs the last one("3.jpg").
How it comes? Where am I wrong?
Posted: Thu Aug 03, 2006 7:36 am
by Jenk
To make things easier.. try to understand that everything in PHP is compiled BEFORE the user sees ANYTHING on their screen.
Therefore, before the user can even see the link, let alone click it, the value of $_SESSION['photo'] will be the last file in the directory.
As in volka's example, that is what you are doing with $_SESSION['photo'].
What is most viable for you to do, is use a query string variable on your links to identify which photo the user wants to view.. ergo:
Code: Select all
<?php
session_start();
$maindir="C:\\web\\docs\\thumbs\\";
$_SESSION["directory"] = $maindir;
$mydir = opendir($maindir);
$exclude = array(".", "..", "Thumbs.db") ;
while($fn = readdir($mydir)) {
if (!in_array($fn, $exclude)) {
echo htmlentities($fn) . '<br />';
echo'<img src="next.php?photo=' . htmlentities($fn) . '"/><br>';
}
}
closedir($mydir);
?>
Then on next.php use $_GET['photo'] to identify the file in question, you can still use $_SESSION['directory'] to keep the directory in as that is constant throughout the images. If you change directory within the same page however, you will need to find alternative means of passing it between pages..
Posted: Thu Aug 03, 2006 8:31 am
by Kestas
Thanks a lot!
Now it's all clear to me. All that I needed was "everything in PHP is compiled BEFORE the user sees ANYTHING on their screen"(by Jenk).