Question : unserialize


dont understand
unserialize

on
http://php.net/manual/en/function.unserialize.php

Answer : unserialize

I've done a bit of digging and I have attached a fragment from a class that saves a shopping basket in the session. Since shoving arrays in the session can be messy it converts the basket data (two arrays) to two strings and saves them in two session variables.

On the load of the next page, the class's initialisation checks to see if the session contains variables indicating that a basket is in use. If it finds them it uses UNSERIALIZE to unpack the strings back into arrays and then assigns them to class properties.
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:
// Store the current array in a session variable
	//
	private function updateBasket() {
		$_SESSION[$this->sessName] = serialize($this->basket);
		$_SESSION[$this->sessQty]  = serialize($this->qty);
	}



	// Initialise the basket
	//
	private function initialise() {

		// Check if session is in progress and if so retrieve the existing basket
		//
		if ( isset($_SESSION [$this->sessName]) && isset($_SESSION [$this->sessQty]) ) {
			$this->basket = unserialize( $_SESSION[$this->sessName] );
			$this->qty    = unserialize( $_SESSION[$this->sessQty] );
		}
		else {
			$this->basket = array();
			$this->qty    = array();
			$this->updateBasket();
		}

	}
Random Solutions  
 
programming4us programming4us