2
votes

Im trying to figure out how to use a php $_SESSION value. The situation is:

  • Main HTML site has an <iframe> that loads products.php
  • Within products.php a form submits and saves the two values below:

    $_SESSION["subtotal"] = $_POST['SUBT'];

    $_SESSION["tax"] = $_POST['TAX'];

  • These work well I can echo them no problem (within that page)

  • This page replaces products.php with confirmation.php

Once in the confirmation.php I am not able to grab the session values. I have tried the following:

<?php
session_start();

$_SESSION["subtotal"] = $_POST['SUBT'];
$_SESSION["tax"] = $_POST['TAX'];

echo $_SESSION["subtotal"];
echo $_SESSION["tax"];
echo $subtotal;
echo $tax;

?>

AND

$_SESSION["subtotal"] = $subtotal;
$_SESSION["tax"] = $tax;

echo $_SESSION["subtotal"];
echo $_SESSION["tax"];
echo $subtotal;
echo $tax;

?>

AND

$subtotal = $new_subtotal;
$tax = $new_tax;

echo $new_tax;
echo $new_subtotal;

?>

All have failed to see $_SESSION - Is the value getting lost with the <iframe> change?

I have used the following code on the products.php page to confirm the session were susecssfully created - They are:

echo $_SESSION["subtotal"]." stored in session <br />";
echo $_SESSION["tax"]." stored in session <br />";

NOTES:

  • Both confirmation & products.php are on the same server.
  • print_r($_SESSION); Returns BLANK array.
  • session_start() is at the very top of both pages
2
are both pages on the same server?Jasen
It gets a bit funky here. Main html site is on our server. 1st form page sends a token request to PayPal through our server to theirs. They send request back. Now when user enters CC info this page (products.php) shoots directly to PayPal with token. Not sure if that matters. But confirmation.php & products.php exist on the same server in the same folder. - Hope that helpsNewCodeMan
on confirmation.php do this:- <?php session_start(); echo "<pre/>";print_r($_SESSION); and check you have subtotal and tax indexes in it or not? comment rest of the code for nowAnant Kumar Singh
did you start session on product.php also?Andre Polykanine
Ok I get an empty Array()NewCodeMan

2 Answers

0
votes

Use something like this:

products.php

<?php
session_start();
$_SESSION['myValue']=$_GET['YourFormElement'];
//or
$_SESSION['myValue']=3; // You can set the value however you like.
?>

Any other PHP page:

<?php
session_start();
echo $_SESSION['myValue'];
?>

A few notes to keep in mind though: You need to call session_start() BEFORE any output, HTML, echos - even whitespace.

You can keep changing the value in the session - but it will only be able to be used after the first page - meaning if you set it in page 1, you will not be able to use it until you get to another page or refresh the page.

The setting of the variable itself can be done in one of a number of ways:

$_SESSION['myValue']=1;
$_SESSION['myValue']=$var;
$_SESSION['myValue']=$_GET['YourFormElement'];

And if you want to check if the variable is set before getting a potential error, use something like this:

if(!empty($_SESSION['myValue'])
{
    echo $_SESSION['myValue'];
}
else
{
    echo "Session not set yet.";
}
0
votes

I did that once by adding $_SESSION to url inside iframe and in file that is used in iframe I used that $_SESSION by $_GET.

I am not sure 100% but it should work as:

$session = $_SESSION['session'];

<iframe src="somePage.php?s=<?php echo $session; ?>"></iframe>

somePage.php

if(isset($_GET['s'])){
   $session = $_GET['s'];
}