Best way to store variable - php

I need to save the value of a variable to use on another page. What is the best way to do this?
$ _SESSION is not a possibility because it will be executed by cronjob.
apc_store seemed to me an excellent function for this but it does not seem to work anymore or needs to install extension on server to work.
Is there any native PHP alternative?
Edit:
I use Curl to get the value in a site1 and saved in a variable then it is redirected to Site2 where I want to retrieve the value of that variable. Using POST is not possible because it is a form and would conflict the data that the form needs with the data I am passing. The application does not use DB, creating one so this would be bad.
Cookies falls into the same SESSION problem (I'm using cronjob)
I think the best solution would be a txt file manipulated by file_put_contents.
Would that be the only solution?

apc_store seemed to me an excellent function for this but it does not seem to work anymore or needs to install extension on server to work.
The APC variable cache is not suitable for this application -- it isn't shared between the web server and CLI scripts, and values stored in the cache may be evicted at any time.
If you need a value to be reliably stored across executions of a CLI script, write it to a file or store it in a database.

Related

Does PHP have inside cache?

I want save the editor content for 30s, and upload the content to server. If the browser have some problem or it closed. user can restore the content next time they login.
But I think, if the content save to the SQL, not good. I wondered if PHP has a cache function. Like memcache. The content is put in the cache, if the server restart, the cache can clear. and do not need write in SQL.
so, does PHP have a cache mechanism? Or can you give me a solution handle the content, must run quickly and the server must not require pressure.
The client side has nothing to do with PHP, by the time the user receives the output that is the page, all of the PHP has been transformed. You could make this PHP's business, by means of pushing your cache data up to the server using AJAX. So, you need two solutions: one to manage data on the client side (say, using Javascript) and one to manage data from the client side on the server side (with PHP).
This is if you still want to use PHP, when you more than likely don't need to. Anyway, PHP does not have a built-in cache implementation that I am aware of, other than perhaps using $_SESSION, you'll need to roll your own.
there's no standart "inside" cache in php. You'll have to use either your own system, html5's localStorage or memcached/any other plugin. Memcached is definetely recommended though.
PHP does have inherent cache implementation. It's called APC(alternative PHP cache). It should be turned on in php.ini.
APC is of course server-side technology and has nothing to do with browser and javascript technologies. It's used for same reason memcached is used(and few others) - to give SQL Server some slack.

PHP In Memory Classes or Variable

Hi is there anyway to store variable into memory until server restart or manually recreate the same variable using PHP.
Actually the problem is this I nee do have a router in my application which have several configuration to route the URLs on different APIs. I do not want to load my configuration each time a request lands so I am searching a way to keep the configuration data into memory until I manually recreate the same.
Please suggest if there is anyway to do this.
Thanks in Advanced.
PHP stores all variables in memory until the process exits (if the variable is a global) or until the scope ends (if the variable is local).
I think where you're getting confused is that Apache (or whatever webserver you're using) runs continuously, but it only starts PHP for every request, so PHP is never running continuously.
To store things across different requests, you'll need to use external persistence. You can do this with PHP sessions, a database, flat files, etc. but none of these will store the data in memory.
Even though it's external, you can use a caching server like memcached to store data in memory continuously, and then use PHP to access it through the Memcached class.

Apache Module development: PHP SESSION access

I am trying to develop an Apache 2 module that, among other functions, should intercept certain POST variables, and then establish a PHP session underneath, setting all the relevant session variables.
One option I've considered was using cookie libraries to set the PHPSESSID in the request, and then insert all these values in the $_SESSION associative array.
Problem is I can't seem to find an API for the PHP, and my suspicions that these values actually lie underneath, in the Apache server itself, are currently unfounded.
Does anyone know if this is even possible? Or even if I am overlooking a simple workaround that would work?
I eventually decided to write a PHP extension, alongside another handler on my Apache Module, since I prefer to parse some POST vars there.
The module will "chew" the data that needs to be saved to the session, and pass it off as new POST variables, after criptografically validating the data. The PHP extension will then start a new session, and set each item as a Session variable.
This assumes my module will run before the PHP module itself. In case that doesn't happen, I will have to handle the validation on PHP, or try to force the handler to run before PHP somehow.

