phpcookiessetcookie

Unable to get Cookie value when Jotform redirects user to another page


I have a condition that when a user shares his referral link then it sets a Cookie in the browser so that if GET value is removed from URL even then I can get referral.

I have checked in developers tool that Cookie is available across all web pages of my website. But the issue is when Jotform redirects the user to that page which contains PHP script to store the record in database along with referral value then it doesn't store referral value.

I have tried to add delay on the page but in vain.

I also checked the Cookie value by directly opening that page in browser which contains the PHP script.

Please tell me how can I fix this issue?

Here is the code inside header file. I have wrote this code before <html>:

<?php
if (isset($_GET['ref'])) {
  $cookie_name = "referral";
  $cookie_value = $_GET['ref'];

  setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", ".candidateside.com", true);
}
?>

And this is the code which is being executed to insert record in database:

<?php
include_once "error_handler.php";
require_once "connection.php";

sleep(3);

if ($mysqli->connect_error) {
    error_log("Connection failed: " . $mysqli->connect_error);

    // echo "<script>window.location.replace('https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');</script>";
    header('Location: https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');
    exit;
}

if (isset($_POST['submission_id'])) {
    echo '<pre>';
    print_r($_POST, 1);
    echo '</pre>';

    $sid = $mysqli->real_escape_string($_POST['submission_id']);
    $formID = $mysqli->real_escape_string($_POST['formID']);
    $name = $mysqli->real_escape_string(implode(" ", $_POST['name']));
    $haveYou = $mysqli->real_escape_string($_POST['haveyou']);
    $email = $mysqli->real_escape_string($_POST['email']);
    $phoneNumber = $_POST['phonenumber']['full'];
    $veteranStatus = $mysqli->real_escape_string($_POST['veteranstatus']);
    $ethnicity = $mysqli->real_escape_string($_POST['ethnicity']);
    $dateOfBirth = $_POST['dateof']['year'] . "-" . $_POST['dateof']['month'] . "-" . $_POST['dateof']['day'];
    $howLong = $mysqli->real_escape_string($_POST['howlong']);
    $areYou131 = $mysqli->real_escape_string($_POST['areyou131']);
    $areYou132 = $mysqli->real_escape_string($_POST['areyou132']);
    $willYou = $mysqli->real_escape_string($_POST['willyou']);
    $areYou = $mysqli->real_escape_string($_POST['areyou']);
    $haveYou136 = $mysqli->real_escape_string($_POST['haveyou136']);
    $typeA = $mysqli->real_escape_string($_POST['typea']);

    // Retrieve the referral from the cookie
    $referral = isset($_COOKIE['referral']) ? $_COOKIE['referral'] : null;

    if (is_null($referral)) {
        error_log("Referral cookie not set. Details: 
        IP: " . $_SERVER['REMOTE_ADDR'] . ", 
        URL: " . $_SERVER['REQUEST_URI'] . ", 
        User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . " at " . date('Y-m-d H:i:s'), 0);
    }

    $db_table = "combined_jotform";

    $result = $mysqli->query("SELECT * FROM $db_table WHERE submission_id = '$sid'");

    if ($result->num_rows > 0) {
        /* UPDATE query */
        $result = $mysqli->query("UPDATE $db_table SET name = '$name', haveYou = '$haveYou', email = '$email', phoneNumber = '$phoneNumber', veteranStatus = '$veteranStatus', ethnicity = '$ethnicity', dateOfBirth = '$dateOfBirth', howLong = '$howLong', areYou131 = '$areYou131', areYou132 = '$areYou132', willYou = '$willYou', areYou = '$areYou', haveYou136 = '$haveYou136', typeA = '$typeA', referral = '$referral' WHERE submission_id = '$sid'");
    } else {
        /* INSERT query */
        $result = $mysqli->query("INSERT IGNORE INTO $db_table (submission_id, formID, name, haveYou, email, phoneNumber, veteranStatus, ethnicity, dateOfBirth, howLong, areYou131, areYou132, willYou, areYou, haveYou136, typeA, referral) VALUES ('$sid', '$formID', '$name', '$haveYou', '$email', '$phoneNumber', '$veteranStatus', '$ethnicity', '$dateOfBirth', '$howLong', '$areYou131', '$areYou132', '$willYou', '$areYou', '$haveYou136',  '$typeA', '$referral')");
    }

    $mysqli->close();

    // Set the cookie expiration to the past to delete it
    setcookie("referral", "", time() - 3600, "/", ".candidateside.com", true);

    // echo "<script>window.location.replace('https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');</script>";
    header('Location: https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');
    exit;
} else {
    // echo "<script>window.location.replace('https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');</script>";
    // header('Location: https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');
    exit;
}

