Is it possible to have two scripts that both run on say a cron jobs, and pass a SESSION variable from Script1.php to Script2.php? I understand the most common usage of SESSION variables is to create the typical unique ID cookie for users accessing a site via the browser and carrying information about that user from page to page. What would happen if I set a SESSION variable in a script that was executed server side and then run another to attempt and pick up the particular variable? Would the server generate a unique ID for itself that would expire after the likely default of 1440 seconds?
My thoughts are that, I build a lot of PHP scripts for things like consuming APIs where there is multiple files that often need to run in some sort of sequence. Instead of passing information from one script to next via MySQL database updates and selects, I'd like to use the more lightweight and seemingly more convenient SESSION variable method.
Session variables are not for passing data between scripts. They are stored by the server, so the cli, which executes php scripts run by cron, would not have a session at all. Sessions are not something that makes the least bit of sense to use for passing data/state/whatever it is you are talking about between two processes. If you want IPC then you need to either use threading or another language. PHP has traditionally used a database of some sorts for sharing data between processes. There is a reason for this.
The short answer to your original question "What would happen if I set a SESSION variable in a script that was executed server side and then run another to attempt and pick up the particular variable?" Is that if the script was executed by the CLI you would get an error trying to access something that wasn't there. If you were accessing it through a script executed by Apache, for instance, you would put the data into the session belonging to the client that browsed to the URI. The next script would not be able to access that unless it was the same client.
You could set and get environment variables for cli scripts to pass data I suppose.
Related
Long time with C - new to PHP.
Please excuse me if I use the word/term in my question.
I have a function that is called multiple times within a page execution. The function's result depends on the previous sub-result held in variables. I have written the function using a global variables for those "sub-results" so they are saved and used again in the next function call.
My concern is when multiple clients hit the server requesting the same page that those "sub-result" variables would be corrupted by multiple pages being executed at the same time.
Is this a concern?
If so, can I have the "sub-result" variables local to a function that is called within page execution and pass them into the function by reference? This should keep the variable's scope limited to the current client's page execution and not affected by other clients, right?
If that is correct -- is there a better way to accomplish this?
Each PHP client (better say request) uses its own data context. This means that if 2 clients are connected at the same time and execute the same PHP code, their variables are not shared and cannot influence to each other.
As pointed by Dan Lugg in comments:
You’d need to use an external data persistence technology to “share” values across PHP processes. Any conventional database/store/file (accounting for locking) would enable you to share state. But, as mentioned, no variables (regardless of static/global modifiers) are shared between processes
I have a question about php and how it works on server side.
Im thinking about creating array of users in PHP than access it. When user access the website, my script will push the username to array. Can I access this array in another page without using the database to store the usernames?
Without database- use sessions. Add session_start() on every page you want to access your users array, and then $_SESSION["session_variable_name"] = $users_array to assign your array to session variable. Then if you want to use it, just access it like a usual variable: echo $_SESSION["session_variable_name"][0].
But using database would be much more appropriate. Session variables are accessible only on that session, so if there's new user added to the array, only client who added it will see it. Each user will have his own session, where user array may be completely different from what others will see.
What I'd do- after successful login to the system, assign username to session variable, and then if I want to perform a task for that user, say, get his email address from database, I'd make sql query, selecting email from users, where username equals to username stored in session.
No. This won't work as you described. You can either use a database (typical with PHP is MySQL but there are many other options) or a file (JSON or any of a number of other formats). But the key is understanding why it won't work:
I think you are looking at PHP on the server as one system serving many users. However, from the perspective of PHP, each user is seeing a fresh and separate instance of the PHP system. Depending on how the server is actually configured, there may or may not be multiple PHP processes running at the operating system level. But at the application level, each instance (run) of a PHP program is totally independent, with its own copy of all local (RAM) data storage - including arrays. That is NOT necessarily the case in all languages, but for practical purposes you can treat a PHP server process as if it were a separate server brought online to serve the one current user, then rebooted to serve the next user. With that setup, it should be clear that the only way for the server process serving the 2nd user to see what the 1st user did is if the process serving the 1st user stored the information in non-volatile storage - e.g., MySQL or a file on disk. The advantages of a database over a simple file are that it allows fast & easy queries of the information and hides most of the little details of cooperation between the different server processes - e.g., if there are 10 users all accessing the server at basically the same time, the individual PHP server processes don't have to worry about who can write the file "now".
In addition to the problem of each server process seeing (or not seeing) the data collected from other users, even within a single user the data is not available. Each PHP page request, including regular POST or GET and AJAX calls too, starts a fresh PHP instance (again, this is logically speaking - the server code may actually be resident and not reloaded, but it runs "fresh" each time), so with the exception of session data (as others have rightly suggested) there is no data carried over between pages even for an individual user. Session data is normally limited in size and often is just used as a reference into server-side data stored in files or a database.
first you need to prepare user details array as per below
$userDetailArr = array(
'username' => 'foobar',
'id' => 1,
'email' => 'foobar#foo.com'
);
now add session_start(); function and store above array into $_SESSION variable
$_SESSION['userdetail'] = $userDetailArr;
$_SESSION['userdetail'] you can use anywhere you want. but make sure to add session_start() function at top of page before use session variable.
I have a Symfony2 project, where at the beginning of each session, I create a folder on the server where the user can manipulate and place his files.
I want to be able to delete the user's folder, when he closes his browser
(or any other related event, maybe check for a session timeout?).
How can I achieve this?
PS: I have read somewhere that java has a sessionHandler where you can code your function.
Is there anything similar in php (Symfony2 specifically)?
First of all, you cannot recongnize if a browser is closed by HTML and PHP. You would need ajax and constant polling or some kind of thing to know the browser is still there. Possible, but a bt complicated, mainly because you might run into troubles if a browser is still there (session is valid) but has no internet connection for a few minutes (laptop, crappy wlan, whatever).
You cannot have a sessionHandler which does this for you in PHP because PHP is executed when a script is retrieved from your server. After the last line is executed, it stops. If no one ever retrieves the script again, how should it do something? There is no magic that restarts the script to check if the session is still there.
So, what to do? First of all you want to make the session visible by using database session storage or something like that. Then you need a cronjob starting a script, looking up all sessions and deciding which one is invalid by now and then does something with it (like deleting the folder). Symfony can help as it allows you to configure session management in a way that it stores sessions in the database (see here) as well as creating a task which can be executed via crontab (see here).
The logical part, which contains deciding which session is invalid and what to do with this sessions) is your part. But it shouldn't be very hard as you got the session time and value in the database.
Is there a way of storing variables in a PHP application in the session scope or the application scope...? At the same time can someone please explain the life cycle of a php program..?
Thanx...
For the application scope you can use the define function.
For the session scope you can use the $_SESSION array.
For the third question....too too broad.Anyway you can take a look to this tutorial. Just Google it.
life cycle of a php program is very short.
It's not like a desktop application constantly running in your browser, and not even a demon with persistent connection to your desktop application. It's more like a command line utility - doing it's job and exits. It runs discrete:
a browser makes a call
PHP wakes up, creates an HTML page, sends it to the browser and dies
Browser renders that HTML and shows it to the user.
User clicks a link
a browser makes a call
another PHP instance, knowing nothing of the previous call, wakes up and so on
That's why you need sessions - to save the variables between calls.
How long does the instance of a class, object, in PHP last. Is it confined to the running script or will it last for a session? If it does last for a session is this regardless of whether the PHP script has start ed the session?
A simple question but this makes a big difference to me because it will mean user data will survive as an on the server and won't need storing in the session variable. Hence affecting some fundamentals of my design.
Thanks
Colin
the question doesn't really belong to OOP but to PHP behavior in general
All PHP data is going nowhere as well as PHP script itself.
PHP scripts execution is atomic. It's not like a desktop application constantly running in your browser, and not even a daemon with persistent connection to your desktop application. It's more like a command line utility - doing it's job and exits.
That's why using external storage, like file or database is required. But of course you can save only strings there, not instances of variables or anything of the kind. Strings only.
It depends on the way PHP is configured. If PHP is configured as CGI, instances would be lost on each invocation. PHP would be invoked for each http request.
If PHP is configured as a module, then there would be multiple processes handling PHP requests. So, the instance would survive in "that particular" process. But subsequent requests might be handled by a different process.
If you need class instance to survive, you will need to serialize it and store it in DB or file.
If this information is transient (or stored somewhere else) you can store (seralize) it in session. One such example is user's full name which might be required for each http request, so it can be read from DB once and then stored in session.
Example of storing class instances in session : http://www.talkphp.com/advanced-php-programming/3407-storing-class-instance-into-session.html
It's nicely explained here :
How long does an instance variable persist? In Rails? In Java? In PHP?