/    Sign up×
Community /Pin to ProfileBookmark

document.cookie question and php $_COOKIE

We all know that js is only a frontend script and php is a backend script.
However, in js a statement such as

[code]
document.cookie = “DATA=”+data;
[/code]

On the php side:

[code]
<?php
session_start();

echo $_COOKIE[‘DATA’];
?>
[/code]

One can actually get data in php.
I am wondering how this work. Doesn’t the frontend js has to have some kind of mean like ajax to pass the data to the php backend? Thanks.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@shakazorbaApr 01.2017 — when your browser first connects to server

it sends any cookies it may have for that domain

to the server, the server response may contain

a request to set a cookie on the client it could

also contain the cookies that were sent

by the browser

this is does not depend on JavaScript
Copy linkTweet thisAlerts:
@rich1812authorApr 01.2017 — Thank you for your reply.

So the process is that document.cookie set the cookie on the browser, when the browser does to the server pass on the cookie data to php script. When php gets the cookie data, process it on the server, sent it back to the browser if necessary. Am I understand this correctly?

One thing I am not very clear is "this is does not depend on JavaScript", what does it mean?

Thanks again.
Copy linkTweet thisAlerts:
@rootApr 02.2017 — Your browser cookie can be initated during the PHP phase of the page request and accessed on the client side by calling to it or inserting to it.

FYI cookies are not really a good way of storage of information as they can be deleted by the user, expired or read by other web sites.

Use localStorage if supported to make it impossible to read by other websites as localStorage is domain specific.
×

Success!

Help @rich1812 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.8,
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,
)...