Thread: Strange session problem

Strange session problem

From
Lynna Landstreet
Date:
Hi there,

I'm having a strange problem with sessions on my site. I've been using them
(via the $_SESSION) variable with no major problems for some while, but one
particular page seems to be refusing to save the session, although I can't
find any major code differences in the way the session is being handled from
any of the other pages that do work.

The page in question is part of the process for deleting images from the
database, and is meant to show the use what images they're about to delete
and have them verify that they really want to do this.

What the page does is:

- pulls an array of integers from $_POST into a variable called $image_id,
representing the image_id numbers of the image or images the user chose to
delete in a form on the previous page.

- uses that array to query the database for the image titles and paths, and
then displays the images and their titles, and asks if the user is sure they
want to delete these images

- displays a "Yes, I'm sure!" button which the user can click to verify,
which goes to another page which does the deletion and then tells the user
that it's done. The image_id array is written into $_SESSION and passed to
the next page that way rather than via $_POST with the form, because it's an
array and that seemed like the easiest way to pass it.

But what happens is that, while saving the image_id info into $_SESSION is
successful (I can print out $_SESSION at the end of the problem page and it
contains the correct info), when I submit the form and go to the next page,
the session's gone. Trying to print $_SESSION there produces a blank array.

Things I've tried in the course of troubleshooting:

- Replacing the entire following page with one whose sole function is to
print $_SESSION and does absolutely nothing else. Result: it's still blank.

- Using serialize() on $image_id and passing it via a hidden form field,
then unserializing it on the following page. Result: still a blank array,
even though it had the data before serializing it.

- Going directly to the next page by typing the URL into the browser's
location bar, rather than by submitting the form, in case the process of
submitting the form was causing problems. Result: still loses the session.

- Checking to make sure the PHPSESSID cookie was set. Result: it was, and
still was after the session was lost.

- Checked, double-checked, and triple-checked to make sure all pages had
session_start() at the beginning.

I'm completely out of ideas. Does anyone else have any idea what else might
be causing this problem?


Lynna

--
Resource Centre Database Coordinator
Gallery 44: www.gallery44.org
Database Project: www.gallery44db.org



Re: Strange session problem

From
Lynna Landstreet
Date:
on 8/11/04 10:46 AM, Andres Ferrando at anferr@mecon.gov.ar wrote:

> Some ideas:
>
> be sure that the index yo you use in $_SESSION (if any), is integer or
> string, and you aren't using array or similar as index.

It just uses numeric keys - i.e. [0], [1], [2], etc. I assigned the image_id
array from the previous page to $_SESSION directly, as in $_SESSION =
$image_id. And then printed $_SESSION with print_r and everything seemed OK,
but when I go to the next page, $_SESSION is empty.


> how do you go to second page? if you use a header(rediret), you need to
> pass SID explicit.

It goes to the second page by submitting a form.


> if nothing works, try posting code (important parts, as less)

OK. Here it is. I've deleted irrelevant parts, like most of the HTML, and
just replaced them with notes in square brackets:

-------------------------------------------------------------------------

<? session_start(); ?>

[HTML header stuff]

<? require_once "/usr/local/www/gallery44db.org/www/resources/db.php"; ?>

[That include file is just the database connection, which is called on every
page]

[More HTML stuff - end of head section, beginning of body, navigation, etc.]

<?

    require_once("includes/login_check.inc.php"); // used on every page

    // some more HTML stuff here

    $image_id = $_POST['image_id']; // from form on previous page

    $image_query = "SELECT image_id, image_path, title
        FROM images WHERE ";

    foreach ($image_id as $id) {
        $image_query .=  "image_id = " . $id . " OR ";
        }

    $image_query = substr($image_query, 0, -4); // removes the final " or "

    $image_info = pg_fetch_all(pg_query($image_query));

    // here it displays a table with all the images and their titles

    $_SESSION = $image_id;

    echo "<pre>\n\nSession Contents:\n\n";

    print_r ($_SESSION);

    // that works, so as of this part, the correct data is in $_SESSION

    echo "</pre>";

