It depends what type of cookie. If you're just starting out in PHP, make sure you read up asap on sessions, the $_SESSION[] array quickly becomes a vital tool and you'll spend most of your time passing variables into your session using the JQuery.load() AHAH method.
3rd party cookies will, for the most part, look after themselves and any that you use yourself can either be used wholly within Javascript or passed into the PHP session for further manipulation or storage (via database/xml) there.
Remember that, when writing internal scripts, you can pass PHP variables into it; so the following will work:
PHP Code:
$(document).ready(function () {
$('#click-me').click(function () {
alert('<?php echo $_SESSION['username']; ?>');
});
});
In this case, the PHP is written into the script server-side, then sent to the page. The actual source for the code as you would see it in the browser would read:
PHP Code:
$(document).ready(function () {
$('#click-me').click(function () {
alert('ApacheTech');
});
});
You can make external scripts behave like this as well, if you add a .htaccess file with some alternate file headers in it to the js folder, then start your scripts with:
PHP Code:
<?php header("Content-type: text/javascript; charset: UTF-8"); ?>
However, this can screw up with remotely hosted files and with SVN builds like Git.
After our epic discussion on the new cookie laws, I'm unsure of what to recommend here. My view of it is that if it is something that only affects the client side, such as css theme or page text size then keep it in JS/JQ. If it's anything that is better handled by PHP, such as username, anything that needs to go in a database, etc; pass it to the $_SESSION using AHAH with JQ.
Bookmarks