Answer the question
In order to leave comments, you need to log in
How do I assign or read cookies from my other site?
Hello!
There are:
- Site #1, which accepts the get-parameter "g" with the value written into it (eg ?g=1). Then he works with him.
- Site #2, which should pass/overwrite the get-parameter "g" with its value to site #1.
- The value of the "g" get-parameter is public and has no special value, except for the owner of the site.
- Both sites are located on different domains, belong to the same owner and can be located on the same server.
- A user who visits site #2 with a certain get-parameter, and then visits site #1 without it.
Purpose: It is necessary that when visiting site No. 1, the cookies of site No. 1 already contain the data of the get-parameter with which the user entered site No. 2.
I plan to solve the problem using cookies. But as I googled, assigning cookies to another domain, as well as reading cookies from another domain, is impossible. Is it possible to write some code that takes a get parameter from site #2 and writes it to site #1's cookie? Let me remind you that these are not authorization data and their security is not important.
Thanks in advance for any help!
Answer the question
In order to leave comments, you need to log in
For security reasons, you cannot read / write cookies from another domain (I leave the issue of subdomains of one domain and the domain property for independent study). But, as with any rule, there is an exception. Cross-domain cookies can be made if both domains are yours. By "your" I mean that you yourself can place the scripts you need on them. This can be organized as follows. We write a script that reads the name of the cookie from the request and issues a value in the response, another script that sets the cookie. Now, on another domain, we can easily place html where, using script elements, we can pull scripts from another domain that will read / write cookies. Html will lie in one domain, and we will pull cookies for another domain.
Here is the simplest example:
setcookie.php:
<?php
setcookie($_REQUEST["name"], $_REQUEST["value"], time() + 36000);
?>
<?php
$cookiename = $_REQUEST["name"];
echo "alert('" . $cookiename . " = " . $_COOKIE[$cookiename] . "');";
?>
<?php
foreach ($_COOKIE as $name => $value) {
echo "alert('" . $name . " = " . $value . "');";
}
?>
<html>
<head>
<script type="text/javascript">
function listCookies() {
exec(document.getElementById('scriptsLocation').value + 'listcookies.php?rnd=' +
Math.random());
}
function setCookie() {
exec(document.getElementById('scriptsLocation').value + 'setcookie.php?name=' +
document.getElementById('cookieName').value + '&value=' +
document.getElementById('cookieValue').value + '&rnd=' + Math.random());
}
function getCookie() {
exec(document.getElementById('scriptsLocation').value + 'getcookie.php?name=' +
document.getElementById('cookieName').value + '&rnd=' + Math.random());
}
function exec(sUrl) {
var head = document.getElementsByTagName('head').item(0);
var script = document.createElement('script');
script.src = sUrl;
script.type = 'text/javascript';
script.defer = true;
void(head.appendChild(script));
}
</script>
</head>
<body>
scripts location on other domain: <input type="text" id="scriptsLocation" value="http://your.other.domain/scripts/location/" /><br />
cookie name: <input type="text" id="cookieName" /><br />
cookie value: <input type="text" id="cookieValue" /><br />
<input type="button" value="Set cookie" onclick="setCookie()" />
<input type="button" value="Get cookie" onclick="getCookie()" />
<input type="button" value="List cookies" onclick="listCookies()" />
</body>
</html>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question