Custom PHP cache

I have an application that is in need of caching large amounts of data (sometimes even MBs) over multiple page request (for the same user/session). After doing some Googling etc. I've concluded that it is likely best to implement the caching mechanism by writing cache files to disk (please correct me if you think there are better alternatives).
Now, my idea was to have a root cache folder, within which I create folders for each session ID to not overwrite any cached data used in separate sessions. Then for each block of data I will create an unique identifier which can be linked to the data whenever I want to retrieve it again. The data will then be serialized to a string format (using the default PHP 'serialize' function) after which it is written to the appropriate file.
The thing I'm not so sure on how to implement is the clean up of the cached files. At some point either the data is not needed anymore, for example when the session has expired or a number of other reasons. Since it will likely be too much overhead to check for this during each page request, I expect to have to do this externally using some kind of scheduler. However, I cannot guarantee that my application will run on a UNIX environment, so I'd have to consider other platforms as well (Windows, Mac). Is there a general solution that anyone can think of that would be cross-platform without too much hassle?
I'm also thinking that there maybe is a way to intelligently check or mark certain files to be cleaned up, without have to check all the existing files separately. I was considering maybe storing their last accessed timestamp or something, but there may be other criteria besides time that could make the cached data obsolete, such as an exception being triggered in the application (though I could say that whenever that happens the entire cache for that sessions will be emptied or something like that).
Any suggestions on these issues would be very much appreciated!
If you got MemCache installed, you can use that for caching. It is faster that file cache, and you can give it an expiration time, so it will automatically be removed from the cache after a given period of time.
Both Windows and Unix have scheduled job support - cron for Unix/Linux, and 'at' for Windows. It would be a simple matter to whip up a PHP script to scan your cache directory and apply your deletion criteria to what it finds. Last access timestamp is trivial, basing it on cached file contents or other triggers slightly less so.

Is there a PHP/MySQL database admin that doesn't use sessions?

I am doing development work on a site with a strange server set up where sessions basically don't work. It's kind of a long story, but the main crux is it's a cluster of servers that are syncronized from an FTP server every few minutes. And for example, anything written to the filesystem in PHP gets deleted within 5 minutes.
So this means sessions don't work and I get some strange problems in phpMyAdmin, like it forgetting which page of a table I was on - I click 'next page' and end up back at the start again.
I've also tried SQL Buddy and am getting similar problems.
Are there any equivalents that don't use sessions? Doesn't need to be as full-featured as PMA, it's mainly for adding/editing some stuff.
There's always the MySQL GUI Tools.
You can make phpmyadmin use other authentication methods:
http://www.phpmyadmin.net/documentation/#authentication_modes
Depends how much security you need and how restricted you are, but 'config' authentication mode with a custom .htaccess sounds like it might work for you.
I don't know how hard it would be to plug this into phpMyAdmin, but PHP has a functionnality that allows sessions to be stored in another way than using files.
In your case, you already have a database server, obviously, so maybe you could create a "technical" database, and use it to store sessions ? This way, you would still be able use phpMyAdmin (which is quite a good tool), but your problem should be solved.
The PHP function you need to know to do that is session_set_save_handler :
session_set_save_handler() sets the
user-level session storage functions
which are used for storing and
retrieving data associated with a
session.
This is most useful when a
storage method other than those
supplied by PHP sessions is preferred.
i.e. Storing the session data in a
local database.
There are a couple of examples (take a look at the comments at the bottom of the page : some might be helpful)
For instance, Drupal uses this solution to store sessions into DB instead of files, by default.
Another solution would be to use memcached to store your sessions -- of course, if you don't have a memcached server at your disposal, this might be a bit harder than storing them in DB ^^
Or, of course, if you have access to your DB server via the network, you could install phpMyAdmin on your local computer, or use a tool like MySQL GUI Tools and its MySQL Query Browser.
I found a neat solution! SQLBuddy has a feature where you can put the password in the config file and it will use it automatically without a need to log in.
Obviously this is insecure by default, but coupled with a .htaccess and .htpasswd (which does work on the server) I've now got a secure login.

Categories