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;
}
Jotform
in main page. In theJotform
settings I have added a redirect link to the same website but page is different which is containing this second script ofPHP
. This is inserting rest of the values but not inserting referral value which is stored in aCookie
. And I'm sending user to stripe page even if connection is failed or any other error occurs only for user convenience as I also have this record inJotform
dashboard.