我有一个多页面表单,我正尝试在我的网站上实现它的工作方式如下:
1 Enter details of customer -- $_POST --> 2 Confirm Customer Details --$_SESSION--> 3 Generate PDF from Customer Details
正如您在我的第二页上看到的那样,我从 $_POST 获取表单中的值。在同一个页面上,我从名为 customer 的类中创建了一个名为“customer”的对象,它存储了从我的第一页发布的所有值。
然后我设置一个名为“customer”的 $_SESSION 变量并将其分配给我的对象。
session_start();
//includes html header part of page
include("includes/page/header.php");
//customer class
include("classes.php");
$customer = new customer();
$_SESSION['customer'] = $customer;
现在在我网站的最后一页上有以下代码:
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;
测试这个我的页面上没有显示任何内容,所以我的 session 没有按照我的预期进行。
仅供引用:
- 我类的客户很好,工作正常,所有值都正确存储!
- 我的帖子数组工作正常
session 的 var_dump 在最后一页。
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" }
有什么想法可以让它发挥作用吗?
谢谢
圣诞快乐!
最佳答案
您需要在最终页面上执行 include("classes.php");
,在 session_start()
之前,因为在反序列化 session 变量时,PHP 会尝试实例化您放入 session 数据中的同一类的对象。因此,如果 PHP 找不到该类,则会引发错误。
关于php - 在 session 中存储 PHP 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8605414/