Forum Moderators: coopster

Message Too Old, No Replies

adding on to session data

         

sneaks

10:50 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



i am trying to incorporate session variables into my site... and am stuck with a session being overwritten as compared to appending on to the array data.


$info = $_GET[info];
$ic=count($_SESSION);
if (isset($_GET[info]))
{
$ic++;
$_SESSION[$ic] = $info;
}

i am confused as to why it doesnt add to the array, when i go to print_r shows only [1] item in the array over and over?

j.

coopster

2:30 pm on Sep 23, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are starting your session first, correct?

sneaks

2:37 pm on Sep 23, 2005 (gmt 0)

10+ Year Member



absolutely, ,maybe this example will show you better why i am confused:

-- file: thisone.php --


<?php
session_start();
$word=$_POST['word'];
array_push($_SESSION, $word);
print_r ($_SESSION);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<br />
<br />
<br />
<FORM METHOD="POST" ACTION="thisone.php">
Word: <input type="text" name="word">
<input type="SUBMIT" value="Submit">
</FORM>
</body>
</html>

--------------------
it doesnt append to the existing session it replaces it each time... therefore losing the old session data. yesterday was my first time playing with sessions and i havent been able to figure this out... i have tried a lot methods, same result... replace instead of append. just trying to stockpile a few variables and not sure where I am going wrong.

thx.j.

thanks
j.

mcibor

10:22 am on Sep 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If that's the case I would recommend using this strucure, not pushing:

<?php
session_start();
if(!isset($_SESSION['word'])) $_SESSION['word'] = array();
if(isset($_POST['word']) &&!empty(trim($_POST['word']))
{
$_SESSION['word'][] = $_POST['word'];
}

print_r ($_SESSION);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<br />
<br />
<br />
<FORM METHOD="POST" ACTION="thisone.php">
Word: <input type="text" name="word">
<input type="SUBMIT" value="Submit">
</FORM>
</body>
</html>


Best regards
Michal Cibor