1

Short and sweet, I need to have a variable NOT be unset after a page has finished loading. I've used a file to store the value, and I've used a MySQL table with 1 record, and updated/read from that, but I want something cleaner and simpler. Any ideas?

Some people misunderstood the question, so here's an example. At the top of my page, I would have some code such as:

$_PERMANENT['hits']+=1;
print 'Hits: '.$_PERMANENT['hits'];

Note that this works across multiple clients, so it's not $_SESSION.

DanRedux
  • 9,119
  • 6
  • 23
  • 41
  • Wish you would have made that more clear in the first place. For this, a MySQL query like 'UPDATE hit SET counter = counter + 1' is very appropriate. However, that's not a lot of information. Recommend installing an approach like Google Analytics, instead. – zanlok Apr 26 '12 at 02:42
  • It's not for hitcounts, it's for whatever. The MySQL approach I've tried, as I said, but then you have to connect to it, select a DB, run a whole query, get the output.. It's a lot of functions. I'm ideally aiming for some sort of "permament" superglobal array. – DanRedux Apr 26 '12 at 02:44

2 Answers2

1

I finally found the answer: apc_store et al

DanRedux
  • 9,119
  • 6
  • 23
  • 41
0

Use the $_SESSION, that's exactly what it's for. This either requires the user's browser has cookies on, or that you format links to maintain the session id.

At the start of your pages, use session_start() - only do this once, and it must be before content is written as it needs access to the header area.

session_register() is deprecated, so just do a $_SESSION['key'] = $value; and the next page load within that session will have access to the value via: $value = $_SESSION['key'];

zanlok
  • 1,608
  • 1
  • 16
  • 29
  • Search for perhaps a more applicable [approach](http://stackoverflow.com/questions/6848455/php-application-global-settings), there's lots of existing answers that will help. – zanlok Apr 26 '12 at 02:45