Register for your free account! | Forgot your password?

You last visited: Today at 05:31

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



PHP Cookie

Discussion on PHP Cookie within the Web Development forum part of the Coders Den category.

Reply
 
Old   #1

 
boxxiebabee's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 1,222
Received Thanks: 500
PHP Cookie

Servus,

ich hab mal vor längerer Zeit eine Klasse für Cookies geschrieben.
Wollte es grad testen, funktioniert aber nur bedingt.

Unter Firefox wird das Cookie normal gesetzt, unter Chrome jedoch nicht.

Dies funktioniert jedoch einwandfrei:
Code:
setcookie ("test", "value");
Klasse:
Code:
<?php
class SecureCookie
{
    private $name;
    private $secret;
    
    public function __construct($name, $secret)
    {
        $this->name = $name;
        $this->secret = $secret;
    }
    
    private function Generate($id, $expiration)
    {
        $key    = hash_hmac('sha512', $id . $expiration, $this->secret);
        $hash   = hash_hmac('sha512', $id . $expiration, $key);
        $cookie = $id . '|' . $expiration . '|' . $hash;
        return $cookie;
    }
    
    public function Verify()
    {
        if(!empty($_COOKIE[$this->name])) {
            list($id, $expiration, $hmac) = explode('|', $_COOKIE[$this->name]);
            $key  = hash_hmac('sha512', $id . $expiration, $this->secret);
            $hash = hash_hmac('sha512', $id . $expiration, $key);
            if ($hmac != $hash || $expiration < time()) {
                $this->Destroy();
                return false;
            }
            return true;
        }
        return false;
    }
    
    public function getID()
    {
        if (!empty($_COOKIE[$this->name])) {
            list($id, $expiration, $hmac) = explode('|', $_COOKIE[$this->name]);
            return $id;
        }
        return false;
    }
    
    public function Destroy()
    {
        setcookie($this->name, '', time() - 1000, '/');
    }
    
    public function setCookie($id, $remember = false)
    {
        if ($remember) {
            $expiration = time() + 2419200; // 28 days
        } else {
            $expiration = time() + 43200; // 12 hours
        }
        $cookie = $this->Generate($id, $expiration);
        setcookie($this->name, $cookie, $expiration, '/', $_SERVER['HTTP_HOST'], false, true);
    }
}
?>
Jemand ne Idee woran es liegt?
boxxiebabee is offline  
Old 10/08/2012, 10:02   #2
 
NotEnoughForYou's Avatar
 
elite*gold: 0
Join Date: Jun 2010
Posts: 3,406
Received Thanks: 2,024
Hab da eine Vermutung, editiere es sobald ich zuhause bin.

#Edit: bei mir klappt es im Chrome.

Aufruf
PHP Code:
<?php
$cookie 
= new SecureCookie("testcookie""sss");

$cookie->setCookie(210099999);

if(isset(
$_COOKIE['testcookie'])){
    echo
'existiert';
}
else {
    echo
'existiert nicht';
}
?>
NotEnoughForYou is offline  
Old 10/08/2012, 10:05   #3
 
kissein's Avatar
 
elite*gold: 0
Join Date: Sep 2005
Posts: 427
Received Thanks: 87
Wird auf localhost getestet?
Code:
 setcookie($this->name, $cookie, $expiration, '/', $_SERVER['HTTP_HOST'], false, true);
setzt mal http_host auf 127.0.0.1 beim Chrome test dann müsste es klappen.
kissein is offline  
Old 10/09/2012, 08:40   #4

 
boxxiebabee's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 1,222
Received Thanks: 500
Hey,

danke für die Antworten. Nachdem ich es mal auf den Webspace hochgeladen habe kamen gleich mal 2 Fehlermeldungen. Da hat sich anscheinend davor noch irgendein echo versteckt, demzufolge wurden die Header schon gesendet und somit konnte keine Session/Cookie mehr gesetzt werden.
boxxiebabee is offline  
Old 10/09/2012, 17:23   #5
 
elite*gold: 0
Join Date: Jan 2012
Posts: 759
Received Thanks: 416
Hast du irgendeinen bestimmten Grund dafür, dass du Generate, Verify und Destroy am Anfang groß, die Setter und Getter aber klein schreibst? Wenn nicht ist es sicher nur vorteilhaft, wenn du dich auf einen "Stil" beschränkst - bestenfalls natürlich für den "üblicheren" Stil (erster Buchstabe klein).
dowhile is offline  
Old 10/09/2012, 20:06   #6

 
boxxiebabee's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 1,222
Received Thanks: 500
Quote:
Originally Posted by dowhile View Post
Hast du irgendeinen bestimmten Grund dafür, dass du Generate, Verify und Destroy am Anfang groß, die Setter und Getter aber klein schreibst? Wenn nicht ist es sicher nur vorteilhaft, wenn du dich auf einen "Stil" beschränkst - bestenfalls natürlich für den "üblicheren" Stil (erster Buchstabe klein).
Wenn ne Funktionen aus mehreren Wörtern besteht, und das erste ein Verb ist, schreib ich es eig. immer klein, ist einfach eine Angewohnheit.
Man könnte natürlich alles klein / groß oder was auch immer schreiben, aber nachdem mein Editor sowieso die Funktionen vorschlägt, und es vermutlich nicht opensource wird ist mir das eig. relativ egal.
boxxiebabee is offline  
Reply


Similar Threads Similar Threads
Cookie Fix
06/28/2012 - DarkOrbit - 1 Replies
So all you have to do is use Google Chrome. And use incognito mode. What this does is No save cookies? No bad
Cookie GodMT2
05/16/2009 - Metin2 Private Server - 4 Replies
Hello please a give code which was a tamper data a GodMT2 a free items



All times are GMT +1. The time now is 05:33.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.