바로

2016-09-25 5 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()를 가져올 수 없습니다 (둘 이상의 새로 고침 후). 너희들 여기 문제가 보이니? 그런데

는, 다음과 같은 간단한 예제가 완벽하게 작동합니다 :

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