Hi there, I need to check if a user is logged in, before executing some code when my javascript gets triggered on a button press
however, I have not been able to determine how to do this in javascript.

I have searched stack-overflow, and the web, and have found nothing that seams to work. I hope you can help!

I am using Drupal 6.16,

I have tried using this method:

var cookie_value = jQuery.cookie("DRUPAL_UID");
if(cookie_value != 0)
{
// execute some code...
}

but I always get "null"

I have thought of setting a variable in my PHP and passing it to javascript, but what happens when the user logs out?

Comments

jaypan’s picture

Can you show us how you are you adding your javascript to the page?

Contact me to contract me for D7 -> D10/11 migrations.

alfaguru’s picture

A javascript setting should be OK, using something like

drupal_add_js(array('mymodule'=>array('loggedin' => $loggedin)), 'setting');

If you are using page caching then every cached page will be one built for an anonymous user, so they'll all have the correct value.