/    Sign up×
Community /Pin to ProfileBookmark

cookies wth php and javascript

Hi,

i want to create a cookie using javascript but also being able to read it with php. Is it possible? for example:

[CODE]In javascript:

create cookie called session

In php:

if(session cookie exists) { echo “whatever”; }[/CODE]

Thank you.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@supercainauthorOct 08.2012 — Forget that question, i just found out it is indeed possible and how to do it. However now i need to make the cookie get cleared as the user leaves the page where it was created. Can you give me a hint?

Thank you.
Copy linkTweet thisAlerts:
@supercainauthorOct 08.2012 — Okay, i figured out the answer to my last question too. Now i have a new problem. I used the codes below and they work fine in firefox and chrome, but as usual not in Explorer (its a piece of ****, you know)

[CODE]
<script>document.cookie ='chat=sesion; path=index.php';</script>
<script language="javascript">
function clearCookie(){
document.cookie='chat=;expires=Thu, 01-Jan-70 00:00:01 GMT;';
}
window.onunload = clearCookie;
</script>[/CODE]


I would appreciate a cross-browser solution.

Thanks.
Copy linkTweet thisAlerts:
@Anthony_Oct 10.2012 — I'm not totally sure but perhaps IE requires some sort of required format? For testing purposes, use this code to set a cookie (taken from w3schools' website),
function setCookie(c_name,value,exdays) {
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
Copy linkTweet thisAlerts:
@supercainauthorOct 11.2012 — Thank you, i had already fixed. You have to use path=/ instead of path=index.php.
Copy linkTweet thisAlerts:
@supercainauthorOct 21.2012 — Sorry again but for some reason tis code is not working anymore:

<script language="javascript">

function clearCookie(){

document.cookie ='chat=; path=/';

}

window.onunload = clearCookie;

</script>[/QUOTE]


its supposed to clear the cookie named chat when closing the window but its not clearing it. Could you show me an alternative?

Thank you.
Copy linkTweet thisAlerts:
@supercainauthorOct 21.2012 — Sorry, i think i should have specified that i need the cookie to be deleted as either the TAB or the window is closed.
×

Success!

Help @supercain spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...