Heim  >  Artikel  >  Backend-Entwicklung  >  为什么小弟我的COOKIE这样无法使用?还有PHP一定要以这种方式编写的吗

为什么小弟我的COOKIE这样无法使用?还有PHP一定要以这种方式编写的吗

WBOY
WBOYOriginal
2016-06-13 13:16:07823Durchsuche

为什么我的COOKIE这样无法使用?还有PHP一定要以这种方式编写的吗?
这是前两天照书本上写的一段代码,注释是我自己写的,我在下面的login.php页面登录,无法取得cookie,但是我在login.php页面里最前面用echo $_POST["username"]却可以获取到从表单传过来的值,不知道是我cookiemanager这个类有错呢,还是哪里出错了?


  新手,对自定义函数的用法还不是很明白,再问一下大家PHP里都是采用这样把主要代码写成单独的一个页面,然后对其进行载入使用吗?或许问题有些小白,但这是熟悉PHP的一个重要步骤··

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php class CookieManager{
    public function Get_CookieV($key){    //获取cookie
        return $_COOKIE[$key];
    }
    public function Set_Array_Cookie($arryc){    //获取的cookie数组循环
        foreach($arryc as $key=>$value){
            setcookie($key,$value);
        }
    }
    public function Set_Cookie($key,$value){     //设定
        setcookie($key,$value);
    }
    public function Get_Array_Cookie(){
        return $_COOKIE;
    }
    public function up_Cookie($key,$value){
        setcookie($key,"",$time);
    }
}
global $cm;
if(!isset($cm)){
    $cm=new CookieManager();     //判断是否存在$cm变量,不存在则实例化
}
?>


PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php ob_start();
 require_once('inc/cookiemanager.php');
 $cm=new CookieManager();
 if(!$cm->Get_CookieV("username")){
     echo 
  <label>username:
  <input type="text" name="username">
  </label>
  <p>password:
    <label>
    <input type="text" name="password">
    </label>
  </p>
  <p>
    <label>
    <input type="submit" name="ButtonNname" value="登录">
    </label>
  </p>
  <p> </p>
EOT;
}
else{
    print_r($cm->Get_Array_Cookie());
    echo '<p><a href="loginend.php?out=true">退出</a>';
}

  ob_flush();
?>
</p>





------解决方案--------------------
PHP code
<?php class CookieManager{
    public function Get_CookieV($key){    //获取cookie
        return $_COOKIE[$key];
    }
    public function Set_Array_Cookie($arryc){    //获取的cookie数组循环
        foreach($arryc as $key=>$value){
            setcookie($key,$value);
        }
    }
    public function Set_Cookie($key,$value){     //设定
        setcookie($key,$value);
    }
    public function Get_Array_Cookie(){
        return $_COOKIE;
    }
    public function up_Cookie($key,$value){
        setcookie($key,"",$time);
    }
}
?> <div class="clear">
                 
              
              
        
            </div>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn