简体   繁体   English

javascript函数保存到cookie

[英]javascript function save to cookies

For now, i have a function macs, and i need to implement this function to save inside a cookie and have it stored in mysql.. 现在,我有一个功能Mac,我需要实现此功能以保存在cookie中,并将其存储在mysql中。

So how am i supposed to have this function together? 那么我应该如何一起具有此功能?

        <script language="JavaScript">

        function getMacAddress(){
          document.macaddressapplet.setSep( "-" );
          return (document.macaddressapplet.getMacAddress());
        }

        function setCookie(c_name,value,expiredays)
        {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
        }

        setCookie('cookie_name','getMacAddress()','1');
        </script> 
        <body>

        <?php
         //Defaults to 1
        $javascript_cookie = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;
        echo "$javascript_cookie";

        // db insert query
        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbname = 'registration';
        mysql_connect($dbhost, $dbuser) or die("Could not connect database");
        mysql_select_db($dbname);
        $sql_query = mysql_query("SELECT * from user WHERE UserID ='".$_POST['newUserID']."'");
        $sql = "INSERT INTO test(mac) VALUES ('".$javascript_cookie."')";
        mysql_query($sql);
        ?>

Here are the two functions I use to handle cookies: 这是我用来处理Cookie的两个函数:

function writeCookie(name,value,days) {
    var date, expires;
    if (days) {
        date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();}
    else{
        expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var i, c, ca, nameEQ = name + "=";
    ca = document.cookie.split(';');
    for(i=0;i < ca.length;i++) {
        c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length,c.length);
        }
    }
    return '';
}
  • name is the name of the information you want to store name是您要存储的信息的名称
  • value its value 重视其价值
  • days is to set and expiration date or not if empty 是要设置的,到期日期是否为空

Use a Javascript library like Dojo or JQuery . 使用Java库,例如DojoJQuery They have good simpliefied interfaces for common functionality like this, and hide most of the messy cross-browser compatibility issues behind their API. 它们具有良好的简化接口来实现此类通用功能,并且将大多数凌乱的跨浏览器兼容性问题隐藏在其API的后面。

Well, for the JavaScript/setCookie part, if you replace 'Text3123234' with getMacAddress(), that should get you started. 好吧,对于JavaScript / setCookie部分,如果用getMacAddress()替换'Text3123234',那应该会让您入门。 So that line should look like: 因此,该行应如下所示:

setCookie('cookie_name', getMacAddress(), 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM