F
F
Forman2017-12-15 06:23:46
PHP
Forman, 2017-12-15 06:23:46

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

2 answer(s)
F
Forman, 2017-12-16
@Forman

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);
?>

getcookie.php
<?php
    $cookiename = $_REQUEST["name"];
    echo "alert('" . $cookiename . " = " . $_COOKIE[$cookiename] . "');";
?>

listcookies.php:
<?php
    foreach ($_COOKIE as $name => $value) {
        echo "alert('" . $name . " = " . $value . "');";
    }
?>

testcookies.html
<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>

PS Check the performance of a specific example in Firefox. IE does not want to dynamically load scripts that have parameters in the url. But if this is even an IE limitation, then this is easily bypassed through mod_rewrite.
Source: rsdn.org/forum/web/1903262.1

S
SuperNikiforov, 2017-12-15
@SuperNikiforov

insert a picture from site1 to site2 and set the cookie

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question