Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.