看的韩顺平的《PHP:从入门到精通》的视频,从第188集开始讲购物车,前面所有视频里面的代码我都照着敲了一遍,都能实现,到了190集的时候,发现session机制出了问题。它这个购物车是用zend framework写的,我测试以前非zend framework框架下写的跟session相关的代码,发现session机制都起作用。现在就不顶用了。
我仔细检查了一遍,我的代码跟韩顺平视频里的代码是一模一样的,找不到问题在哪里。大家帮我看一下吧。
application下面的configs下面的application.ini文件:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
[mysql]
db.adapter=PDO_MYSQL
db.params.host=localhost
db.params.username=root
db.params.password=123456
db.params.dbname=shopping
controllers下面的BaseController.php文件
<?php
class BaseController extends Zend_Controller_Action{
public function init(){
//初始化我们的数据库适配器
$url = constant("APPLICATION_PATH").DIRECTORY_SEPARATOR.'configs'.DIRECTORY_SEPARATOR.'application.ini';
$dbconfig=new Zend_Config_Ini($url,"mysql");
$db = Zend_Db::factory($dbconfig->db);
$db->query("SET NAMES UTF8");
Zend_Db_Table::setDefaultAdapter($db);
}
}
controllers下面的GlobalController.php文件
<?php
require_once "BaseController.php";
class GlobalController extends BaseController
{
public function okAction(){
file_put_contents("d:/mylog.txt", __FILE__."\r\n",FILE_APPEND);
}
public function errAction(){
}
}
?>
controllers下面的HallController.php文件
<?php
require_once "BaseController.php";
require_once APPLICATION_PATH.'/models/Book.php';
class HallController extends BaseController
{
//到购物大厅去
public function gohalluiAction()
{
$bookModel=new Book();
session_start();
$this->view->loginuser=$_SESSION['loginuser'];
$this->view->books=$bookModel->fetchAll()->toArray();
$this->render('hall');
}
}
controllers下面的IndexController.php文件
<?php
require_once "BaseController.php";
class IndexController extends BaseController
{
public function indexAction()
{
// action body
}
}
?>
controllers下面的LoginController.php文件
<?php
require_once "BaseController.php";
require_once APPLICATION_PATH.'/models/Users.php';
class LoginController extends BaseController
{
public function loginAction()
{
//调用表模型
$userModel=new Users();
//获取用户的id和密码
$id=$this->getRequest()->getParam("id","");
$pwd=$this->getRequest()->getParam("pwd","");
$db=$userModel->getAdapter();
$where=$db->quoteInto("id=?", $id).$db->quoteInto(" AND pwd=?", md5($pwd));
$loginuser=$userModel->fetchAll($where)->toArray();
if(count($loginuser)==1){
session_start();
$_SESSION['loginuser']=$loginuser[0];
$this->_forward('gohallui','hall');
}else{
//失败
$this->view->err="<font color='red'>您的用户名或密码错误</font>";
$this->_forward('index','index');
}
}
public function logoutAction()
{
// action body
}
}
controllers下面的ShoppingController.php文件
<?php
require_once "BaseController.php";
require_once APPLICATION_PATH.'/models/MyCart.php';
class ShoppingController extends BaseController
{
public function addproAction()
{
$bookid=$this->getRequest()->getParam('bookid');
$mycart=new MyCart();
session_start();
$loginuser=$_SESSION['loginuser'];
if($mycart->addProduct($loginuser['id'],$bookid)){
$this->view->info="添加商品成功";
$this->_forward("ok","global");
}else{
$this->view->info="添加商品失败";
$this->_forward('err','global');
}
}
}
?>
models下面的Book.php文件
<?php
class Book extends Zend_Db_Table{
protected $_name="book";
}
?>
models下面的MyCart.php文件
<?php
class MyCart extends Zend_Db_Table{
protected $_name='mycart';
public function addProduct($userId, $productId, $nums=1){
$now=time();
$data=array(
'userid'=>$userId,
'bookid'=>$productId,
'nums'=>$nums,
'cartDate'=>$now
);
if($this->insert($data)>0){
return True;
}else{
return False;
}
}
}
?>
models下面的MyCart.php文件
<?php
class Users extends Zend_Db_Table{
protected $_name="users";
}
?>
views/scripts/global下面的err.phtml文件
<script type="text/javascript">
alert('<?=$this->info?>');
history.back();
</script>
views/scripts/global下面的ok.phtml文件
<script type="text/javascript">
alert('<?=$this->info?>');
window.location.href="/hall/gohallui";
</script>
views/scripts/hall下面的hall.phtml文件
<html>
<head>
<title>购物大厅</title>
<script language="javascript">
function goMyCart(){
window.location.href="??";
</script>
</head>
<body>
<h1>欢迎<?=$this->loginuser['name']?>光临购物大厅</h1>
<table width="700px">
<tr>
<td>书名</td>
<td>价格</td>
<td>出版社</td>
<td>购买</td>
</tr>
<?php foreach ($this->books as $book) {?>
<tr>
<td><?=$book['name']?></td>
<td><?=$book['price']?></td>
<td><?=$book['publishHouse']?></td>
<td><a href='/shopping/addpro?bookid=<?=$book['id']?>'>购买</a></td>
</tr>
<?php }?>
<tr>
<td colspan=4><input type="button" onclick="goMyCart()" value="查看购物车"/></td>
</tr>
</table>
</body>
</html>
views/scripts/index下面的index.phtml文件
<html>
<head>
<title>用户登录</title>
</head>
<h1>登录入口</h1>
<form action="/login/login" method="post">
<table>
<tr>
<td>用户id:</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td>用户密码:</td>
<td><input type="password" name="pwd"/></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>
<td><input type="reset" value="重新填写"/></td>
</tr>
</table>
</form>
<?=$this->err?>
</html>