'$_SESSION array is empty after redirection - Wordpress

I have a WordPress page template themename/template-payment.php

    <?php
    /**
    * Template Name: payment
    */
    session_start();
    
    ?>
    <!doctype html>
    <html>
    <head>
    </head>
    <body>
    <form action="/stripe_payment/charge.php" method="post" id="payment-form">
    <!-- form fileds and submit button-->
    </form>
    </body>
    </html>
?>

I have created a page using this template. page url : https://www.sample.com/payment When I submit a form using this form the data post to /stripe_payment/charge.php file. stripe_payment folder contain in the root folder of the website.

the charge.php file:

<?php
session_start();
//do the payment things
//store data in php sessions
$_SESSION['paid_amount'] = $paid_amount;
$_SESSION['invoice'] = $invoice;
$_SESSION['payment_status'] = 1 ;
header('Location: https://'.$_SERVER['SERVER_NAME'].'/payment-result/' );
?>

after payment process I store a set of data in php sessions and redirect to a result page.

url: https://www.sample.com/payment-result

but when I try to print the session data the session array show as empty

<?php
/**
* Template Name: payment-result
*/
 
session_start();

print_r($_SESSION);// empty??

die();

?>

But if I redirect to a php file after payment I can see the session array has values.

header('Location: https://'.$_SERVER['SERVER_NAME'].'/tes.php' );//session not empty

But if I redirect to a WordPress url, the session array show as empty.

header('Location: https://'.$_SERVER['SERVER_NAME'].'/payment-result/' );// session empty

I think there should be some issue with WordPress, But could not catch the issue. Can anyone help?



Solution 1:[1]

PHP sessions relies on the cookie. Maybe WordPress or your browser deletes this cookie. You should check this from your browser.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JoelCrypto