I need to pass an array from one function to another.
I tried with session variables.
$array;
$this->session->set_userdata('array',array);
$array = $this->session->userdata('array');
but it does not work for older arrangements 2KB.
I read that you can not spend such a long array, that session variables are stored in cookies on the browser and does not allow more than 2 or maximum 4KB according to the browser.
I tried to serialize also. Any ideas?
Switch sessions to database, then you can store larger amount of data. See http://www.codeigniter.com/user_guide/libraries/sessions.html#database-driver for details.
You have two option on this issue.
Don't store every data in session. Store somthing unique.
Explenation: if your database having all the user data and you are trying to store all the in session. Its worthless. Just store user id with session time So when ever you need and user data you can call the session user id and send request to database and get the user information..
Can use session tables which provided by Codeigniter.
Related
I need to save payment data in DB. So one of the ways is to store the data into session and in saving part do the rest.
I save it like this:
$request->session()->put('paymentData', $request->all());
And then in another controller I call the same session, do some things and then remove the session:
$data = $request->session()->get('paymentData', 'default');
// do some DB storing
// delete session
$request->session()->forget('paymentData');
My question is, if two users are doing payments at same time. Can it happen that they mix these sessions because they will access it under the same name paymentData? Session will be stored in file in storage\session. Or Laravel have some method to distinguish these two Sessions even though they have the same name/key?
Sessions are created per browser session. The Session instance is accessed by a specific session ID. The browser keeps the session ID in a cookie and sends it every time it makes a request, so the server knows which session to use for that request.
What you put within the session can be duplicated no problem - other users cannot see that data (unless they somehow obtain the session ID by a browser attack).
Been brain storming and researching to get 2 index file.php. One to save session and cookie value to database and the second one would call and echo the saved session and cookie value from database when user visits again. Anyone can help?
In PHP, the session value are store in $_SESSION, and $_COOKIE for cookie value. The process can be as simple as create one table that has column consist of userid, session and cookie value, and write your code to save into the table. After that in the next visit retrieve it from the table by the user id.
P.S.: Why you want to keep the session and cookie value in the database though? Take not ethat as long as the session or cookie value is not been deleted, the value will be there until the next visit.
I found this on github and tried bit the session info was not saved to DB
Session integration to DB
But i was finally able to get what i was doing wrong with my php code. I was able to achieve the task using this in my corresponding php files
<?php echo $_SESSION['userId'];?>`$Id = $_SESSION['userId'];`$_SESSION['userId'] = $Id;`
Well i have a problem and need suggestions.
I am currently uploading a csv file which contains products data. There can be a lot of products. I am reading csv file using a library and than taking all the data in an array. Now i have a situation where i am stuck how to solve this.
Scanrio :
After uploading csv the user has a form where there are 9 specific columns. All the columns from csv should come into an array which will be displayed as dropdown. In the left side the form will contain specific fields and in the right side before each field there will be dropdown containing csv column names. After selecting the map button will be clicked and the columns will be selected from array and saved it into table. Now i dont know how i can hold data without saving into table while user selects the columns for mapping. What do i do? Shall i put the whole array in a hidden fields or else? Please suggest.
Now i dont know how i can hold data without saving into table while user selects the columns for mapping. What do i do? Shall i put the whole array in a hidden fields or else? Please suggest.
You could store them in session variables, that way whatever you store there is kept for each user separately and until that user logs out or some part of your code explicitly remove it. (Say, after you have discarted or made permanent the data on a database).
Hopefully, you are already using sessions, now give a look at session variables at php.net
Note: I have never used codeigniter, but a quick search show that it includes a class for mamaging sessions, look under FlashData, it seems to be the "codeigniter way" of doing it.
Edit:
Codeigniter sessions variables have a limit of 4KB, that may be enough for you, or maybe not. If it is not, you cal always use one of these alternatives:
Use the database to store the data, and store in the session variable some item id and user id to be able to retrive it.
Use a file, you can generate a random name and store it on the session variable, then store in the file whatever contents you need.
In case you can't handle the end of the sessions, you can have a expiration date stored in the database and a programmed task (are you able to use crons?) to remove the expired items from the temporary table (or file).
Although, beware! I don't know about the security of codeigniter... but, there are some risks. You may want to add a hidden "token" field with a random unique value associated with the user in a database table and an expiration date matching the lifetime of the session. You should create a new token each time you send a form, and after you recieve it verify if the token from the submited form matches a valid token for the user (identified by the session) in the database and if so, delete the token and proceed to process the rest of the form. If the token doesn't match or it is no longer valid, then the session has expired, somebody has been messing with cookies, or you have survived an attack (the case somebody recreates a post with an old token).
I repeat, I don't know of codeingiter security. But if I were the author of that framework, it would already have tokens implemented... so, chances are this security risk is already covered.
You can learn more about Cross-site Request Forgery at OWASP.
Why don't you store the user data in a session? This way you can pass it from page to page without putting it in the HTML(where it could be easily altered by a user).
Your best options are session or cookie. Cookies are limited in that they can only contain a certain amount of data and you are limited to a certain amount of cookies since cookies are stored on the clients machine. You can use sessions and this will save the information on the server. Codeigniter limits sizes to 4kb. To do this, in PHP you simply add
session_start()
to the top of every page you wish to use your session variables.
You should sanitize your user input. To set your session variables you use the assignment operator (=).
$_SESSION['var'] = filter_input( INPUT_POST, 'field_name', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
If you are using get you can use INPUT_GET.
If the user is not submitting the form, you could add an event listener via javascript and when form values are filled out you can store them in cookies and use PHP to extract the data from the cookie.
It is always best to sanitize and validate user data on the server.
I want to store a database table in a cookie (for example, to store items in a shopping cart for a user that is not logged in).
My problem is, say the table has a list of items where I need to store an ID and amount for each item.
What is a good data structure I could use for PHP to store the data in the cookie?
Cookie Can only store 4KB of session data.You can store the session data in the database or cache in the raw file at server. if you really wish to store the data at client side take a look on some of these javascript storage libraries pablotron , DojoStorage , localstrogage Firefox,Chrome,Safari,Internet Explorer 8+ 5 MB per domain.
I have a login page. User can login to their account and can get their details. But I am confused, should I store the users details in the session variables or should I pass only the ID as session variables, and in next page all the details retrieved by class using the ID.
If I pass only the ID as session variable, then I have to call the class in every page. Please give me the right idea, I don't want to call the class repeatedly, the page should load faster.
Thanks in advance.
It won't matter performance-wise for a long, long time. You can pick any of the methods.
In my experience, storing the ID in the session, and retrieving the user data when needed in the user class is the more common way to go.
Storing an object containing the user data in serialized form in the session is also possible, but
it can cost a lot of memory (because the session data is loaded into the PHP script on every request)
You can't rely on the data being fresh (what if the user changed their preferences, or something else happened?)
I think you should only store the session ID in session variables. You will get no performance benefit if you store some information about the user in session becouse you will never no what kind of additional information will you need later and serialization-deserialization takes some time also (specially when you store sessions in database!).
For exampla i'm always load user data from db in every single request becouse i have to check the user for several reason: is he/she locked, is he/she disabled, what is his/her preferred language, when was he/she here last time and so on.