简体   繁体   中英

How to include a variable when including a page (PHP)

I have developed a shopping cart that submits the order total amount as a hidden variable across pages as the user selects options throughout the checkout process. On the last page, where the user inputs his/her credit card information, I require that the user check a Terms of Use box. If the user does not select the box, an error message is displayed along with the last checkout page beneath it.

Unfortunately, if I use include(page.php) to display the checkout page, the order amount is $0.00 because it is not including the order total that I have been passing across pages. Is there a way to get the value of that variable to show up on the page? Or is there a better way to display the error message than to use echo for the message and then include to display the page beneath it? I need to be able to point out the error and still have the order total display on the page.

Thanks!

It's not recommended to pass such an important variable via user submitted data; doing this may cause users to pay less than what they're supposed to pay.

A better approach is to store this value inside a session , ie

session_start();
// update the total
$_SESSION['total'] = 123;

Then, in the checkout page:

session_start();
echo "Total: ", $_SESSION['total'];

Sessions are perpetuated with session cookies, comprising a session identifier; at each request the cookie is sent along with the request, so that the system can locate the actual data on disc (or another storage medium).

Note that session_start() must be run before any output is sent to the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM