i hava a class that hold a list for each request
but each request the list is empty again
what can i do to make it live
here is my class , i want the list to hold values from previews requests
(yes each request i'm settings a value there )
class Sessions{
private static $list = array();
....
.....
}
It's supposed to be like that. Every request is independent and restarts whole program. Use sessions to store data between requests.
'static' variables do not survive until the next request. You should either use $_SESSION to store custom data per user, or save it to some file/database/...
If you want data to persist for each request from the same user you have to use session. If you want data to persist for every user you have to store them in a file or in database.
Related
I would like to use Classes in my next website. Part of the site involves a multi-page profile form. The visitor fills in their name, submits this and then fills in their date of birth, submits this and then adds some text.
When the first form is submitted I will instantiate the class and update the database etc but when the visitor submits the next form do I need to declare another "new" class or does it stay instantiated?
What is the recommended way of achieving this kind of behaviour (ie not using classes for this / using sessions to hold the data / instantiating all classes on each page refresh / etc)
Thanks for some direction with this.
As PHP is stateless any classes you initiate only exist for that one page. If you create an instance with data in, it will only exist on that page. If you want it to exist on multiple pages then you will need to reload it on every page.
For what you're describing it would seem that you would first gather all the data, storing the users answers in session between each page, and then once you have all the answers initiate your class to do whatever it is you want to with your data.
Php data is unset after next request exept for persistant data like session and etc... .
To answer your question:
If this case is on one request and the scope of the variable is ok you could use the same variable(and instance) of the object.
If not, you should make a new instance.
I want to temporary store a series of array which will be used by next request. The stored information contains some sensitive data which will be used for navigating around that page with ajax call. The data were different from pages to pages. So, I just need to temporary store it for use when user is on that page.
First, I try to do it with cache: Cache::put($dynamickey, $multiArray, 20); But this will result in huge amount of "junk" cache store inside the folder even after it is expired.
So, I tried with session flush: Session::flash($dynamickey, $multiArray);. This works when user is open only 1 tab of webpage. But if user is open multiple tab of this website, it breaks.
For example:
1. User browse this website on tab1.
2. Then, user browse this website on tab2. As soon as after user browse website on tab2, the session data for tab1 is removed.
3. User come back and navigate tab1 content. The system break, and not working.
How can I store temporary data which will be deleted once it is no longer required, but also works well with multiple tab?
Thank you.
So, on the page that actually sets the session data you will need to generate a dynamic key which you can also generate when the ajax call is made. So:
Session:put($dynamicKey, $data);
Since the server doesn't know if you have multiple tabs open it just processes more requests, we need to distinguish AJAX requests from standard ones. This can be achieved via:
if (Request::ajax())
{
if (Session::has($dynamicKey)) {
Session::forget($dynamicKey);
// Do your application logic
}
}
So the session will not be removed until an ajax request is made where you can regenerate that key, now if you cannot regenerate that key from the data provided then you cannot tell apart two different requests. So you will need to get this key to the client side some how such as echoing it into a bit of javascript.
Now the AJAX call can utilise this key and send it in the request, where your server can pick it up and find the correct session of that tab.
Hope you understand this.
i have two page one.php and two.php
one.php for form submit and two.php for receiving the form values.
In two.php, i have an array to insert the posted value in it and redirect again to one.php. Is it possible to fixed that array with values until another post is coming from one.php?
If my question is not clear ,plz let me know...
Thanks....
No. HTTP is stateless. Every new request will wipe out the old page and re-build the array. You should probably use $SESSION or some other storage for a data source that will persist across requests.
You can achieve this by using SESSION or any Caching techniques.
On each request store the POST values to SESSION or Cache and retrieve those data as needed.
$_SESSION('post_data') = $_POST;
You can append the current time stamp while storing to distinguish the POST values or store some distinct vales like( username etc..)
i have a user class with a function to add a user.
i also have a form that triggers an ajax call on submit which passes a query string for the username and potentially other information to the controller.
everytime the controller gets called via ajax, it creates a new instance of my user class and calls the add user function by passing the query strings as parameters.
is there a way where i can output an array of usernames that have been submitted without storing it in a sessions.
I use a global variable to indicate I'm currently in AjAX mode. So when you call the AJAX controller set a global variable.
Then just skip the add user function by checking to see if the global variable is defined.
Your options are to store it:
In a session.
In a file.
In a database.
PHP is not a persistent service, and will not remember anything between requests other than what has been stored either in a session, or in an external store.
I'm trying to transfer a large array between two sites in PHP. I'm the admin in both.
The array is created on one site, and after its creation I wish to automatically redirect the user to the other site, and pass the processed array along.
I cannot use the SESSION superglobal, as it is limited to a specific domain.
GET is not suitable, as the array is too long.
I'm not sure if POST is suitable, and if there is a way to automatically send the data without forcing to user to click a button and submit some form. I know javascript can be used for this, but prefer to have something more robust.
I'm relatively new to PHP, and would love to hear of any other ways of performing this.
Thanks!
The easiest way would be to use a HTTP library like cURL and setup and send a POST request to the other site. Also including the users IP address would allow you to associate the posted data. Without JavaScript you cannot redirect a user with POST data.
One thing you may want to be aware of with the above method is that depending on how it is implemented the user may arrive before the data does.
There is no limit on POST as defined in the HTTP specs, but you may run into issues handling it on your other server (depending on what you mean by large) depending on php configuration. (POST limit is I believe set to 8MB by default)
Send an HTTP POST request via cURL functions and add the serialize()ed array to the request body.
I'd do something like this:
generate a token on Server A (e.g. sha1(timestamp + session id + random()))
use cURL to post the serialized array to the Server B, passing along the token you generated
On Server B, store the serialized data and token in a database table - fields: token (CHAR), data (BLOB)
redirect the user to http://ServerB/?data_token=[TOKEN GENERATED IN STEP 1]
Server B fetches the data associated with the token from the db, deletes the db entry, and stores the array in the new user session.
Well, if they are both on the same server, you can have one hijack the others session. I have used this to jump to a secure server before, use the session_id() function on the first host to get the session, then use the same function to set it on the second host.
See http://www.php.net/manual/en/function.session-id.php
I would suggest that after you create the "Array" you associate it with an ID(and store somewhere) and then redirect to the other with this ID. From site 2 using the ID, you can call a page on site 1 which returns the "Array"
Your problem:
sent array(ar) From Server(a) to Server(b)
My solution:
Server(a) generates an unique url(url) for Server(b) which contains Array(ar) encoded in for example json using json_encode(ar). This Array(ar) should be stored at url using for example mysql or just a simple text file.
$uid = md5(uniqid(mt_rand(), true)); // to generate unique id
Server(a) redirects browser to Server(b) also containing $uid
$url = "http://server-b/page"; // url to page
header('Location: $url?uid=$uid');
Server(b) gets the content from url on Server(a) and decodes content back to Array(ar)
$uid = $_GET['uid']; // uid
$url_server_a = "http://server-a/webservice?uid=$uid";
$ar = json_decode(file_get_contents($url_server_a));
I guess you could serialize it, save it as a file that is accessible from the other server and load it again from the other server. That way, no user action would be required but you'd have to protect the directory where you save the file to avoid privacy problems.
Edit: I´m assuming they're on different servers, otherwise it would be even easier...
If it were me, I'd store the information in some other medium: a memcache type environment for example, or a database that both can access.
After the array is created you could quickly generate a page that has a form which contains the data in a hidden field. This page could then automatically submit the form (with method="POST") to your redirect.
You could decode the array into JSON and send a link to the second server, which contains the download for the temporary JSON file, just re-decode the JSON file back into PHP and you do not have to use LONG URLs.