Reputation: 11
I have a PHP script that processes a stripe payment and increments the donor number upon a successful transaction. However, the script is incrementing by two or sometimes three instead of one. Stripe transactions work. My best understanding is that the page is somehow running twice.
Donor number is saved in a .txt file.
There is no html on the page.
LAMP stack.
<?php
ini_set('session.gc_maxlifetime',5);
session_set_cookie_params(5);
session_start();
$unsuccessful = false;
$paid = isset($_SESSION['number']);
//composer requirement for stripe sdk
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("XXXXX");
// Get the payment token ID submitted by the form:
$amount = $_POST['amountInCents'];
$token = $_POST['stripeToken'];
try {
$charge = \Stripe\Charge::create([
"amount" => $amount,
"currency" => "usd",
"card" => $token,
"description" => "Business Name"
]);
} catch(Stripe_CardError $e) {
$unsuccessful = true;
// The card has been declined
}
if($unsuccessful == false){
#store current donor number in sessioncount.txt
$file = 'sessioncount.txt';
$current = (int)file_get_contents($file);
$new = $current;
if($paid === false){
$new = $current + 1;
}
$_SESSION['number'] = $new;
file_put_contents($file, $new);
header("Location:https://example.com/nextpage.php");
}
Upvotes: 1
Views: 210
Reputation: 1028
It's impossible to know what might be causing the double execution without knowing a little more about the problem. How is this script called? Is somebody pressing a submit button? If so, do you disable the submit button when the user clicks it the first time in order to only submit the request one time in the case that a user "double-clicks"? This is probably the most common cause of double processing for scripts like this. Let me know a little more about the use-case and I can help more.
EDIT: Since you have now tried disabling double submits, try this to help you debug. It may not completely solve the problem, but will help you narrow down if there are multiple requests hitting the script in parallel:
<?php
ini_set('session.gc_maxlifetime',5);
session_set_cookie_params(5);
session_start();
$unsuccessful = false;
$paid = isset($_SESSION['number']);
//composer requirement for stripe sdk
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("XXXXX");
// Get the payment token ID submitted by the form:
$amount = $_POST['amountInCents'];
$token = $_POST['stripeToken'];
if(!isset($_SESSION['processed'])) {
try {
$charge = \Stripe\Charge::create([
"amount" => $amount,
"currency" => "usd",
"card" => $token,
"description" => "Business Name"
]);
} catch(Stripe_CardError $e) {
$unsuccessful = true;
// The card has been declined
}
if($unsuccessful == false){
#store current donor number in sessioncount.txt
$file = 'sessioncount.txt';
$current = (int)file_get_contents($file);
$new = $current;
if($paid === false){
$new = $current + 1;
}
$_SESSION['number'] = $new;
file_put_contents($file, $new);
header("Location:https://example.com/nextpage.php");
$_SESSION['processed'] = true;
}
} else {
//Put some code here to help you troubleshoot, such as echoing an alert. If this code is executed, it means that something has externally triggered the execution of this script more than once.
}
If this doesn't work, it means that the requests for this file are being made in parallel, and something external to the script is causing the file to be executed multiple times.
Upvotes: 1