PHP sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

PHP sessions

Post 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"]);  
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

$_SESSION["photo"]=$fn;
That will always just contain the last file in the directory.

You'll need to store all the filenames in an Array for example
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

On each itteration...

Code: Select all

echo"$fn<br>";
...will output a different value, and on each itteration...that value is stored here...

Code: Select all

$_SESSION["photo"]=$fn;
So, on the last itteration...

Code: Select all

$_SESSION["photo"]
...will be set to the value on the last itteration
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

It's like

Code: Select all

<?php
$a = 1;
$a = 2;
$a = 3;
$a = 4;

echo $a;
?>
What will echo $a; print?
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Even if so, what would change?
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?).
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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..
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

Post 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).
Post Reply