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!

有帮助吗?

解决方案

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.

其他提示

It could be done in JavaScript but there is no reason not to use PHP. The manual page is quite straightforward.

If you do implement it in JavaScript, simply break out into a regular page and include your script in the way you would usually. You'll need a window.location.href call in there to redirect after setting the cookie.

Please use google: PHP function

Syntax: bool setcookie ( $name , $value , $expire , $path );

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top