Yes, it does work, because PHP on Server 2 processes the script independently for each request from a client. In turn, each of the execution instances of the PHP script receive their own HTTP request from stream_context_create()
, most probably even in different TCP connections. There is no way the values for $context
get accidentally swapped between execution instances and a client receives a response originally intended for another client.
Your problem doesn't even require the existence of a »Main server«. When you're writing a PHP script, critical information about how the requests are handled by it, is hidden from you, which is why you might be confused. But this is a good thing:
If you abstract away the details about how PHP is invoked, your webserver has many (configurable) choices on how to handle requests from concurrent clients: It could e.g. use multiple threads, multiple processes, serialize all the executions, use a threadpool. You wouldn't want to rewrite your PHP code, just because your Webserver has to use threads instead of processes. If you write your PHP code well, you can use the same PHP code in different contexts, e.g. call it from the command line.
You are getting certain basic guarantees, like that no client will accidentally get a response intended for some other client.
However, if you want your »Main server« to know, that it is serving different clients, you have to inform it accordingly. This could work with passing on cookies (in your case, you pass on cookies from the »Main server« to the client, but passing on cookies from the clients to the »Main server« is missing).