Quantcast

cookies

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

cookies

backdoc-2
Is there any reason why this would not be setting a cookie?  The code
doesn't generate any errors.  It just doesn't set a cookie.  If I put
in setcookie('name', 'value'), that will work.

$cookie = md5($username . $id . "_somerandomstring_");
Zend_Loader::loadClass("Zend_Http_Client");
require_once "Zend/Http/Cookie.php";
$client = new Zend_Http_Client();
$cookie = new Zend_Http_Cookie('foo', $cookie, '.mydomain.com', time()
+ 7200, '/path');
$client->setCookieJar();
$client->setCookie($cookie);

The code above raises a side question.  I don't want to get
sidetracked.  But, I guess I'll ask it now, what's the difference
between require_once and Zend_Loader::loadClass?

TIA,
Darren
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: cookies

Isaak Malik
require_once is an internal PHP way of including files while Zend_Loader::loadClass is part of the Zend FW which is more advanced and does the following:
- If the class has already been included this method will abort execution (which will also prevent PHP errors from showing up);
- Tries to find the path to the file automatically
- Uses the Zend FW exception error reporting which allows you to modify error reporting easily

I'm not familiar with the Zend_Http_Cookie class so I can't help you with that, all I can say is that you should try the same without the path and domain, also check if this class doesn't automatically include the timestamp with your expiration time, if so your expiration time would be unrealistically high and would cause problems with some browsers, like Internet Explorer.

On Sat, Apr 5, 2008 at 5:13 PM, darren <[hidden email]> wrote:
Is there any reason why this would not be setting a cookie?  The code
doesn't generate any errors.  It just doesn't set a cookie.  If I put
in setcookie('name', 'value'), that will work.

$cookie = md5($username . $id . "_somerandomstring_");
Zend_Loader::loadClass("Zend_Http_Client");
require_once "Zend/Http/Cookie.php";
$client = new Zend_Http_Client();
$cookie = new Zend_Http_Cookie('foo', $cookie, '.mydomain.com', time()
+ 7200, '/path');
$client->setCookieJar();
$client->setCookie($cookie);

The code above raises a side question.  I don't want to get
sidetracked.  But, I guess I'll ask it now, what's the difference
between require_once and Zend_Loader::loadClass?

TIA,
Darren

