Best way to pass event callback from JQuery/JavaScript to php function

The proper way is to use php to delete the cookie. Do not use javascript to delete the cookie - for security reasons user session cookies should be set with the httponly attribute, meaning they will not be accessible by javascript at all - both setcookie() and session_start() functions will allow you to set httponly cookies. Therefore, your only option is to send a request to a php script on your server and this script will delete the cookie with its response headers (e.g. using setcookie() function).

Making the logout request can be done in one of several ways:

  1. Use plain html link to your logout script - no javascript necessary (however, you can create the link with javascript, of course, or use location.href='...' to load the log out script).

  2. Submit a form that targets your log out script - either with javascript’s form.submit() function or with a form button.

  3. Use javascript (ajax) to send the log out request to the server - after you receive the response use javascript to redirect the user to the page that he should see after he has logged out.

1 Like