Closed Thread Icon

Topic awaiting preservation: Problems with session Pages that link to <a href="https://ozoneasylum.com/backlink?for=12840" title="Pages that link to Topic awaiting preservation: Problems with session" rel="nofollow" >Topic awaiting preservation: Problems with session\

 
Author Thread
Rhino
Bipolar (III) Inmate

From: New Jersey, USA
Insane since: Jul 2003

posted posted 07-31-2003 11:38

I am working on a little site and am trying to use PHP's session to pass back errors as well as the data entered, but am running into problems. It seems that I can set a session value using

$_SESSION["ID"] = $user_name;

and this is valid for the entire session, but when I try to store values after the initial login I run into problems. Here is an example of the code that I have written to store the values into session

if ($row = mysql_fetch_array($result))
{
$portfolio["portfolio_id"] = $row["portfolio_id"];
$portfolio["category_id"] = $row["category_id"];
$portfolio["small_picture"] = $row["small_picture"];
$portfolio["large_picture"] = $row["large_picture"];
$portfolio["client"] = stripslashes($row["client"]);
$portfolio["art_direction"] = stripslashes($row["art_direction"]);
$portfolio["design"] = stripslashes($row["design"]);
$portfolio["description"] = stripslashes($row["description"]);

$_SESSION["portfolio"] = $portfolio;
}

This is how I retrieve these values from the session on the next page

$portfolio = array();

if (isset($_SESSION["portfolio"]))
$portfolio = $_SESSION["portfolio"];


Any help is greatly appreciated.

One last thing...I am developing the code on a windows platform where everything seems to work properly, but when deployed it doesn't seem to be able to keep session. The only thing kept in session when deployed is the ID when first logging in.



[This message has been edited by Rhino (edited 07-31-2003).]

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 07-31-2003 12:10

Rhino: Welcome. You have bits of your code there so I'm not sure where the problem is sneaking in but have you started the session? It may be that your Windows server is set to automaticall start sessions while your web server isn't. Anyway there are problem a few places were problems can sneak in - have a look through TP's guide:

:FAQ: Basic PHP Session Tutorial

___________________
Emps

FAQs: Emperor

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 07-31-2003 12:11

The first you should check is if you have this line
<?php session_start(); ?>

at the very top (not even a blank row above it) of every page that you want to use/change/set values in/from your session.

If that doesn't help, welcome bach with more code
/Dan


