tags:

views:

117

answers:

4

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..

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);
        ?>
A: 

Use a Javascript library like Dojo or JQuery. They have good simpliefied interfaces for common functionality like this, and hide most of the messy cross-browser compatibility issues behind their API.

rmalayter
i have a function for that,http://stackoverflow.com/questions/2115690/javascript-calling-functionbut its not working, can you take alook?
kennedy
A: 

Well, for the JavaScript/setCookie part, if you replace 'Text3123234' with getMacAddress(), that should get you started. So that line should look like:

setCookie('cookie_name', getMacAddress(), 1);
BobS
Edited the post again, this is what ive now.. but the output turns out to be a value of "1" instead of mac address...
kennedy
You need to remove the quotes from the call to getMacAddress in the setCookie call. otherwise the value of the cookie will be the string 'getCookie()'.Please post what you get if you put the following call before the setCookie call: alert("MAC address is " + getMacAddress());
BobS
A: 

Here are the two functions I use to handle cookies:

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
  • value its value
  • days is to set and expiration date or not if empty
Mic
A: 

see here Save name to cookie

http://java.pakcarid.com/Cpp.aspx?sub=385&amp;ff=3050&amp;topid=40&amp;sls=25

lois