照着韩顺平PHP视频敲的代码,重新生成空白的session文件是怎么回事?

看的韩顺平的《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>

controllers下面的LoginController.php文件中函数loginAction()中的session相关代码

 session_start(); 
 $_SESSION['loginuser']=$loginuser[0];

是起作用的,生成了一个有内容的session文件。

controllers下面的HallController.php文件中函数gohalluiAction()中跟session相关的代码


        session_start();
        $this->view->loginuser=$_SESSION['loginuser'];

也是起作用的,所以我们能在views/scripts/hall下面的hall.phtml文件中看到代码

 <h1>欢迎<?=$this->loginuser['name']?>光临购物大厅</h>

在浏览器中第一次显示的时候<?=$this->loginuser['name']?>被替换成了名字。

但是一旦在views/scripts/hall下面的hall.phtml中点击“购买”,跳转到/shopping/addpro,也就是controllers下面的ShoppingController.php文件中的函数addproAction()的时候,里面的session相关代码

 session_start();
 $loginuser=$_SESSION['loginuser'];

执行的时候出问题了,生成了空白的session文件,在mysql数据库里发现$loginuser['id']对应的字段的值为Null,

然后在views/scripts/global下面的ok.phtml文件中执行

 window.location.href="/hall/gohallui";

然后跳转到hall.phtml, 发现


 <h1>欢迎<?=$this->loginuser['name']?>光临购物大厅</h>

中<?=$this->loginuser['name']?>没值,浏览器直接显示“欢迎光临购物大厅”。

我的httpd.conf与apache.ini文件中的相关配置应该都没有问题,然后我把上面session相关代码
换成了zend framework里面的风格,就是引用Zend/Session/Namespace.php, 发现还是一样的。
我百思不得其解,求高人指点。

controllers下面的LoginController.php文件中函数loginAction()中的session相关代码

session_start();
$_SESSION['loginuser']=$loginuser[0];
是起作用的,生成了一个有内容的session文件。

图片说明

controllers下面的HallController.php文件中函数gohalluiAction()中跟session相关的代码

    session_start();
    $this->view->loginuser=$_SESSION['loginuser'];

也是起作用的,所以我们能在views/scripts/hall下面的hall.phtml文件中看到代码

欢迎<?=$this->loginuser['name']?>光临购物大厅
在浏览器中第一次显示的时候<?=$this->loginuser['name']?>被替换成了名字。

图片说明

但是一旦在views/scripts/hall下面的hall.phtml中点击“购买”,跳转到/shopping/addpro,也就是controllers下面的ShoppingController.php文件中的函数addproAction()的时候,里面的session相关代码

session_start();
$loginuser=$_SESSION['loginuser'];
执行的时候出问题了,生成了空白的session文件,在mysql数据库里发现$loginuser['id']对应的字段的值为Null,

图片说明

图片说明

图片说明

然后在views/scripts/global下面的ok.phtml文件中执行

window.location.href="/hall/gohallui";
然后跳转到hall.phtml, 发现

欢迎<?=$this->loginuser['name']?>光临购物大厅

中<?=$this->loginuser['name']?>没值,浏览器直接显示“欢迎光临购物大厅”。

图片说明

我的httpd.conf与apache.ini文件中的相关配置应该都没有问题,然后我把上面session相关代码
换成了zend framework里面的风格,就是引用Zend/Session/Namespace.php, 发现还是一样的。
我百思不得其解,求高人指点。

但天才如我者当然是不会束手就擒的,我把这个shopping项目另外又拷了一份,起名为shopping2。我把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();
        if($this->getRequest()->getParam("mysid")!=Null){
            $sid=$this->getRequest()->getParam("mysid");
            session_id($sid);

        }

        session_start();


        $this->view->loginuser=$_SESSION['loginuser'];
        $this->view->books=$bookModel->fetchAll()->toArray();
        $this->render('hall');
    }



}

我把controllers下面的ShoppingController.php文件改成了下面这样:

 <?php

require_once "BaseController.php";
require_once APPLICATION_PATH.'/models/MyCart.php';
class ShoppingController extends BaseController
{

    public function showcartAction(){
        echo "显示购物车";
        exit();
    }

    public function addproAction()
    {   
        $sid=$this->getRequest()->getParam('PHPSESSID');
        $bookid=$this->getRequest()->getParam('bookid');
        $mycart=new MyCart();
        session_id($sid);
        session_start();
        $loginuser=$_SESSION['loginuser'];
        if($mycart->addProduct($loginuser['id'],$bookid)){
            $this->view->info="添加商品成功";
            $this->view->sid=$sid;
            $this->_forward("ok","global");
        }else{
            $this->view->info="添加商品失败";
            $this->_forward('err','global');
        }
    }


}

?>

我把views/scripts/global下面的ok.phtml文件改成了下面这样:

 <script type="text/javascript">
    alert('<?=$this->info?>');
    window.location.href="/hall/gohallui?mysid=<?=$this->sid?>";
</script>

我把views/scripts/hall下面的hall.phtml文件改成了下面这样:

 <html>
<head>
<title>购物大厅</title>
<script language="javascript">
    function goMyCart(){
        window.location.href="/shopping/showcart";
    }
</script>
</head>
<body>
<?php 
session_start();
$sid=session_id();;
?>
<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']?>&PHPSESSID=<?=$sid?>">购买</a></td>
</tr>

<?php }?>
<tr>
<td colspan=4><input type="button" onclick="goMyCart()" value="查看购物车"/></td>
</tr>

</table>
</body>

</html>

然后一切都搞定了。

图片说明

图片说明

图片说明

图片说明

图片说明

图片说明

图片说明

图片说明

上面有的图重复了,有的图没传上,应该是我用这个文本框上传图片的时候,还没等图片的路径更新就点击“上传图片了”。再发一遍。

图片说明

图片说明

图片说明

图片说明

图片说明

图片说明

图片说明

图片说明

这种小问题对咱们研究代数几何的才子来说当然是小case啦,但即使我采用自己修改后的方案达到了老韩PHP视频中相同的效果,在我存放session文件的目录下还是会生成空白的session文件,求精通PHP的高手指点迷津,不胜感激!

我采用自己修改后的方案达到了老韩PHP视频中相同的效果,在我存放session文件的目录下也只有一个有用的session文件,没有空白的session文件。

不好意思,说错了。我采用自己修改后的方案达到了老韩PHP视频中相同的效果,在我存放session文件的目录下也只有一个有用的session文件。
我不明白的是,为什么我一开始按照老韩视频中敲出一模一样的代码,却达不到相同的效果呢?为什么会产生空白的session文件呢?
产生session文件的过程是这样的:
登录的时候产生一个非空的有用的session文件,然后第一次点击“购买”,一次产生两个空白的session文件。以后再点击“购买”就不会产生session文件了,这时存放session文件的目录下就总是一个有用的session文件,两个空白的session文件。不管点击多少次“购买”, 在命令行中输入“select * from mycart”, 在userid字段下的值都是NULL。

求精通PHP的高手指点迷津,不胜感激!

图片说明

原来老韩视频中session也出现问题了,只不过我的出现问题早一点,他的要晚一点。

各位看官,你们用Zend Framework做开发的时候session是不是也出现过问题呢?

图片说明

老韩也一头雾水啦!哇嘎嘎!!!