...

View Full Version : javascript, php, cookies



kennedysee
01-26-2010, 08:40 AM
the application returns a value "1" in the database instead of the mac address...

I'm able to document.write the output of the mac address, but not able to have it store in the database.

Is the program using the previous cookie? (but Ive deleted all the cookies from the pc)

but if i change the variable mac to string data, it keep refresh my webpage. Why is that so... (var mac="test data";)

Please help! :confused:

create_users.php



<script language="JavaScript">

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

function setCookie(c_name,value) {
document.cookie = c_name + "=" +escape(value);
}

//var mac="test data";
var mac = getMacAddress();


setCookie('cookie_name',mac);
window.location = "checkAvailability.php";

</script>

<script type="text/javascript">
document.write(getMacAddress());
</script>



checkAvailiability.php


$dbhost = 'localhost';
$dbuser = 'root';
$dbname = 'registration';
mysql_connect($dbhost, $dbuser) or die("Could not connect database");
mysql_select_db($dbname);
$javascript_cookie_value = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;
mysql_query("INSERT INTO test (mac) VALUES ('$javascript_cookie_value')");

Fou-Lu
01-26-2010, 02:26 PM
In JS, when you use a variable for an identifier, do you still need to wrap it up with its single quotes? I'm not a JS kinda guy, but the only thing I can think of that may be an issue is:


document.cookie = c_name + "=" +escape(value);


Many years ago when I learned js, I recall cookies were set with document.cookie = 'name=value'; combination. So I'm thinking that you still need them quotes:


document.cookie = "'" + c_name + "=" + escape(value) + "'";


Also, get used to having a 1 for you're result anyway. Mac's require a client technology to retrieve, and only if the system supports and allows it. I assure you that no java program will have system access on my machines, nor will I be using internet explorer.

kennedysee
01-27-2010, 06:02 AM
Do you have any idea where the value "1" come from??

Len Whistler
01-27-2010, 06:12 AM
Do you have any idea where the value "1" come from??

The value "1" is coming from the default value in this line if the cookie isn't set:

$javascript_cookie_value = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;

Because this line isn't working:

var mac = getMacAddress();


Replace with this line for error checking:

var mac="test data 556 555 777";

If the test data is sent to the cookie post in the Javascript Forum and ask them why this line does not work:

var mac = getMacAddress();


----------

kennedysee
01-27-2010, 06:26 AM
Is that possible that i set my cookie expiry to 1day with,



function setCookie(c_name,value) {
document.cookie = c_name + "=" +escape(value);
}


TO



function setCookie(c_name,value,1) {
document.cookie = c_name + "=" +escape(value);
}

Len Whistler
01-27-2010, 06:29 AM
Is that possible that i set my cookie expiry to 1day with,



function setCookie(c_name,value) {
document.cookie = c_name + "=" +escape(value);
}


TO



function setCookie(c_name,value,1) {
document.cookie = c_name + "=" +escape(value);
}


No expiry date is set, and I don't know what the default expiry date is for cookies.


---------------

kennedysee
01-27-2010, 06:33 AM
Is that possible to have it code when the user visit the webpage, the system will automatically delete the cookie and store this current cookie?

Len Whistler
01-27-2010, 06:35 AM
Is that possible to have it code when the user visit the webpage, the system will automatically delete the cookie and store this current cookie?

Just give the cookie the same name, it will overwrite the original cookie.


-----------

kennedysee
02-01-2010, 03:37 AM
The value "1" is coming from the default value in this line if the cookie isn't set:

$javascript_cookie_value = isset($_COOKIE["cookie_name"]) ? $_COOKIE["cookie_name"] : 1;

Because this line isn't working:

var mac = getMacAddress();


Replace with this line for error checking:

var mac="test data 556 555 777";

If the test data is sent to the cookie post in the Javascript Forum and ask them why this line does not work:

var mac = getMacAddress();


----------

Even i replace the line for error checking:

var mac="test data 556 555 777";

My internet explorer keep refreshing non-stop, same for my Firefox.

Len Whistler
02-01-2010, 05:24 AM
Even i replace the line for error checking:

var mac="test data 556 555 777";

My internet explorer keep refreshing non-stop, same for my Firefox.

This is the original code and it works with the test data.

http://www.codingforums.com/showpost.php?p=915030&postcount=13

The var mac = getMacAddress(); line will have to be sorted out in the Javascript Forums.




---------------------



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum