2009年1月23日星期五

Re: [fw-core] Cookie question

Oops,

$_COOKIE['search_hit'] = true;

should instead be set with setcookie():

setcookie('search_hit', 1);

-Hector


On Fri, Jan 23, 2009 at 9:59 AM, Hector Virgen <djvirgen@gmail.com> wrote:
I would probably use a controller plugin to do this. The nice thing about controller plugins is that they always run for every request, unless explicitly coded to disable itself or something.

From within the plugin, you could examine the request to see if the user came to your site from a search engine. If so, set a cookie to indicate this. Then, check the cookie to see if its true and if it is append a new action to the action stack.

I would hook into routeStartup or one of the other hooks that only run once per request.

SearchEngineDetector.php

<?php
class SearchEngineDetector extends Zend_Controller_Plugin_Abstract
{
    protected $_searchHit = false;

    public function RouteStartup(Zend_Controller_Request_Abstract $request)
    {
        if ($_COOKIE['search_hit'] OR preg_match('/google.com/', $request->getServer('HTTP_REFERER'))) {
            // Set cookie
            $_COOKIE['search_hit'] = true;

            // Save value in plugin
            $this->_searchHit = true;
        }

        if (true ===$this->_searchHit) {
            $actionStack = Zend_Controller_Front::getPlugin('Zend_Controller_Plugin_ActionStack');
            $actionStack->pushStack(// your specialized action goes here //);
        }
    }
}


In bootstrap or whever you set up your plugins:

<?php
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new SearchEngineDetector());

-Hector



On Fri, Jan 23, 2009 at 6:20 AM, Raavi Raaj <raaviraaj77@gmail.com> wrote:
Hi,
 
I would like to...
- Serve additional content if my user is a search visitor.
 
For which...
- I need to set a cookie; after verifying she is indeed a search visitor based on the referrer.
- For the first request: Read a variable in my view helper to render additonal content and
- For subsequent requests: Read the cookie in my view helper to render the additional content
 
My problem...
- I dont know where to set my cookie?
- How to get hold of the variable in the first request?
 
Use case...
Show a prominent "Subscribe to our newsletter" form to only search visitors; on landing page and all subsequent pages.
 
I hope I have been able to explain myself. I can give ot another try of this does not make any sense :)
 
Can someone share some thoughts in this. All help is appreciated.
 
-R
 
P.S.
- I have a conventional setup
- I have a base controller My_Action which all my controllers extend.


没有评论: