I have this OO PHP project that serializes an object and forwards it to the next page via the query string. The basic operation is fine, but instead of adding a new array element, it replaces the existing array element. Here's the code (with the non-relevant stuff cut out):
The class:
class User extends SystemUtils{
var $default_page="index.php";
var $order;
function User(){
/*
Initializes order and sets up class vars.
*/
$orderdata=false;
extract($_GET,EXTR_IF_EXISTS);
$orderdata=unserialize($orderdata);
if($orderdata){
foreach($orderdata as $k=>$v){
$this->order[$k]=$v;
}
}else{
$this->order["order_date"]=date("Y-m-d");
}
}
function addPhotoToOrder($image_id){
/*
Adds quantity of 1 of specified image to order and returns user to gallery with serialized order info.
Serialized order info is picked up by the class constructor when the page loads.
*/
$this->order[$image_id]=1;
//ABOVE DOESN'T ADD - JUST REPLACES, EVEN THOUGH $image_id IS DIFFERENT FROM THE EXISTING INDEX
header("location:".$this->default_page."?orderdata=".serialize($this->order));
}
}
index.php:
require_once("init.php");
$option="";
$password_req="";
$email_req="";
$image_id=0;
$user_id=false;
extract($_GET,EXTR_IF_EXISTS);
extract($_POST,EXTR_IF_EXISTS);
extract($_SESSION,EXTR_IF_EXISTS);
include($user->docroot."/html/head.htm");
if($user_id){
//Logged in, proceed
switch($option){
case 'add_photo':
$user->addPhotoToOrder($image_id);
break;
default:
foreach($user->order as $k=>$v){
echo "$k=>$v<br/>";
}
$gallery_name=$user->getGalleryName($user_id);
$content=$user->getContent($user->docroot."/html/user/view_gallery.htm");
$content=$user->viewGallery($gallery_name,$content);
print $content;
}
}else{
//Not logged in, perform login function calls
if($option=='login'){
if($_POST){
$credentials=serialize(array("password"=>$password_req));
if($login_id=$user->authenticate($credentials,"user")){
if($user->emailIsValid($email_req)){
$email_id=$user->collectEmail($link,$email_req,$login_id);
$user->login($email_id);
}else{
include($user->docroot."/html/messages/invalid_email.htm");
include($user->docroot."/html/user/login.htm");
}
}else{
include($user->docroot."/html/messages/login_failed.htm");
include($user->docroot."/html/user/login.htm");
}
}else{
include($user->docroot."/html/user/login.htm");
}
}else{
include($user->docroot."/html/user/home.htm");
}
}
include($user->docroot."/html/foot.htm");
What's supposed to happen is when $image_id is passed, it's supposed to add on to the $this->order array, but all it does is replace the existing, single index in that array. $image_id is different from the existing index, so I don't know how it's getting replaced instead of a new array element being created.