php
  • cookies
  • 2015-03-19 35 views 0 likes 
    0

    饼干内容我对设置得到的参数PHP

    $id = "1" 
        $password = "test"; 
        $cookie_name = "megamitch_server_status"; 
        $cookie_time = (3600 * 24 * 30); // 30 days 
        setcookie ($cookie_name, 'usr='.$id.'&hash='.$password, time() + $cookie_time); 
    

    我怎么能得到的USR该cookie名“megamitch_server_status”里面的值这个cookie?

    任何帮助,意见,建议将不胜感激。谢谢!

    +0

    你试过了吗? – Rizier123 2015-03-19 03:19:37

    +0

    到目前为止,我所尝试的是echo $ _COOKIE ['megamitch_server_status'] ['usr'];但可悲的是,没有工作 – 2015-03-19 03:21:56

    回答

    0

    这应该为你工作:

    (在这里,我只是用preg_match_all()提取数据)

    <?php 
    
        preg_match_all("/usr=(.*)&hash=(.*)/", $_COOKIE["megamitch_server_status"], $matches); 
        echo "usr: " . $matches[1][0]; 
        echo "hash: " . $matches[2][0]; 
    
    ?> 
    

    输出:

    usr: 1 
    hash: test 
    

    或者如果你愿意,你可以存储你的cookies像这样:

    setcookie ($cookie_name . "[usr]", $id, time() + $cookie_time); 
    setcookie ($cookie_name . "[hash]", $password, time() + $cookie_time); 
    

    然后你可以像这样访问它们:

    echo $_COOKIE["megamitch_server_status"]["usr"]; 
    echo $_COOKIE["megamitch_server_status"]["hash"]; 
    
    +0

    为什么在用户中,我得到了“= 1”?其据称只有“1”。 – 2015-03-19 03:30:58

    +1

    @CodeDemon忘我忘记更新我的答案,现在它应该适合你 – Rizier123 2015-03-19 03:31:37

    +0

    @CodeDemon不客气! – Rizier123 2015-03-19 03:35:24

    相关问题