?>

[a little more HTML]

<form method="post" action="/admin/test.php">

    <input type="submit" name="Submit" value="Yes, I'm sure!">

</form>

[yet more HTML, ending the page]

-------------------------------------------------------------------------

Does that help?


Lynna

--
Resource Centre Database Coordinator
Gallery 44: www.gallery44.org
Database Project: www.gallery44db.org



Re: Strange session problem

From
Justin Wyer
Date:
i am really not sure what is wrong, but I had funny issues with sessions
at times, try calling session_write_close(); before the end of your page
and after you have finished working with the $_SESSION variable, that
may be of help I have had problems with session variables getting chewed
before that always seemed to help.

hope it helps and good luck.

Re: Strange session problem

From
"Majolee InfoTech"
Date:
Hello,

The problem is you are not giving a name to that session.

Use $_SESSION[image_ids]

And it will be ok.

===============
Mihir & Biren
Partners
Majolee InfoTech
===============
----- Original Message -----
From: "Lynna Landstreet" <lynna@gallery44.org>
To: <pgsql-php@postgresql.org>
Cc: "Andres Ferrando" <anferr@mecon.gov.ar>
Sent: Thursday, August 12, 2004 2:07 AM
Subject: Re: [PHP] Strange session problem


> on 8/11/04 10:46 AM, Andres Ferrando at anferr@mecon.gov.ar wrote:
>
> > Some ideas:
> >
> > be sure that the index yo you use in $_SESSION (if any), is integer or
> > string, and you aren't using array or similar as index.
>
> It just uses numeric keys - i.e. [0], [1], [2], etc. I assigned the
image_id
> array from the previous page to $_SESSION directly, as in $_SESSION =
> $image_id. And then printed $_SESSION with print_r and everything seemed
OK,
> but when I go to the next page, $_SESSION is empty.
>
>
> > how do you go to second page? if you use a header(rediret), you need to
> > pass SID explicit.
>
> It goes to the second page by submitting a form.
>
>
> > if nothing works, try posting code (important parts, as less)
>
> OK. Here it is. I've deleted irrelevant parts, like most of the HTML, and
> just replaced them with notes in square brackets:
>
> -------------------------------------------------------------------------
>
> <? session_start(); ?>
>
> [HTML header stuff]
>
> <? require_once "/usr/local/www/gallery44db.org/www/resources/db.php"; ?>
>
> [That include file is just the database connection, which is called on
every
> page]
>
> [More HTML stuff - end of head section, beginning of body, navigation,
etc.]
>
> <?
>
>     require_once("includes/login_check.inc.php"); // used on every page
>
>     // some more HTML stuff here
>
>     $image_id = $_POST['image_id']; // from form on previous page
>
>     $image_query = "SELECT image_id, image_path, title
>         FROM images WHERE ";
>
>     foreach ($image_id as $id) {
>         $image_query .=  "image_id = " . $id . " OR ";
>         }
>
>     $image_query = substr($image_query, 0, -4); // removes the final " or
"
>
>     $image_info = pg_fetch_all(pg_query($image_query));
>
>     // here it displays a table with all the images and their titles
>
>     $_SESSION = $image_id;
>
>     echo "<pre>\n\nSession Contents:\n\n";
>
>     print_r ($_SESSION);
>
>     // that works, so as of this part, the correct data is in $_SESSION
>
>     echo "</pre>";
>
> ?>
>
> [a little more HTML]
>
> <form method="post" action="/admin/test.php">
>
>     <input type="submit" name="Submit" value="Yes, I'm sure!">
>
> </form>
>
> [yet more HTML, ending the page]
>
> -------------------------------------------------------------------------
>
> Does that help?
>
>
> Lynna
>
> --
> Resource Centre Database Coordinator
> Gallery 44: www.gallery44.org
> Database Project: www.gallery44db.org
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>
>