は右

2016-09-25 15 views
1
私は、次のクラスを書いた

、Cookie.phpは右

class Cookie extends Config{ 

//Variables declaration 
private $cookieName; 
private $cookieValue; 
private $cookieExpireTime; 
private $cookiePath; 
private $cookieDomain; 
private $cookieSecureThroughSSL; 
private $cookieOnlyThroughHTTP; 

//Constructor method, creates a new cookie with the assigned values 
function __construct($presetCookieName, 
        $presetCookieValue, 
        $presetCookieExpireTime, 
        $presetCookiePath='/', 
        $presetCookieDomain = NULL, 
        $presetCookieSecureThroughSSL = false, 
        $presetCookieOnlyThroughHTTP = true){ 

    $this->cookieName = $presetCookieName; 
    $this->cookieValue = $presetCookieValue; 
    $this->cookieExpireTime = $presetCookieExpireTime; 
    $this->cookiePath = $presetCookiePath; 
    $this->cookieDomain = $presetCookieDomain; 
    $this->cookieSecureThroughSSL = $presetCookieSecureThroughSSL; 
    $this->cookieOnlyThroughHTTP = $presetCookieOnlyThroughHTTP; 

    return $this->createCookie(); 
} 

//Clean cookie from possible malicious HTML code, or mistakenly typed spaces 
private function cleanCookieValue($value){ 
    return htmlspecialchars(str_replace(' ', '', $value)); 
} 

//Create a new cookie function 
public function createCookie(){ 
    return setcookie($this->cleanCookieValue($this->cookieName), 
        $this->cleanCookieValue($this->cookieValue), 
        $this->cleanCookieValue($this->cookieExpireTime), 
        $this->cleanCookieValue($this->cookiePath), 
        $this->cleanCookieValue($this->cookieDomain), 
        $this->cleanCookieValue($this->cookieSecureThroughSSL), 
        $this->cleanCookieValue($this->cookieOnlyThroughHTTP)); 
} 

そして、次のテストファイルを動作するようにしますsetcookie()を取得できません(2回以上リフレッシュした後も)。 あなたはここで問題を見ていますか?ところで

は、次の簡単な例では、完璧に動作します:

setcookie("token", "value", time()+60*60*24*100, "/"); 

if(isset($_COOKIE['token'])){ 
    echo 'Token succeeded'; 
} 
else{ 
    echo 'Token failed!'; 
} 

答えて

0

をクラスでは、あなたが3番目のパラメータを掲載していた$presetCookieExpireTimeではなく、「人生の秒」です。それを動作させるために -

$cookie = new Cookie("testCookie", "Value", time() + 3600, "/"); 
+0

はどうもありがとうございましたよ!出来た! – StackMaster