PHP-CPP extension which retains value of a variable across page acess

时间:2017-06-15 09:26:10

标签: php c++ linked-list

I implemented a php-cpp extension following the documentation at [enter link description here][1].

The CPP code is just one file which has a global variable(root) of type NODE(of a linked list). There are 2 functions :

  1. To add a node to the list referenced by root.
  2. To view the elements in the list which are printed using Php::out.

My index.php contains an input box with button to add a number. When add button is pressed the value in the add box is posted to a php file which calls the add function in the CPP extension. Another button is view which display the nodes in the list, on the same page.

What I expect:

I expect that the root should retain its value across page views. i.e. if I load my index.php and add a number to the list and then try to view the list form another page, it should give me the inserted element from the first page. This should happen from any no of pages, or browsers or machines I try to load the page from. Any addition made from any source should be visible on all pages.

What is happening:

  1. Sometimes when I open a new tab and view I can see the previous values sometimes empty list is printed.
  2. If I open a new browser then definitely the previous values are lost.
  3. If a tab is loaded and it shows empty list and I enter a value from this page, then all the previously opened tabs loose their previous value and now show this value.
  4. If I leave the tab opened for a long time and then view then some tabs show empty values and some show some random values(if one shows a value then all tab will show that value).

Please suggest what should I do so that the root element always points to the same list, and that the data in the list does not get lost, across multiple pageviews.

1 个答案:

答案 0 :(得分:0)

Most likely your php script is interpreted on server by launching one or more shot-lived php interpreter processes. Each of these processes will have it's own global variable. When process dies the content of it's global variable is lost.

If you want to retain a value of variable for extended period of time then you should save it in Data Base of some kind. There are plenty of php tools to work with SQl and NO-SQL DBs.