Search code examples
phpjqueryjquery-cookie

How to set a cookie with jQuery in a PHP function


I have a very basic validation script using PHP, and I want to set a cookie with jQuery Cookie plugin if they are validated. Being very new to PHP, I don't know how to code a Javascript function here:

if($validated) {
  //SET jQUERY COOKIE HERE: $.cookie('COOKIE_NAME', 'COOKIE_INFO', { expires: 1, path: '/' });)
  header("Location: correct.php");
} else {
  header("Location: incorrect.php");
}

I know I can use PHP for this, but later on I need to access the cookie in a jQuery function, and being far more familiar with client-side, I'd like it to be done using jQuery Cookie plugin.

Any guidance would be much appreciated!


Solution

  • I guess you are mixing concepts.

    You can set a cookie in both server and client sides. And this cookie can be read by both parts again.

    So you can set the cookie with PHP (http://php.net/manual/en/function.setcookie.php) and read it with jQuery (https://github.com/carhartl/jquery-cookie).

    Hope it helps. Regards.