Jump to content

JS/PHP work together with cookies?


zang8027

Recommended Posts

If i create a cookie in javascript that holds a certain value, can i access the same thing with php?

 

I want to create a cookie from a session..

 

so like..

 

$user = $_SESSON['priv'];

 

and i want to create a cookie that contains the value $user. But I am using javascript to find out if cookies are enabled or not. If they are, i would like to create a cookie. So my code block is like:

 

<script type="text/javascript">
if(!window.navigator.cookieEnabled){
  alert('Please enable cookies to continue');
}else{
  [CREATE A COOKIE HERE FROM THE PHP SESSION VALUE]
}

</script>

 

 

 

 

the reason i am doing this is because for some reason, my session is being messed up when you leave my site but enter it again

Link to comment
https://forums.phpfreaks.com/topic/144970-jsphp-work-together-with-cookies/
Share on other sites

Javascript is a client side language, while PHP is server side...

 

when page is displayed in the browser, PHP's job is already done. Javascript's job starts there after. so you cannot execute a PHP code directly depending on condition checked in JS.

 

however to achieve this, you can check condition in JS and redirect the user if cookie is set to another PHP page, which will set the cookie.

 

hope it is clear.

 

Sam

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.