How to set or read cookies from other my website?


- The # 1 site that accepts a get parameter "g" written in it value (eg. ?g=1). Then he runs with it.
Site No. 2, which needs to push/overwrite the get-parameter "g" with its value to 1.
- The value of get-parameter "g" - public and carries no special value except to the owner of the website.
- Both sites located on different domains belong to the same owner, and are located on the same server.
- The user who visits the site No. 2 with a certain get parameter and then goes to site # 1 without it.

Goal: we Need to make when entering the site No. 1 cookies site No. 1 already had data get parameter, which the user went to site # 2.

Planning to solve the problem using cookies. But as I googled, assign a cookie to another domain or to read cookies from another domain - it is impossible. Is it possible to write some code that takes a get parameter from site # 2 and writes it to a cookie site No. 1? Let me remind you that this is not authorization data, and their safety is not important.

Thanks in advance for any help!
June 10th 19 at 14:49
2 answers
June 10th 19 at 14:51
Security reasons you cannot read/write cookies from a different domain (the subdomain one domain and leave the domain property for the independent study). But, as in every rule there is an exception. Cross-domain cookies can be done if both domains are yours. By "your" I mean you can place the scripts. It is easy to organize in the following way. Write a script that reads the cookie name from the request and outputs the value in response, another script that sets the cookie. Now on a different domain we can safely placed where the html by using the script can pull the scripts from another domain that will read/write cookies. The Html will be in the same domain and cookies to pull, we are going to another domain.

Here is a simple example:

 setcookie($_REQUEST["name"], $_REQUEST["value"], time() + 36000);

 $is cookiename = $_REQUEST["name"];
 echo "alert('" . $is cookiename . "= " . $_COOKIE[$is cookiename] . "');";

 foreach ($_COOKIE as $name =--> $value) {
 echo "alert('" . $name . "= " . $value . "');";


<script type="text/javascript">
listCookies function() {
 exec(document.getElementById('scriptsLocation').value + 'listcookies.php?rnd=' + 
function setCookie() {
 exec(document.getElementById('scriptsLocation').value + 'setcookie.php?name=' + 
 document.getElementById('is cookiename').value + '&value=' + 
 document.getElementById('cookieValue').value + '&rnd=' + Math.random());
function getCookie() {
 exec(document.getElementById('scriptsLocation').value + 'getcookie.php?name=' + 
 document.getElementById('is 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;

location scripts on other domain: <input type="text" id="scriptsLocation" value="http://your.other.domain/scripts/location/"><br>
cookie name: <input type="text" id="is 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()">

PS to Check the efficiency of a specific example in Firefox. IE does not want to dynamically load scripts whose url has parameters. But even if it's a limitation of IE, it can be easily bypassed using mod_rewrite.

June 10th 19 at 14:53
insert picture with сайта1 on сайт2 and cook prescribe
Things don't work out. Can be a little more on how to do this? Preferably with an example :) - holly99 commented on June 10th 19 at 14:56
well , to cook in the browser of the user to record you just need to request the server sent. well, that and send the request on at least ten sites of any website. - michele_Reinger commented on June 10th 19 at 14:59
not necessarily even a picture, you can even js to load on one site with another. And js, may write and read cookies. - Roderick92 commented on June 10th 19 at 15:02
well I for example about the picture told. when the counters did 1x1 pixel. now of course everything in js - michele_Reinger commented on June 10th 19 at 15:05
, ,
1 posted by js-script, reaching from the url get parameter and sets it in a cookie.
Hooked up this script to site # 2. At the entrance to site # 2, the cookie is set for domain number 2. And I need them to be installed on the domain number 1.

How to make cookie set for the domain No. 1?

As I understand it, I need them to transfer/assign the site # 1 in the boot time script, but I don't know how to do it. - holly99 commented on June 10th 19 at 15:08
cookies must be installed on the server, not through js - michele_Reinger commented on June 10th 19 at 15:11
but then how to load them, if it will be php?!
Show an example? It's worth a thousand words. Tried to Google, but found nothing similar. - holly99 commented on June 10th 19 at 15:14
, - michele_Reinger commented on June 10th 19 at 15:17
I assumed that this site was created in order to help each other in the solution of any specific problems. But apparently, You have different views, if You instead of a couple lines of code that will send the questioner on the right path, refer to the 600-page manuscript.

My knowledge in this area is very modest, this is not my area of expertise. I am grateful to You that You are so eager to help educate, but I just needed to get insights into solving a small problem. Sorry to hear that You were able to help me with this only a reference to the Bible. But thanks anyway. - holly99 commented on June 10th 19 at 15:20
Yes there are 3 pages about a cookie with examples. - michele_Reinger commented on June 10th 19 at 15:23
Thank you. So not understood You correctly. I looked at them, but I know how to assign cookies in php. The problem is, I don't know how to pass the values (the cookie or get-parameter) from one site to another. Why no vision of a solution.

As I see it now:
1 is js script or image that is loaded on the site # 2.
1 have a php script that sets a cookie on the # 1 site?
How are we supposed to load this php script from the site No. 2?

So the user visits the site No. 2, received the cookies No. 1. - holly99 commented on June 10th 19 at 15:26
Found here some solution:
Then realized that I do not fit the solution with the purposes of the cookies. My problem is solved a little differently and probably easier (sending get-request from one site to another at the time of treatment). Will just have to see how this is possible, except the way with redirecting ( like this: ). Thank you all for participating. - holly99 commented on June 10th 19 at 15:29
I thought it was a mistake of the hammer. - michele_Reinger commented on June 10th 19 at 15:32
Not quite. This method had to solve the problem (and if you delve probably decide), just revealed details that further the script assigns some cook, some of which are dynamic (like a counter). In the end, I decided that it is easier and safer to pass a get parameter. As its transmission involves the script, and he correctly prescribe the right cookies. :) - holly99 commented on June 10th 19 at 15:35

Find more questions by tags PHPjQueryWordPressHTTP CookiesJavaScript