A
A
Alexander2012-05-31 01:51:46
PHP
Alexander, 2012-05-31 01:51:46

Ways to exchange data between PHP scripts?

Hello and good day to all.

Immediately I apologize for the stupid question in my opinion, but already the 3rd day of googling does not give results. The only thing that it showed is that people often encounter this problem but never find a solution (crutches do not count).

Actually, the task arose to transfer, when clicking on a link, several variables from one script to another, so that this data was not visible to an ordinary user.

The first thing that comes to mind when we hear about passing data between php scripts is get and post requests.
The option with GET is not suitable, because the data is clearly hanging in the url.
Things are much better with POST, but you can’t transfer them through a regular link, you need to imitate a form, and so on ... it seems to me that this is also not the best option.

It also came to mind to pass parameters using sessions. For example, create a service session, and if it is necessary to pass a parameter, write it to it.

Cookies also came to mind, but I can hardly imagine it, you need to constantly monitor that there is nothing superfluous left in them.

So the question is, how can this problem be solved? Are there methods in PHP specifically designed for this situation. How do you solve such a problem? Is it really impossible to do without crutches (js, ajax, cookies, sessions, etc.).

Thank you for your responses!

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Alexander, 2012-05-31
@0lympian

The question is not quite correct, tk. There can be 2 interpretations:
1. If we are talking about web scripts that are executed sequentially, then why do you not like the session? In this case, this is not a crutch - it is precisely for this purpose that it was invented. If you don’t like the standard implementation directly, then you can take your own (fundamentally similar): generate a client (session) ID and pass it via get/post/cookie (to taste), and store the data itself either in files or in a database, and receive using this identifier as an association key.
It is naturally not worth transmitting data directly through cookies, get and post in this case: these things can always be faked, because. they go through the client. It can be called a crutch :)
================
2. If we are talking about 2 shell scripts running simultaneously, i.e. inter-process communication (IPC), then you can use different more or less system-dependent options. From standard IPC mechanisms: semaphores, messages, shared memory block, to specific things like a named pipe or socket; or things similar to item 1 (database / files).

V
Vladimir Chernyshev, 2012-05-31
@VolCh

cookies are not a crutch, in fact, they were invented for this (session is a further development of the idea)
And you have a contradiction in the conditions, in my opinion:
The option with GET is not suitable, because the data is clearly hanging in the url, it contradicts , but you can’t transfer them through a regular link . Either the data hangs in the URL, or it cannot be transmitted through a regular link (without additional scripts on the client side). There is no third.
It may suit the option:
1. On the initial page, a regular GET link to an intermediate page with data in the URL, click
2. The server receives the data, writes it to the session and redirects to the target page without data in the URL
3. The target page retrieves the data from the session and immediately clears the session.
If the user does not look where the link leads (or his browser does not show), then the data in the URL will flash in the address bar for a very short time on a modern unloaded computer and a fast Internet.
Again, if the task is to hide data from the user completely, then this is not realistic, now all popular browsers can monitor traffic. You can complicate life by encrypting data, but only complicate it. And to protect against accidental data leakage according to a scenario like “the hacker saw the secret url, remembered it and entered it at home” my scenario, IMHO, is enough.

S
shvedovka, 2012-05-31
@shvedovka

Also a crutch, but without js, ajax, cookies, sessions, etc., as you asked.
The script calls another script by URL in any way convenient for you, pass a parameter in at least GET which will not be visible to the user anyway.
The simplest option (if allow_url_fopen is enabled):
$answer = file_get_contents('http://www.example.com/script2.php?param1=A¶m2=B');
If you wish, you can even return something and use it.

O
Oleg Karnaukhov, 2012-05-31
@BupycNet

If inside the server from one soft to another, then there are several options
1. files (we write, open another, read, use)
2. database (same)
3. session (almost the same, only even easier)
4. memcached (almost the same , good choice)

A
avorobiev, 2012-05-31
@avorobiev

Hmm, you yourself have listed all the options. Others have not yet been invented. Just write down the pros and cons of each implementation and pick the best one.
For me, this is how the link should perform a GET request, respectively, all the data in the URL. Redirecting to hide something is nonsense.
The button can perform a post-request.
If you really, really need a link to make a POST request, use javascript.
Both GET and POST are transparent to monitoring. To prevent the parameters from being abused, use validation on the server side. Cut off invalid requests.
If you need to store state between pages, so that the user does not even know what you store for him there, use sessions, passing only the session identifier to cookies, and not the stored values ​​themselves.

V
Vladimir Luchaninov, 2012-07-06
@how

the simplest is $_SESSION
if you want it more difficult, then a unique id is created for each user, and somewhere on the server (MySQL, membase, /tmp/,...) the uid => data hash is stored

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question