{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

Rhino
Bipolar (III) Inmate

From: New Jersey, USA
Insane since: Jul 2003

posted posted 07-31-2003 12:51

Sorry I didn't include more code.. Here is an example where I am allowing the person to edit an entry. I am loading up an array called $portfolio and setting it to Session (atleast I think).

<?php session_start();

include "dbInc.php";

if (!isset($_SESSION["ID"]))
{
header("Location: login.php");
break;
}

$connection = getConnection();

$portfolio = array();
$errors = array();

$action = "";
if (isset($_GET["action"]))
$action = $_GET["action"];


if ($action == "edit")
{
$id = $_GET["id"];

$query = " SELECT p.*, c.category "
. " FROM portfolio p, category c "
. " WHERE p.category_id = c.category_id "
. " AND p.portfolio_id = $id ";

if ($result = mysql_query($query, $connection))
{
if ($row = mysql_fetch_array($result))
{
$portfolio["portfolio_id"] = $row["portfolio_id"];
$portfolio["category_id"] = $row["category_id"];
$portfolio["small_picture"] = $row["small_picture"];
$portfolio["large_picture"] = $row["large_picture"];
$portfolio["client"] = stripslashes($row["client"]);
$portfolio["art_direction"] = stripslashes($row["art_direction"]);
$portfolio["design"] = stripslashes($row["design"]);
$portfolio["description"] = stripslashes($row["description"]);

$_SESSION["portfolio"] = $portfolio;
}
else
{
$errors["edit"] = "An Error occurred while trying to retrieve an item for editing";
}
}
else
{
$errors["edit"] = "An Error occurred while trying to retrieve an item for editing";
}
}

header ("Location: portfolio.php");

?>

This is the page that is called after the Array is filled. Attempting to retrieve the $portfolio object from session.

<?php session_start();

include "dbInc.php";

if (!isset($_SESSION["ID"]))
{
header("Location: login.php");
break;
}

$connection = getConnection();

$portfolio = array();
$errors = array();

if (isset($_SESSION["portfolio"]))
$portfolio = $_SESSION["portfolio"];

if (isset($_SESSION["errors"]))
$errors = $_SESSION["errors"];

unset($_SESSION["errors"]);
unset($_SESSION["portfolio"]);

$selected_category = 0;

if (isset($_GET["category"]))
$selected_category = $_GET["category"];

?>

Finally here is an example of setting the values in the page
<tr>
<td colspan="2"><input type="hidden" name="id" value="<?php if(isset($portfolio["portfolio_id"])){ echo $portfolio["portfolio_id"]; }?>"><img src="./images/spacer.gif" border="0" height="3" width="1"></td>
</tr>

Hope this is enough to give you guys an idea of what is going wrong.


Thanks again for all your help!

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 07-31-2003 17:40

One of the easiest ways to see what content you have and how it is structured in the session is to drop this in the head of the page:

code:
print("Session:<pre>");
print_r($_SESSION);
print("</pre>");



Try that and you'll see what you have in the session and how it is structured.
/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 07-31-2003 18:11

Rhino: Yep - follow DmS' advice - that is one of the most useful tricks I've found yet. It makes trouble shooting tricky things like arrays much simpler (thanks again for sharing ).

___________________
Emps

FAQs: Emperor

trib
Paranoid (IV) Inmate

From: Den Haag, Netherlands
Insane since: Sep 2002

posted posted 07-31-2003 18:39

from http://www.php.net ...

quote:
session_register() accepts a variable number of arguments, any of which can be either a string holding the name of a variable or an array consisting of variable names or other arrays. For each name, session_register() registers the global variable with that name in the current session.



From the above, together with a sneaking suspicion that I've heard already that complex objects cannot be stored in the session file type, I might be forgiven for assuming that $_SESSION only works with scalar values (well name,value tuples to be more precise) and not objects (like arrays for example) ... so ...

$_SESSION["portfolio"] = $portfolio; ... and later ...
if (isset($_SESSION["portfolio"])) ...

may not provide you with the result you were expecting ...

First - Try testing for $_SESSION["portfolio_id"] instead of $_SESSION["portfolio"] if you're lucky, the array might have been "unfolded" and inserted into the $_SESSION array as single elements ... and if that doesn't work, assign portfolio to $_SESSION by looping through it and assigning each value pair separately ...

foreach ($portfolio as $key => $value) { $_SESSION[$key]=$value }

and then, again, test for $_SESSION["portfolio_id"] in the if statement ...

CAVEAT EMPTOR ... I couldn't find anything on a quick search to confirm my suspicions, so I am working from memory only. There are more experienced PHP coders than me on here, and some of them are young enough or sufficiently un-mendicated to still have full control of their memories ... What I suggest can be tested very quickly, but you might want to wait until someone else either confirms or shoots down the above ... Personally I always use scalar assignments to #_SESSION anyway .. it makes the session code more readable, the contents more easily accessible, and debugging really so much simpler



Bug-free software only exisits in two places
A programmer's mind and a salesman's lips

[This message has been edited by trib (edited 07-31-2003).]

Rhino
Bipolar (III) Inmate

From: New Jersey, USA
Insane since: Jul 2003

posted posted 07-31-2003 19:29

I ended up taking trib's suggestion and giving it a try since I was able to store the ID in session. So, I removed everything from the array and am storing only Simple scalar values into Session. AND IT WORKED! Yahoo.


Thanks all for the help.



DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 07-31-2003 22:59

Well, I'd have to disagree on that one (no arrays in the session)

We are right now building a large and complex site w PHP/MySQL that uses sessions heavily for caching of information.
Basically we have a four layered application with:
- Database layer with db and all code to access it and retrieve data as arrays.
- A stateful-business layer that checks if the required data is in the session, if not it will load it from the db and store it in the session, then it serves the info from the session (a bit more complicated than that but close enough).
- Then we have the client-layer that takes the data and transforms it into HTML-snippets.
- Last the presentation layer which is basically straight HTML with method calls to the client-layer to get the required elements to complete the page.

We store all our cached info as arrays in the session with no problems at all.

Lets say we get an array back from the db "$returnedDataFromDb" that is an associative array:
[data_1] => "item one"
[data_2] => "item two"
[data_3] => "item three"
, we just store it in the session: "$_SESSION['requiredDbData'] = $returnedDataFromDb"
Then access it with: "$_SESSION['requiredDbData']['item_1']"

Works like a charm.
/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

Rhino
Bipolar (III) Inmate

From: New Jersey, USA
Insane since: Jul 2003

posted posted 07-31-2003 23:09

What type of platform are you running this on? I had complete success storing arrays in Session when developing on a Windows platform, but when I finally deployed it onto a Linux server it no longer maintained the arrays and alot of functionality disappeared. So, I am back to the Name/Values in Session. Can you include more code snippets so we can see how you are doing it and please let us know what the platform you are running this on.

Regards

Mike

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 08-01-2003 10:24

We are developing on win2k or XP with phpdev as a basic developing platform (slightly modded to fit the clients platform) running Apache 1.3.27 PHP 4.2.1 and MySQL 3.23.34

We deploy to Linux with PHP 4.2.1 and MySQL 3.23.34 with no problems at all.
We need to change some paths and write permissions, but that's about it.

It's wholly OOP so it's a bit hard to show code but here's one snip that checks if the key exists in the session, gets stuff from the db if it's not in the session, loads the session from the db then serves data from the session.

code:
function getRegions()
{
$retVal = array(); // Return value
$loopArr;
$tempArr;

if(!array_key_exists('regions',$_SESSION))
{
$tempArr = array();
// Try to recover. 'regions' is no array in session, i.e. no session data?
$loopArr = $this->dataLoader->getRegions();
foreach($loopArr as $key1 => $value1)
{
if(is_array($value1))
{
$tempArr[$loopArr[$key1]['region_id']] = $loopArr[$key1]['region_name'];
}
}
reset($tempArr);
$_SESSION['regions'] = $this->filterRegions($tempArr,'');
}
reset($_SESSION['regions']);

// Get data from session
$retVal = $_SESSION['regions'];
return $retVal;
}


Remember that this is just a very small part of a big package of code.
/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

trib
Paranoid (IV) Inmate

From: Den Haag, Netherlands
Insane since: Sep 2002

posted posted 08-10-2003 10:55

Thanks DMS - interesting information. I've stored it in my "Things Trib should know" braincells ...


Bug-free software only exisits in two places
A programmer's mind and a salesman's lips

« BackwardsOnwards »

Show Forum Drop Down Menu