Solution

  • After searching a lot and debugging my code, I figured it out that I need to add html in my php script file so that browser can handle it as a web page. Because cookies will only load when a web page will load in the browser. So, I made following changes:

    1. Header file:
    <?php
    if (isset($_GET['ref'])) {
      $cookie_name = "referral";
      $cookie_value = $_GET['ref'];
    
      setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", ".candidateside.com", true, true);
    }
    ?>
    
    1. Script file:
    <!doctype html>
    <html lang="en">
    
    <head>
        <title>Processing</title>
        <!-- Required meta tags -->
        <meta charset="utf-8" />
        <meta
            name="viewport"
            content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    
        <!-- Bootstrap CSS v5.3.2 -->
        <link
            href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
            rel="stylesheet"
            integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
            crossorigin="anonymous" />
    </head>
    
    <body>
    
        <h2 class="text-secondary p-3">Processing your request...</h2>
    
        <?php
        include_once "error_handler.php";
        require_once "connection.php";
    
        sleep(2);
    
        if ($mysqli->connect_error) {
            error_log("Connection failed: " . $mysqli->connect_error);
    
            // echo "<script>window.location.replace('https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');</script>";
            header('Location: https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');
            exit;
        }
    
        if (isset($_POST['submission_id'])) {
            // echo '<pre>';
            // print_r($_POST, 1);
            // echo '</pre>';
    
            $sid = $mysqli->real_escape_string($_POST['submission_id']);
            $formID = $mysqli->real_escape_string($_POST['formID']);
            $name = $mysqli->real_escape_string(implode(" ", $_POST['name']));
            $haveYou = $mysqli->real_escape_string($_POST['haveyou']);
            $email = $mysqli->real_escape_string($_POST['email']);
            $phoneNumber = $_POST['phonenumber']['full'];
            $veteranStatus = $mysqli->real_escape_string($_POST['veteranstatus']);
            $ethnicity = $mysqli->real_escape_string($_POST['ethnicity']);
            $dateOfBirth = $_POST['dateof']['year'] . "-" . $_POST['dateof']['month'] . "-" . $_POST['dateof']['day'];
            $howLong = $mysqli->real_escape_string($_POST['howlong']);
            $areYou131 = $mysqli->real_escape_string($_POST['areyou131']);
            $areYou132 = $mysqli->real_escape_string($_POST['areyou132']);
            $willYou = $mysqli->real_escape_string($_POST['willyou']);
            $areYou = $mysqli->real_escape_string($_POST['areyou']);
            $haveYou136 = $mysqli->real_escape_string($_POST['haveyou136']);
            $typeA = $mysqli->real_escape_string($_POST['typea']);
    
            // Retrieve the referral from the cookie
            $referral = null;
    
            $attempts = 0;
            while (!isset($_COOKIE['referral']) && $attempts < 5) {
                sleep(1);
                $attempts++;
            }
    
            $referral = isset($_COOKIE['referral']) ? $_COOKIE['referral'] : null;
    
            if (is_null($referral)) {
                error_log("Referral cookie not set. Details: 
                        IP: " . $_SERVER['REMOTE_ADDR'] . ", 
                        URL: " . $_SERVER['REQUEST_URI'] . ", 
                        User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . " at " . date('Y-m-d H:i:s'), 0);
            }
    
            $db_table = "combined_jotform";
    
            $result = $mysqli->query("SELECT * FROM $db_table WHERE submission_id = '$sid'");
    
            if ($result->num_rows > 0) {
                /* UPDATE query */
                $result = $mysqli->query("UPDATE $db_table SET name = '$name', haveYou = '$haveYou', email = '$email', phoneNumber = '$phoneNumber', veteranStatus = '$veteranStatus', ethnicity = '$ethnicity', dateOfBirth = '$dateOfBirth', howLong = '$howLong', areYou131 = '$areYou131', areYou132 = '$areYou132', willYou = '$willYou', areYou = '$areYou', haveYou136 = '$haveYou136', typeA = '$typeA', referral = '$referral' WHERE submission_id = '$sid'");
            } else {
                /* INSERT query */
                $result = $mysqli->query("INSERT IGNORE INTO $db_table (submission_id, formID, name, haveYou, email, phoneNumber, veteranStatus, ethnicity, dateOfBirth, howLong, areYou131, areYou132, willYou, areYou, haveYou136, typeA, referral) VALUES ('$sid', '$formID', '$name', '$haveYou', '$email', '$phoneNumber', '$veteranStatus', '$ethnicity', '$dateOfBirth', '$howLong', '$areYou131', '$areYou132', '$willYou', '$areYou', '$haveYou136',  '$typeA', '$referral')");
            }
    
            $mysqli->close();
    
            // Set the cookie expiration to the past to delete it
            setcookie("referral", "", time() - 3600, "/", ".candidateside.com", true, true);
    
            // echo "<script>window.location.replace('https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');</script>";
            header('Location: https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');
            exit;
        } else {
            // echo "<script>window.location.replace('https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');</script>";
            header('Location: https://buy.stripe.com/dR65lq5Rz8Xm704bIJ');
            exit;
        }
        ?>
    
        <!-- Bootstrap JavaScript Libraries -->
        <script
            src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js"
            integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
            crossorigin="anonymous"></script>
    
        <script
            src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"
            integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+"
            crossorigin="anonymous"></script>
    </body>
    
    </html>