Save data in Arrays during sessions

This is a discussion on "Save data in Arrays during sessions" within the PHP Forum section. This forum, and the thread "Save data in Arrays during sessions are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Oct 23rd, 2007, 18:18
New Member
Join Date: Oct 2007
Location: United Kingdom
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Save data in Arrays during sessions

Hi I'm a new member and I need some help here

I've got a book selling site and I'm trying to create a way of customers clicking on buttons generated from a query and that list of book IDs or titles appearing in a text area box in a form ready for posting as email.

The question is about sessions and arrays

Now I need to preserve data across multiple clicks, as the book IDs are added to an array - registered in a session. My problem - and I think a lot of people have had this - is that the array data is not preserved between pages.

The code i've got so far is :

PHP: Select all

 session_start();
    
    
$AddStuff $_POST['BookText']; 
    
    
     if (!
session_is_registered('stuff'))
    
         {        

        
session_register('stuff');
        
        
$stuff = array();
        
             
         }
         
     if (
session_is_registered('stuff')){

       
        
$stuff[] = $AddStuff;
       
       } 

OK i've used session_register which many say is no good but there is no security issue here - no logins or credit card numbers - just passing info to an email form. And it works - BUT

when I go back and click on another book button the array doesn't get any bigger - there is always just ONE element in it - because the info in the array isn't being preserved 'between' pages.

I understand this is a bug in versions of php after 4.0 or something and I've seen some people recommend $_SESSION - which I've sort of tried but that doesn't even seem to enter one element.

Can anyone help me out here ?

Andy

Last edited by c010depunkk; Oct 23rd, 2007 at 18:21. Reason: added [PHP] tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 23rd, 2007, 18:25
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Save data in Arrays during sessions

$_SESSION works on anything later then 4.1.0. And earlier versions have $HTTP_SESSION_VARS. I've never heard of this problem before and I've coded with everything since PHP 3.*....

Last edited by c010depunkk; Oct 23rd, 2007 at 18:26. Reason: i sounded unfriendly.. :D
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 24th, 2007, 13:46
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Save data in Arrays during sessions

I have had similar problems.

If you have php >= 4.1 you could use

PHP: Select all





 session_start
();

    

    
$AddStuff $_POST['BookText']; 

    

    

     if (!
$_SESSION['stuff']))    

         {               

               
$stuff = array();            

         } else {

       

        
$stuff unserialize($_SESSION['stuff']);       

       } 



      
$stuff[] = $AddStuff;

      

     
$_SESSION['stuff'] = serilize($stuff); 
Serilizing and passing as a string seems to work (I can't seem to figure out the reason why this happens as on some sites the arrays pass around without issue).

When you want to get the array in $_SESSION['stuff']

you can use $someStuff = unserialize($_SESSION['stuff']);

Hope that helps,

Cheers,
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problems with arrays Scream JavaScript Forum 2 Jan 10th, 2008 16:15
arrays images and backgroundImage gleb JavaScript Forum 0 Sep 9th, 2007 20:39
Looping through arrays assgar Starting Out 1 Apr 22nd, 2007 18:43
sending email arrays with php ppgpilot PHP Forum 2 Jan 25th, 2007 16:49
arrays? macupryk ASP.NET Forum 1 Sep 30th, 2003 12:44


All times are GMT. The time now is 11:05.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42