Is PHP restrict transfer data between files?


Is PHP restrict transfer data between files?



In one file I make query and get result.



index.php (outputs the links to browser)


foreach ($conn->query($sql) as $info) {
$output_html = '<a href = addsearch.php?id='.$info['id'].' target=_blank >'.$title_.'</a>';
print("$output_html<br>");
}
$_SESSION['info'] = $info; // now it's works such way



In other file I wish I would get the copy of result without using GET, POST, SESSION methods. (I no need in GETPOST as data stay on server in nearest RAM area. Also wouldn't want to use the SESSION variable as it use HDD.)



addsearch.php (launch only when user click on the link)


session_start();
print_r($_SESSION['info']); // works now
...



Is there another methods to get data? Any global RAM variables or cache, common shared resource between files.



I tried first example from PHP manual:


<?php
$a = 1;
include 'b.inc';
?>



but it doesn't work :-) because I launch files separately, so they have different processes.





global keyword use before declaring variable as mentioned in link you posted.
– Lovepreet Singh
Jun 29 at 10:05


global





Why is using the session (and HDD) an issue ?
– AymDev
Jun 29 at 10:06





"Also wouldn't want to use the SESSION variable as it use HDD."...how much data are you putting in there? Is that really something to worry about? I've never heard anyone get concerned about this before. Storage is cheap, session variables are rarely more than a few KB per user in any case. You also have the option to re-configure your session to be stored in RAM, or in a database, if you prefer.
– ADyson
Jun 29 at 10:11






Anyway the web is inherently stateless, it's normal to run the query again on each request, unless the query is something very slow and large, in which case yes consider caching it somehow - on disk, in RAM (be careful if you have lots of users doing this simultaneously), in a data store of some sort. It's very much a design choice you can make for yourself based on the exact circumstances and requirements.
– ADyson
Jun 29 at 10:14





@LovepreetSingh Do You mean: index.php: <?php $variable = 'TEST'; ?> addsearch.php: <?php global $variable; echo "var=".$variable; ?> it doesn't work. May be You write correct example? ?>
– user3079758
Jun 29 at 10:34




1 Answer
1



PHP isn't restricting anything, it doesn't know about that data in the first place. There are two parts to understanding this:



The first is a design decision of PHP: every request receives a completely new environment, so data isn't held in memory between HTTP requests from users. This makes the language much more predictable, because actions on one page have very few side-effects on another.



The second, however, is more fundamental: even if PHP stored data between requests, it would be storing them in one pot for every user that accessed your site. That's because HTTP doesn't have any native tracking of "state", so two requests from the same user looks fundamentally the same as two requests from different users.



That's where cookies and sessions come in: you send a cookie to the user's browser with an ID, and you tie some data to that ID, stored somewhere on your server. That somewhere doesn't need to be on disk - it could be in a memory store like memcache or Redis, in a database, etc - but because of PHP's "shared nothing" model, it can't just be in a PHP variable.



Another relevant concept is caching: storing (again, on disk, in a memory store, etc) the results of slow computations, so that when asked to do the same computation again you can just look up the answer. Whereas a session is good for remembering what the customer puts in their shopping cart, caching is good for displaying the same set of search results to every customer that enters the same search.





@Adyson: Sure I have few KB per A LOT users and also query is very slow and large. Can You give me simple example how re-configure session to be stored in RAM?
– user3079758
Jun 29 at 10:37






to IMSoP: Yes I agree about one pot concept. And SESSION method suitable for me if I change its location to memory. Thanks for detailed explanation.
– user3079758
Jun 29 at 10:43






@user3079758 It's not about "storing the session in RAM", it's about "using a data store which happens to be in RAM". Memcache, as its name suggests, is such a data store, but you're not saying "hey PHP put this data in RAM", you're saying "hey PHP put this data in memcache". Search for "php session handler" and you'll find all sorts of different places you can store your session data.
– IMSoP
Jun 29 at 10:51






Please anyone give me the link to practical example how to direct the sessions to RAM! I read the PHP doc and search internet, but... Only one thing I found - that memcache is not very good still.
– user3079758
Jun 29 at 11:49





@user3079758 Nobody is going to be able to link you to a tutorial on "directing sessions to RAM", for the reasons I explained in my last comment. I don't know what it is about memcache that you think is "not very good", but Wikipedia has a whole list of in-memory databases. Pick one, download or write a PHP "session handler" to put your session data in there, and your data will be in RAM. There is no magic shortcut to "just put it in memory".
– IMSoP
Jun 29 at 13:25






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift