Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active September 29, 2024 05:24

Revisions

  1. code-boxx revised this gist May 26, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 0-PHP-CORS-COOKIE.MD
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ## FULL TUTORIAL
    ## PHP CROSS-DOMAIN COOKIE
    https://code-boxx.com/cors-cookie-php/

    ## LICENSE
  2. code-boxx created this gist May 11, 2023.
    23 changes: 23 additions & 0 deletions 0-PHP-CORS-COOKIE.MD
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    ## FULL TUTORIAL
    https://code-boxx.com/cors-cookie-php/

    ## LICENSE
    Copyright by Code Boxx

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    28 changes: 28 additions & 0 deletions 1-fetch.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>PHP CORS Set Cookie</title>
    </head>
    <body>
    <!-- (A) DEMO BUTTON -->
    <input type="button" value="CORS Cookie" onclick="demo()">

    <script>
    function demo () {
    // (B) CORS FETCH TO SITE B
    fetch("https://site-b.com/2-handler.php", {
    mode : "cors",
    credentials : "include"
    })

    // (C) FOR DEBUGGING, CONSOLE LOG ALL RESPONSES.
    .then(res => {
    console.log(res);
    return res.text();
    })
    .then(res => console.log(res))
    .catch(err => console.log(err));
    }
    </script>
    </body>
    </html>
    30 changes: 30 additions & 0 deletions 2-handler.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    <?php
    // (A) GET REQUEST ORIGIN
    if (array_key_exists("HTTP_ORIGIN", $_SERVER)) {
    $origin = $_SERVER["HTTP_ORIGIN"];
    } else if (array_key_exists("HTTP_REFERER", $_SERVER)) {
    $origin = $_SERVER["HTTP_REFERER"];
    } else {
    $origin = $_SERVER["REMOTE_ADDR"];
    }

    // (B) CHECK ALLOWED DOMAINS
    if (!in_array(
    parse_url($origin, PHP_URL_HOST),
    ["site-a.com", "site-b.com"]
    )) {
    http_response_code(403);
    exit("$origin not allowed");
    }

    // (C) PROCEED - SET CORS COOKIE
    header("Access-Control-Allow-Origin: $origin");
    header("Access-Control-Allow-Credentials: true");
    setcookie("It", "Works", [
    "expires" => time()+3600,
    "path" => "/",
    "domain" => ".site-b.com",
    "secure" => true,
    "samesite" => "None"
    ]);
    echo "OK";
    12 changes: 12 additions & 0 deletions 3-show.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>PHP CORS Set Cookie</title>
    </head>
    <body>
    <div id="demo"></div>
    <script>
    document.getElementById("demo").innerHTML = document.cookie;
    </script>
    </body>
    </html>