0

Back again with another basic question. So I have a php file (main.php) where I am connecting to my mysql database, and am connecting to it via "include 'main.php'". I want to create some variables in this file that I can manipulate and essentially be able to work with and change their values, but be able to see their changes on any other webpage. For example, let's say I have the main.php file as mentioned above, index.php, and next.php, and main.php has a variable $test="0". I want to be able to assign $test a value in index, let's say $test=10, and be able to work with that in next.php, so if I do echo 5+$test, I want the output to be 15, not 5.

Thank you very much!

Sal
  • 1,471
  • 2
  • 15
  • 36
  • 2
    use sessions for this – Salman Zafar Jul 12 '18 at 15:51
  • Possible duplicate of [Passing a variable from one php include file to another: global vs. not](https://stackoverflow.com/questions/4675932/passing-a-variable-from-one-php-include-file-to-another-global-vs-not) – user4035 Jul 12 '18 at 15:52
  • @user4035 I don't think that's what he's asking about. He's not including multiple files in the same script, he's asking about different scripts that the user accesses. – Barmar Jul 12 '18 at 15:55
  • Set the variables in main.php to a session. This way you can change and reach them in any file, during the same session. – Thomas Scheffer Jul 12 '18 at 15:57
  • So I tried using session after I saw your comments, however, I am trying to echo it into a third page, yet it is still not showing up, however when I edit the contents on the second page and echo it, it shows up as expected. I am including session_start(); at the beginning of the php file, triple checked the spelling, to no avail. The value being printed was the one that was originally assigned in main.php – Sal Jul 12 '18 at 16:39
  • It's better if you describe what actually the variable for. So people can help you easier. – Sulung Nugroho Jul 12 '18 at 16:57
  • So up to this point, I was just using the variable arbitrarily to figure out how it works. What I want to use it for is to store the row which has the appropriate login information so that I can just use the returned array to retrieve and store the values in my database so that I don't have to perform the same query on like 8 different pages. – Sal Jul 12 '18 at 17:38
  • @Barmar Ah, yes. I see it now. It's about stateful web programming. – user4035 Jul 13 '18 at 08:58
  • @Sal You'll need to post your actual scripts if you want help fixing them. It should work across any number of scripts, as long as they all begin with `session_start()`. – Barmar Jul 13 '18 at 15:33

0 Answers0