2

I have a multi page form that I am trying to implement on my website which works like so:

1 Enter details of customer -- $_POST --> 2 Confirm Customer Details --$_SESSION--> 3 Generate PDF from Customer Details

As you can see on my second page I get the values in my form from $_POST. On that same page I create an object called "customer" from my class called customer which stores all the values POSTed from my first page.

I then set a $_SESSION variable called "customer" and assign it to my object.

session_start();  

        //includes html header part of page
        include("includes/page/header.php");



        //customer class 
        include("classes.php");


        $customer = new customer();


        $_SESSION['customer'] = $customer;

Now on my final page of my site I have the following code:

    session_start();

//assign customer to equal object stored in session
$customer = $_SESSION['customer'];


//to test if successfull , nothing is displayed!
echo $customer->first_name;


return 0;

Testing this nothing is displayed on my page, so my session is not doing what I thought it would do.

FYI:

  • My class customer is fine and works properly and stores all values correctly!
  • My post array is working fine

var_dump of session on final page.

object(__PHP_Incomplete_Class)#1 (15) { ["__PHP_Incomplete_Class_Name"]=> string(8) "customer" ["first_name"]=> string(5) "Iain" ["second_name"]=> string(6) "Taylor" ["company_name"]=> string(10) "Wired Ltd." ["address_1"]=> string(15) "14 Joseph St" ["address_2"]=> string(8) "West End" ["city"]=> string(7) "Glasgow" ["postcode"]=> string(7) "G45 NP4" ["county"]=> string(29) "Scotland" ["country"]=> string(14) "United Kingdom" ["email"]=> string(31) "somebody@somebody.com" ["phone"]=> NULL ["cid"]=> NULL ["reg_date"]=> NULL ["tel"]=> string(11) "01413395678" } 

Any ideas how to get this to work ?

Thanks

Merry Christmas!

tomaytotomato
  • 3,788
  • 16
  • 64
  • 119
  • Have you tried using `var_dump($_SESSION['customer'])` to check the content ? – Florian Margaine Dec 22 '11 at 14:41
  • Does `$customer->first_name` have a value before you assign it to your session? – paulcol. Dec 22 '11 at 14:42
  • have you verified after setting the session you can read the session right back out? – Robert Dec 22 '11 at 14:43
  • I noticed you included `classes.php` on your first page, but didn't have the `include` on your final page. Intentional? – Josh Dec 22 '11 at 14:45
  • you need to be sure that you can serialize that object to store it in the session (because session data is serialized upon storage).. see the __sleep and __wakeup php magic methods.. it's not that you need to serialize it when you save it.. it needs to be able to be serialized when the session is stored – mishu Dec 22 '11 at 14:46
  • Guys I have posted var_dump on page, @FlorianMargaine – tomaytotomato Dec 22 '11 at 14:51
  • Solution: I found a solution which is to modify the php.ini file It is something to do with unserialise There is a function in php.ini that deals with this and should be turned off to fix it. ini_set('unserialize_callback_func', 0); http://php.net/manual/en/function.unserialize.php – tomaytotomato Dec 22 '11 at 16:20

2 Answers2

5

You need to do include("classes.php"); on your final page, before session_start(), because, when deserializing session variables, PHP tries to instantiate object of the same class that you`ve put in session data. So, if PHP cannot find the class, it raises an error.

Timur
  • 6,668
  • 1
  • 28
  • 37
  • no error was reported on the page - check my updated question for the var_dump – tomaytotomato Dec 22 '11 at 14:56
  • @loosebruce And do you include `class.php` on the final page, before `session_start()`? – Timur Dec 22 '11 at 14:57
  • No I do not, so it must be something else? -Thanks – tomaytotomato Dec 22 '11 at 14:59
  • If you do not include `class.php`, than do it and check if it helps you. – Timur Dec 22 '11 at 15:00
  • I just get the same var dump for my variable $customer and $_SESSION['customer'] . What does incomplete class mean? – tomaytotomato Dec 22 '11 at 15:04
  • `__PHP_Incomplete_Class` is created when `unserialize()` cannot instantiate object of same same class as serialized one. Sessions in PHP are use serialization. So, you need to doublecheck that you are initializing your `customer` class _before_ `session_start()`. For this reason I advised you to include your `classes.php` file on the final page. If you are not able to declare class before `session_start()` fro some reason, may be this will help you: http://stackoverflow.com/questions/965611/forcing-access-to-php-incomplete-class-object-properties – Timur Dec 23 '11 at 07:01
1

@Timur is absolutely correct, except he missed one thing. You need to move the class include above session_start() on every page, not just the last.

"The automatic serialization/unserialization occurs when you call session_start(). That means the order in which you include your files and call the session_start() is very important." - PHP Session with an Incomplete Object

Do some research next time before you ask a question. That was the third result on Google of a search for "PHP incomplete class".

As a general rule you should include all files at the very top of every script. That way you never run into any dependency issues like this.

Community
  • 1
  • 1
David Myers
  • 799
  • 1
  • 9
  • 18
  • Thanks but I found out what the problem was and the only way to fix it was to modify my php.ini file - http://php.net/manual/en/function.serialize.php – tomaytotomato Dec 22 '11 at 16:06
  • if I include all my files at the top some of my variables get rewritten over. – tomaytotomato Dec 22 '11 at 16:21
  • Not sure what you mean by "get rewritten over", but it's standard practice to include all files at the top of your page. Including a class just enables you to use it in your file and it doesn't overwrite anything. If you're not utilizing the OOP nature of PHP and you're just including scripts/functions you've written then who knows what including all of your files at the top of the page will do. That's poor practice however and you should really learn to write your code in an object oriented manner. – David Myers Dec 22 '11 at 16:34
  • I mean I several librariries that use similar variable names like $name etc, and if I include them all the top it messes my pdf maker up. So I only inlcude them when needed at different points in the code. – tomaytotomato Dec 22 '11 at 16:48
  • As I said, unless they're not defined in a class that doesn't matter. It all depends on their scope and if you're doing things correctly, then it should be fine. Trust me this will make your life easier and you won't have to juggle which things you can and can't include based upon the fact that your code wasn't in a scalable manner. – David Myers Dec 22 '11 at 17:01