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

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

WBOY
Release: 2016-06-13 13:16:07
Original
882 people have browsed it

为什么我的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变量,不存在则实例化
}
?>
Copy after login


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>
Copy after login





------解决方案--------------------
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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template