How do I echo a varibale $subtotal from one page to another page in a differnet directory?

rlb1
rlb1 used Ask the Experts™
on
Experts,
I have a variable $subtotal" on my shopping cart page "/secure/mycart.php".  I would like to post the variable to all of the pages in my store in the main directory".  How do I do this?   I have looked at fwrite, fget, cookies, forms, etc.  
Thanks for your help!!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
Commented:
in 1 word: sessions: http://lu2.php.net/manual/en/book.session.php

coding example:

page 1:
<?php
  session_start();
  $_SESSION["subtotal"] = 123;
?>


page 1:
<?php
  session_start();
  print($_SESSION["subtotal"]);
?>
Commented:
Sessions should do it.
/secure/mycart.php
<?php
 
// At the very beginning of your scripts, before any content 
// is sent out to the web browser
 
session_start();
 
// <snip>
// $subtotal is assigned somewhere in your code
// <snip>
 
// To save the value (somewhere after the $subtotal value is set
$_SESSION['subtotal'] = $subtotal;
 
?>
 
/someotherpage.php
<?php
 
// At the very beginning of your script, before any content 
// is sent out to the web browser
 
session_start();
 
// Get the value saved by /secure/mycart.php
$subtotal = $_SESSION['subtotal'];
 
?>

Open in new window

Make sure you don't use the subtotal session in the checkout process as it would be a major "security" issue.

I would save all the shopping cart info in a DB and then save the ID for that in a session. Then you could just make a query to your DB passing the respective ID and then get out the subtotal (and all the other info).
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Most Valuable Expert 2011
Top Expert 2016
Commented:
Better safe than sorry...

If you choose to use sessions, you need to be aware of the risks of "register_globals" since that can inject variables into your scripts without your knowledge.

Read the man pages here:
http://us2.php.net/manual/en/security.globals.php
http://us2.php.net/manual/en/ini.core.php#ini.register-globals

You cannot change this setting at run-time.  Find out if you are at risk by following this technique:
if (ini_get('register_globals')) { /* I HAVE A BIG PROBLEM */ }

If you find that you have register_globals set to "on", you may want to adopt the advice from this post.
http://us2.php.net/manual/en/security.globals.php#89787

HTH, ~Ray

Author

Commented:
Probably not a good route to go for me...  Thanks for all of your help!!

Author

Commented:
Thanks for your help!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial