0

I need to bring a PHP variable to another page, how can I do this?

The question is how do I do this.

My problem is I don't know how to do this.

2 Answers2

5

You want to use a PHP session variable.

To store it:

<?php
session_start();
$_SESSION['variableName']=1;
?>

To retrieve it:

<?php
session_start();
echo "value=". $_SESSION['variableName'];
?>
Scott
  • 3,736
  • 2
  • 26
  • 44
  • 4
    Your second body of code will fail because of missing `session_start();` – Funk Forty Niner Mar 09 '14 at 02:51
  • Oops, thanks! Edited my answer. I usually have my session logic in a common file stored on all pages, that way I can manage my session data in one place. – Scott Mar 09 '14 at 02:53
  • Try outputting the $_SESSION variable to see what's wrong, like so: `var_dump($_SESSION);` – Scott Mar 09 '14 at 03:18
  • I got it working, but I have another question now. I will post a link to that question here if you want. – user3371397 Mar 09 '14 at 03:27
0
  1. as Scott said you can use sessions,
  2. if you are talking about data from website forms -> then through _POST and _GET parameters ( easy reading -> http://www.w3schools.com/Php/php_forms.asp )
  3. you are in file 1.php , and you need a variable from file 2.php ( variable name $myvar ), "include '2.php';" or "require '2.php';" , now after you have done that you can use your variable
Ţîgan Ion
  • 176
  • 1
  • 12