EvilZone

Programming and Scripting => Web Oriented Coding => Topic started by: m0l0ko on December 21, 2012, 05:26:41 AM

Title: Passing variables from javascript to PHP via cookies
Post by: m0l0ko on December 21, 2012, 05:26:41 AM
When I was trying to find out if you could pass variables from javascript to PHP in the past, I came to the conclusion that you can't based on what other people were saying. You can create cookies with javascript then read them with PHP. Are there any major downsides to doing this (besides the fact that not everyone enables cookies)?
Title: Re: Passing variables from javascript to PHP via cookies
Post by: p_2001 on December 21, 2012, 05:44:17 AM
Cookies can be read by anyone.. Means that security is compromised.
Title: Re: Passing variables from javascript to PHP via cookies
Post by: ande on December 21, 2012, 12:57:03 PM
You can pass it as a GET or POST argument as well.

Code: [Select]
window.location = 'index.php?variable='+var;
Title: Re: Passing variables from javascript to PHP via cookies
Post by: m0l0ko on December 26, 2012, 03:47:13 AM
You can pass it as a GET or POST argument as well.

Code: [Select]
window.location = 'index.php?variable='+var;

Yeah the main downside there is that you have to update the page. Plus, if your variables are large strings, passing them through URL variables would be very messy looking.
Title: Re: Passing variables from javascript to PHP via cookies
Post by: Stackprotector on December 26, 2012, 09:56:07 AM
Yeah the main downside there is that you have to update the page. Plus, if your variables are large strings, passing them through URL variables would be very messy looking.
lol, Because of this Ajax is invented http://en.wikipedia.org/wiki/Ajax_(programming) . I use the ajax jquery function and send json data to my php's json controller (wich is actually an switch loop) . :D goodluck
Title: Re: Passing variables from javascript to PHP via cookies
Post by: ande on December 27, 2012, 05:40:46 PM
Yeah the main downside there is that you have to update the page. Plus, if your variables are large strings, passing them through URL variables would be very messy looking.

Javascript can sent GET/POST data without reloading the page using XMLHttpRequest. (pretty much what Factionwars said)