Question

I don't know anything about agavi and there might be a lot of error in this..

I want to set up a cookie:

$this->getResponse()->setCookie( ID, 'ID=2342&ClickBannerID=634&SubID=&ClickDateTime=' + time(), mixed $lifetime = time() * 2, string $path = '/', string $domain = 'mydomain', bool $secure = null)


I get: Parse error: syntax error, unexpected T_VARIABLE

it's form: http://www.agavi.org/apidocs/index.html what am I doing wrong

No correct solution

OTHER TIPS

Usually within Agavi you set cookies on an AgaviView on the AgaviWebResponse object via its setCookie method. The method signature is as follows:

public function setCookie($name, $value, $lifetime = null, $path = null, $domain = null, $secure = null, $httponly = null)

This means, that you can set a simple cookie using the following syntax:

$this->getResponse()->setCookie('cookieName', $cookieValue);

You may use PHP's strtotime function to set an easily readable lifetime for the cookie if you want a cookie that is only valid for a certain amount of time:

$this->getResponse()->setCookie('cookieName', $cookieValue, '+14 days');

As your question states you may want to use the additional parameters (domain etc.) as well. Reading your question it may also be possible, that you want to set multiple cookies instead of one consisting of a string containing ID, ClickBannerId etc., but only you and your application (or your dev) can tell that.

Please note, that you can set the additional parameters per cookie or configure some sane defaults for your application in the app/config/factories.xml file per environment (usually for context _web_) to save on typing in your views:

<response class="AgaviWebResponse">
    <ae:parameters name="cookie_httponly">true</ae:parameters>
</response>

The valid parameter names are:

  • cookie_lifetime (lifetime in seconds)
  • cookie_path (path on the server the cookie should be available for)
  • cookie_domain (the domain the cookie is available on)
  • cookie_secure (set this to true if the cookie should only be available via HTTPS)
  • cookie_httponly (determine whether the cookie should only be allowed to use via HTTP and not client side scripts)

Please note, that the ae namespace was not necessary in older Agavi versions and thus it may be needed that you use parameter instead of ae:parameter.

To remove a cookie you just call unsetCookie with all the same parameters you used for setting the cookie. To get a cookie value simply call getCookie($name). As cookies are untrusted user provided information usually Agavi requires you to validate all incoming parameters, files, headers and cookies before you can access them in your actions and views. This means, that you may need to validate your cookie prior to accessing its value. You do this via a validate.xml file:

<validator class="string" source="cookie" required="false">
    <argument>cookieName</argument>
</validator>

This example is simplified and should not be used in production. Use source _cookie_ and the name of the cookie for the argument and then validate your cookie value according to your rules (format etc.). It may be necessary to write a custom validator if the builtin AgaviValidator classes (like string, regex etc.) are not sufficient, but this is a topic for another day. You may find the Agavi User FAQ somewhat helpful. Sorry for the shameless plug and good luck with your problem at hand. :-)

you mixed function declaration with its run

$this->getResponse()->setCookie( 'your-cookie-id','ID=2342&ClickBannerID=634&SubID=&ClickDateTime=' . time(),  time() * 2,  '/',  
'mydomain', null)

and as OcuS stated in comment - probably hire a PHP developer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top