--
Isaak Malik
Web Developer
[hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: cookies

weierophinney
Administrator
In reply to this post by backdoc-2
-- darren <[hidden email]> wrote
(on Saturday, 05 April 2008, 10:13 AM -0500):

> Is there any reason why this would not be setting a cookie?  The code
> doesn't generate any errors.  It just doesn't set a cookie.  If I put
> in setcookie('name', 'value'), that will work.
>
> $cookie = md5($username . $id . "_somerandomstring_");
> Zend_Loader::loadClass("Zend_Http_Client");
> require_once "Zend/Http/Cookie.php";
> $client = new Zend_Http_Client();
> $cookie = new Zend_Http_Cookie('foo', $cookie, '.mydomain.com', time()
> + 7200, '/path');
> $client->setCookieJar();
> $client->setCookie($cookie);

Zend_Http_Cookie is for use with Zend_Http_Client which is for making
requests to *other* services -- not for setting cookies for a client
browser. Use setcookie().

> The code above raises a side question.  I don't want to get
> sidetracked.  But, I guess I'll ask it now, what's the difference
> between require_once and Zend_Loader::loadClass?

Zend_Loader::loadClass() allows you to pass a class name, and optionally
a list of directories in which to look for the class file. Typically,
require_once will be slightly faster... as long as the file is in your
include_path.

--
Matthew Weier O'Phinney
Software Architect       | [hidden email]
Zend - The PHP Company   | http://www.zend.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: cookies

backdoc-2
Thanks guys.  As I had said, I had originally set the cookie with
setcookie().  But, I kept getting an index error when I tried to
access it.  That's why I thought I'd try the Zend_Http_Cookie class.
After setting it with setcookie(), should I be able to access it in my
init() or preDispatch() functions in my controllers?

Thanks for the explanations.



On Sat, Apr 5, 2008 at 1:24 PM, Matthew Weier O'Phinney
<[hidden email]> wrote:

> -- darren <[hidden email]> wrote
>  (on Saturday, 05 April 2008, 10:13 AM -0500):
>
> > Is there any reason why this would not be setting a cookie?  The code
>  > doesn't generate any errors.  It just doesn't set a cookie.  If I put
>  > in setcookie('name', 'value'), that will work.
>  >
>  > $cookie = md5($username . $id . "_somerandomstring_");
>  > Zend_Loader::loadClass("Zend_Http_Client");
>  > require_once "Zend/Http/Cookie.php";
>  > $client = new Zend_Http_Client();
>  > $cookie = new Zend_Http_Cookie('foo', $cookie, '.mydomain.com', time()
>  > + 7200, '/path');
>  > $client->setCookieJar();
>  > $client->setCookie($cookie);
>
>  Zend_Http_Cookie is for use with Zend_Http_Client which is for making
>  requests to *other* services -- not for setting cookies for a client
>  browser. Use setcookie().
>
>
>  > The code above raises a side question.  I don't want to get
>  > sidetracked.  But, I guess I'll ask it now, what's the difference
>  > between require_once and Zend_Loader::loadClass?
>
>  Zend_Loader::loadClass() allows you to pass a class name, and optionally
>  a list of directories in which to look for the class file. Typically,
>  require_once will be slightly faster... as long as the file is in your
>  include_path.
>
>  --
>  Matthew Weier O'Phinney
>  Software Architect       | [hidden email]
>  Zend - The PHP Company   | http://www.zend.com/
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: cookies

weierophinney
Administrator
-- darren <[hidden email]> wrote
(on Saturday, 05 April 2008, 10:51 PM -0500):
> Thanks guys.  As I had said, I had originally set the cookie with
> setcookie().  But, I kept getting an index error when I tried to
> access it.  That's why I thought I'd try the Zend_Http_Cookie class.
> After setting it with setcookie(), should I be able to access it in my
> init() or preDispatch() functions in my controllers?

You access cookies via $_COOKIE, even in ZF... but cookies you set
cannot be accessed in the same request that they are set, which may be
the issue you're having.

> Thanks for the explanations.
>
>
>
> On Sat, Apr 5, 2008 at 1:24 PM, Matthew Weier O'Phinney
> <[hidden email]> wrote:
> > -- darren <[hidden email]> wrote
> >  (on Saturday, 05 April 2008, 10:13 AM -0500):
> >
> > > Is there any reason why this would not be setting a cookie?  The code
> >  > doesn't generate any errors.  It just doesn't set a cookie.  If I put
> >  > in setcookie('name', 'value'), that will work.
> >  >
> >  > $cookie = md5($username . $id . "_somerandomstring_");
> >  > Zend_Loader::loadClass("Zend_Http_Client");
> >  > require_once "Zend/Http/Cookie.php";
> >  > $client = new Zend_Http_Client();
> >  > $cookie = new Zend_Http_Cookie('foo', $cookie, '.mydomain.com', time()
> >  > + 7200, '/path');
> >  > $client->setCookieJar();
> >  > $client->setCookie($cookie);
> >
> >  Zend_Http_Cookie is for use with Zend_Http_Client which is for making
> >  requests to *other* services -- not for setting cookies for a client
> >  browser. Use setcookie().
> >
> >
> >  > The code above raises a side question.  I don't want to get
> >  > sidetracked.  But, I guess I'll ask it now, what's the difference
> >  > between require_once and Zend_Loader::loadClass?
> >
> >  Zend_Loader::loadClass() allows you to pass a class name, and optionally
> >  a list of directories in which to look for the class file. Typically,
> >  require_once will be slightly faster... as long as the file is in your
> >  include_path.
> >
> >  --
> >  Matthew Weier O'Phinney
> >  Software Architect       | [hidden email]
> >  Zend - The PHP Company   | http://www.zend.com/
> >
>

--
Matthew Weier O'Phinney
Software Architect       | [hidden email]
Zend - The PHP Company   | http://www.zend.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: cookies

backdoc-2
Well, the set and the get are in two different controllers.  So, I'm
thinking not.

On Sat, Apr 5, 2008 at 11:23 PM, Matthew Weier O'Phinney
<[hidden email]> wrote:

> -- darren <[hidden email]> wrote
>  (on Saturday, 05 April 2008, 10:51 PM -0500):
>
> > Thanks guys.  As I had said, I had originally set the cookie with
>  > setcookie().  But, I kept getting an index error when I tried to
>  > access it.  That's why I thought I'd try the Zend_Http_Cookie class.
>  > After setting it with setcookie(), should I be able to access it in my
>  > init() or preDispatch() functions in my controllers?
>
>  You access cookies via $_COOKIE, even in ZF... but cookies you set
>  cannot be accessed in the same request that they are set, which may be
>  the issue you're having.
>
>
>
>  > Thanks for the explanations.
>  >
>  >
>  >
>  > On Sat, Apr 5, 2008 at 1:24 PM, Matthew Weier O'Phinney
>  > <[hidden email]> wrote:
>  > > -- darren <[hidden email]> wrote
>  > >  (on Saturday, 05 April 2008, 10:13 AM -0500):
>  > >
>  > > > Is there any reason why this would not be setting a cookie?  The code
>  > >  > doesn't generate any errors.  It just doesn't set a cookie.  If I put
>  > >  > in setcookie('name', 'value'), that will work.
>  > >  >
>  > >  > $cookie = md5($username . $id . "_somerandomstring_");
>  > >  > Zend_Loader::loadClass("Zend_Http_Client");
>  > >  > require_once "Zend/Http/Cookie.php";
>  > >  > $client = new Zend_Http_Client();
>  > >  > $cookie = new Zend_Http_Cookie('foo', $cookie, '.mydomain.com', time()
>  > >  > + 7200, '/path');
>  > >  > $client->setCookieJar();
>  > >  > $client->setCookie($cookie);
>  > >
>  > >  Zend_Http_Cookie is for use with Zend_Http_Client which is for making
>  > >  requests to *other* services -- not for setting cookies for a client
>  > >  browser. Use setcookie().
>  > >
>  > >
>  > >  > The code above raises a side question.  I don't want to get
>  > >  > sidetracked.  But, I guess I'll ask it now, what's the difference
>  > >  > between require_once and Zend_Loader::loadClass?
>  > >
>  > >  Zend_Loader::loadClass() allows you to pass a class name, and optionally
>  > >  a list of directories in which to look for the class file. Typically,
>  > >  require_once will be slightly faster... as long as the file is in your
>  > >  include_path.
>  > >
>  > >  --
>  > >  Matthew Weier O'Phinney
>  > >  Software Architect       | [hidden email]
>  > >  Zend - The PHP Company   | http://www.zend.com/
>  > >
>  >
>
>  --
>
>
> Matthew Weier O'Phinney
>  Software Architect       | [hidden email]
>  Zend - The PHP Company   | http://www.zend.com/
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: cookies

weierophinney
Administrator
-- darren <[hidden email]> wrote
(on Saturday, 05 April 2008, 11:44 PM -0500):
> Well, the set and the get are in two different controllers.  So, I'm
> thinking not.

If you're forwarding from one controller to the next in the same
request, it *could* be an issue. If they are viewed in separate
requests, then that's another problem entirely.

>
> On Sat, Apr 5, 2008 at 11:23 PM, Matthew Weier O'Phinney
> <[hidden email]> wrote:
> > -- darren <[hidden email]> wrote
> >  (on Saturday, 05 April 2008, 10:51 PM -0500):
> >
> > > Thanks guys.  As I had said, I had originally set the cookie with
> >  > setcookie().  But, I kept getting an index error when I tried to
> >  > access it.  That's why I thought I'd try the Zend_Http_Cookie class.
> >  > After setting it with setcookie(), should I be able to access it in my
> >  > init() or preDispatch() functions in my controllers?
> >
> >  You access cookies via $_COOKIE, even in ZF... but cookies you set
> >  cannot be accessed in the same request that they are set, which may be
> >  the issue you're having.
> >
> >
> >
> >  > Thanks for the explanations.
> >  >
> >  >
> >  >
> >  > On Sat, Apr 5, 2008 at 1:24 PM, Matthew Weier O'Phinney
> >  > <[hidden email]> wrote:
> >  > > -- darren <[hidden email]> wrote
> >  > >  (on Saturday, 05 April 2008, 10:13 AM -0500):
> >  > >
> >  > > > Is there any reason why this would not be setting a cookie?  The code
> >  > >  > doesn't generate any errors.  It just doesn't set a cookie.  If I put
> >  > >  > in setcookie('name', 'value'), that will work.
> >  > >  >
> >  > >  > $cookie = md5($username . $id . "_somerandomstring_");
> >  > >  > Zend_Loader::loadClass("Zend_Http_Client");
> >  > >  > require_once "Zend/Http/Cookie.php";
> >  > >  > $client = new Zend_Http_Client();
> >  > >  > $cookie = new Zend_Http_Cookie('foo', $cookie, '.mydomain.com', time()
> >  > >  > + 7200, '/path');
> >  > >  > $client->setCookieJar();
> >  > >  > $client->setCookie($cookie);
> >  > >
> >  > >  Zend_Http_Cookie is for use with Zend_Http_Client which is for making
> >  > >  requests to *other* services -- not for setting cookies for a client
> >  > >  browser. Use setcookie().
> >  > >
> >  > >
> >  > >  > The code above raises a side question.  I don't want to get
> >  > >  > sidetracked.  But, I guess I'll ask it now, what's the difference
> >  > >  > between require_once and Zend_Loader::loadClass?
> >  > >
> >  > >  Zend_Loader::loadClass() allows you to pass a class name, and optionally
> >  > >  a list of directories in which to look for the class file. Typically,
> >  > >  require_once will be slightly faster... as long as the file is in your
> >  > >  include_path.
> >  > >
> >  > >  --
> >  > >  Matthew Weier O'Phinney
> >  > >  Software Architect       | [hidden email]
> >  > >  Zend - The PHP Company   | http://www.zend.com/
> >  > >
> >  >
> >
> >  --
> >
> >
> > Matthew Weier O'Phinney
> >  Software Architect       | [hidden email]
> >  Zend - The PHP Company   | http://www.zend.com/
> >
>

--
Matthew Weier O'Phinney
Software Architect       | [hidden email]
Zend - The PHP Company   | http://www.zend.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: cookies

backdoc-2
Thanks.  I finally got it.  I think I wasn't setting enough parameters
in setcookie().   So, I had trouble accessing it.  It seems to be
working properly now.

Thanks again.

On Sun, Apr 6, 2008 at 1:07 AM, Matthew Weier O'Phinney
<[hidden email]> wrote:

> -- darren <[hidden email]> wrote
>  (on Saturday, 05 April 2008, 11:44 PM -0500):
>
> > Well, the set and the get are in two different controllers.  So, I'm
>  > thinking not.
>
>  If you're forwarding from one controller to the next in the same
>  request, it *could* be an issue. If they are viewed in separate
>  requests, then that's another problem entirely.
>
>
>
>  >
>  > On Sat, Apr 5, 2008 at 11:23 PM, Matthew Weier O'Phinney
>  > <[hidden email]> wrote:
>  > > -- darren <[hidden email]> wrote
>  > >  (on Saturday, 05 April 2008, 10:51 PM -0500):
>  > >
>  > > > Thanks guys.  As I had said, I had originally set the cookie with
>  > >  > setcookie().  But, I kept getting an index error when I tried to
>  > >  > access it.  That's why I thought I'd try the Zend_Http_Cookie class.
>  > >  > After setting it with setcookie(), should I be able to access it in my
>  > >  > init() or preDispatch() functions in my controllers?
>  > >
>  > >  You access cookies via $_COOKIE, even in ZF... but cookies you set
>  > >  cannot be accessed in the same request that they are set, which may be
>  > >  the issue you're having.
>  > >
>  > >
>  > >
>  > >  > Thanks for the explanations.
>  > >  >
>  > >  >
>  > >  >
>  > >  > On Sat, Apr 5, 2008 at 1:24 PM, Matthew Weier O'Phinney
>  > >  > <[hidden email]> wrote:
>  > >  > > -- darren <[hidden email]> wrote
>  > >  > >  (on Saturday, 05 April 2008, 10:13 AM -0500):
>  > >  > >
>  > >  > > > Is there any reason why this would not be setting a cookie?  The code
>  > >  > >  > doesn't generate any errors.  It just doesn't set a cookie.  If I put
>  > >  > >  > in setcookie('name', 'value'), that will work.
>  > >  > >  >
>  > >  > >  > $cookie = md5($username . $id . "_somerandomstring_");
>  > >  > >  > Zend_Loader::loadClass("Zend_Http_Client");
>  > >  > >  > require_once "Zend/Http/Cookie.php";
>  > >  > >  > $client = new Zend_Http_Client();
>  > >  > >  > $cookie = new Zend_Http_Cookie('foo', $cookie, '.mydomain.com', time()
>  > >  > >  > + 7200, '/path');
>  > >  > >  > $client->setCookieJar();
>  > >  > >  > $client->setCookie($cookie);
>  > >  > >
>  > >  > >  Zend_Http_Cookie is for use with Zend_Http_Client which is for making
>  > >  > >  requests to *other* services -- not for setting cookies for a client
>  > >  > >  browser. Use setcookie().
>  > >  > >
>  > >  > >
>  > >  > >  > The code above raises a side question.  I don't want to get
>  > >  > >  > sidetracked.  But, I guess I'll ask it now, what's the difference
>  > >  > >  > between require_once and Zend_Loader::loadClass?
>  > >  > >
>  > >  > >  Zend_Loader::loadClass() allows you to pass a class name, and optionally
>  > >  > >  a list of directories in which to look for the class file. Typically,
>  > >  > >  require_once will be slightly faster... as long as the file is in your
>  > >  > >  include_path.
>  > >  > >
>  > >  > >  --
>  > >  > >  Matthew Weier O'Phinney
>  > >  > >  Software Architect       | [hidden email]
>  > >  > >  Zend - The PHP Company   | http://www.zend.com/
>  > >  > >
>  > >  >
>  > >
>  > >  --
>  > >
>  > >
>  > > Matthew Weier O'Phinney
>  > >  Software Architect       | [hidden email]
>  > >  Zend - The PHP Company   | http://www.zend.com/
>  > >
>  >
>
>  --
>
>
> Matthew Weier O'Phinney
>  Software Architect       | [hidden email]
>  Zend - The PHP Company   | http://www.zend.com/
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: cookies

pat-61
In reply to this post by backdoc-2
RE: Remember  and Cookies

I've found it convenient to encapsulate cookie functions into an object.
It worked last time I checked although I've been thinking about  moving the
digest,  AKA secret-salt, into the cookie_jar  as a function $this->
getDigest();
so that everything stays in sync.

If I remember correctly, there can be problems with cookies on Windows.
Something related to domains, especially on private networks with no DNS.
Cookies are only available with certain URLs (specified when you set the
cookie).
If your local network is using IP addresses instead of domain names,
nasty problems may arise.

cheers,

pat



<?php
class cookie_jar
{
    private $_cookie_name;
    private $_cookie_contents;
    private $_db_cookie;
    private $_cookie_expire;
    private $_user_name;
    private $_success = false;
   
   
    function __construct ($name)
    {
        $this->_cookie_name = $name;
    }
    function get_cookie_name ()
    {
        return $this->_cookie_name;
    }
    function get_name()
    {
         return $this->_user_name;
    }
    function set_expire($expire)
    {
       $this->_cookie_expire = $expire;
    }
    function get_db_cookie()
    {
         return $this->_db_cookie;
    }
        function get_contents()
    {
         return $this->_cookie_contents;
    }
   
    function get_cookie()
    {
        if(isset($_COOKIE[$this->_cookie_name])){
            $this->_cookie_contents = $_COOKIE[$this->_cookie_name];
            list($username, $cookie) = explode("+",$this->_cookie_contents);
            $this->_user_name = $username;
            $this->_db_cookie = $cookie;
            $this->_cookie_expire = $cookie;
            $this->_success = true;
        }
    }
    function set_cookie()
    {
        setcookie($this->_cookie_name,$this->_cookie_contents ,
$this->_cookie_expire,"/",false,0);
    }
    function unset_cookie()
    {
        setcookie($this->_cookie_name,'', time()-42000,"/",false,0);
    }
                     
    function make_cookie($name, $pass, $digest)
    {
        $this->_user_name = $name;
        $this->_db_cookie = md5("$pass" . "$digest" );
        $this->_cookie_contents = implode("+", array($this->_user_name,  
$this->_db_cookie));
    }  
}
?>
>  

Loading...