Is there a way of passing a multidimensional array into a session? - php

I've been trying with this issue all morning. When i load the function which sets the array directly, and remove the redirect, it works fine. All of the data in both levels of the array is there. When i add back in the redirect, the function redirects and the second level of the array is removed. Leaving just the first. So the Session is setting, but it's dropping the data from the session for some reason.
The Function Which controls the array and redirect
public function test(){
$i=$_GET['i'];
$this->construct_data();
redirect('view/name?i='$i);
}
public function construct_data(){
$i=$_GET['i'];
list($array1)=$this->array1($i);
list($array2)=$this->array2($i);
list($array3)=$this->array3($i);
list($array4)=$this->array4($i);
list($array5)=$this->array5($i);
list($array6)=$this->array6($i);
$container= array(
'array1'=>$array1,
'array2'=>$array2,
'array3'=>$array2,
'array4'=>$array4,
'array5'=>$array5
);
$this->session->set_userdata('construct',$container);
}
The view
$data = $this->session->userdata('construct');
var_dump($data);
The var_dump in the view returns all of the first level of container, array 1 through 5. But it doesn't contain any of the data inside of the functions that are called. it returns $data['array1'].
Now if i were to put the var_dump inside the test function instead of redirect, the data is returned as it should be, $data['array1']['array1item'].
Let me know if this was unclear. Also the names of variables and functions were changed to maintain anonymity.

Instead of adding array directly to session, first convert it into json by using json_encode() function. On retrieving you have decode it by using json_decode() function.
Add To session
$this->session->set_userdata('construct',json_encode($container));
Reading From Session
$data = $this->session->userdata('construct');
var_dump(json_decode($data,true));

Okay, the actual problem was size. I was making it too big and as a result it couldn't pass it outside of the class.

Related

Get Session data in store function

Is it right to get Session data in a store function and store them into db?
public function store(){
...
$idgroup = Session::get('invitation_userid')];
...
}
Or need a store function always a Request Object?
public function store(Request $request){
...
$idgroup = $request('idgroup');
...
}
In both functions is of course a validation part for the input data.
Both approaches are fine, but you should use them appropriately to your use case, I prefer to use the Request data. The main difference is that if u store that inside the Session it will be available application wide, while if u send inside Request it will be available inside the method only
This depends entirely on the context of what your controller is actually named, how this data is being used and why you are not using a database session driver in the first place if you want to do this.
You could simply use the database driver for the session:
https://laravel.com/docs/5.7/session#introduction
It also depends on what your controller is named if you strictly want to follow restful routes:
https://gist.github.com/alexpchin/09939db6f81d654af06b
To answer the second question you don't always need a Request object in your store action. Most of the time you won't even see a Request object because you are simply creating an entirely new resource.
The Global Session Helper
You may also use the global session PHP function to retrieve and store data in the session. When the session helper is called with a single, string argument, it will return the value of that session key. When the helper is called with an array of key / value pairs, those values will be stored in the session:
$value = session('key');

How to store array in CodeIgniter sessions?

I am calling a controller with method setActiveId and store 1 array in session and display after storing by using var_dump($this->session->all_userdata()) it shows perfectly but in same controller when I print the session in other method say checkinkingPermission then the array in session is empty.
Now I repeat all steps with one additional step.
I did store array in session like before and after storing I store one dummy variable in session like this,
$this->session->set_userdata('dummy','tesing');
and again print using var_dump($this->session->all_userdata()) it display all session with array and last dummy variable and when check in checkinkingPermission then array in session and dummy variable is shown perfectly (it solves my problem but its rough solution).
I want to know did anything miss or what problem is there that array not save in first scenario (also array is not greater then 4Kb (CI Session limit)).
Update
Method from model named connections_model
public function setActiveFriend($id) {
$this->load->model('friend_model');
$this->session->set_userdata('AF',$id);
if($id==$this->session->userdata('userid'))
{
$this->session->set_userdata('MEOWN',1);
}
else
{
$this->session->set_userdata('MEOWN',0);
}
$this->friend_model->setFP($id); // Calls Here a method that is defined in another model
}
Another method that defined in friend_model
public function setFriendPermissions($id) {
$this->session->set_userdata('CFP',array());
$this->db->where('user_id',$id);
$locs = $this->db->get('friend_p')->result_array();
if(is_array($locs))
{
foreach($locs as $loc)
{
array_push($this->session->userdata['CP'],$loc['perm_id']);
}
}
$this->session->set_userdata('dummy','tesing'); // Important Line
}
In above method I set values in session array if I want to see session values in same method then its set and viewed but the array is empty if I print session in any method of connections_model file.
Important
If I wrote this line $this->session->set_userdata('dummy','tesing'); then session array save and viewd in all methods and if I don't write that line session array is empty.
You're not accessing the session data properly:
$this->session->userdata['CURRENTFPERMISSIONS']
That's what you have. Keep in mind that $this->session->userdata is a function, not an array, that said, try this
array_push($this->session->userdata('CURRENTFPERMISSIONS'),$locpermission['perm_id']);
Please see the documentation on session management
I don't recommend using all cap keys for your arrays, as that convention is supposed to be used for constants.
In addition to the above, try changing this:
if(is_array($locpermissions))
{
foreach($locpermissions as $locpermission)
{
array_push($this->session->userdata['CURRENTFPERMISSIONS'],$locpermission['perm_id']);
}
}
to this
if(is_array($locpermissions))
{
$tmpArr = $this->session->userdata('CURRENTPERMISSIONS');
foreach($locpermissions as $locpermission)
{
array_push($tmpArr,$locpermission['perm_id']);
}
$this->session->userdata('CURRENTPERMISSIONS',$tmpArr);
}
Although, the more I look at your code, the more I don't understand why you are going ahead and setting up a blank array in the session structure if it only ever gets populated inside a conditional. May want to re-factor that a bit

Need to access associative array data created inside a class by json_decode from outside the class

I've been using procedural php for a long time but am still learning OOP. I was able to find a bit of code online that is in the form of a class and I am able to call a new instance of the class and use var_dump on a string to have it print JSON data to a web page. I can look at the results of the var_dump and see that it's returning exactly what I want. I'm then able to use json_decode on the same string to turn it into and associative array and then I can echo the values from within the class. The problem is, I need to use the array values in more code - it's great that I can confirm it by printing it to a web page but I need to use it... but I'm getting errors that state the array is undefined once I try to access it outside of the class.
I'm using this line to convert the data into an array:
$response_array = json_decode($body, true);
I've already confirmed that this works within the class by using this code to print some of the data:
echo $response_array['amount'];
and it works - I see it on the web page.
I've been using this code to create the new instance of the class:
$fdata = new FData();
$fdata->request($order_total, $cc_exp, $cc_number, $cc_name, $order_id, $customer_id);
(the function named 'request' is defined as a public function inside the class)
After that, I want to grab the $response_array so that I can store the returned data into a transactions table, i.e something like this:
$r = mysqli_query($dbc, "CALL add_transaction($order_id, $response_array['transaction_type'], $response_array['amount'], $response_array['exact_resp_code'], $response_array['exact_message'], $response_array['bank_resp_code'], $response_array['bank_message'], $response_array['sequence_no'], $response_array['retrieval_ref_no'], $response_array['transaction_tag'], $response_array['authorization_num'])");
but I keep getting an error saying that the array values are undefined.
Things I have already tried (and which failed) include:
Defining the variables as public inside the class, setting their value in the class, and then calling them outside the class...
public $amount = $response_array['amount'];
then using $amount in my procedure CALL --- I still get the error saying that $amount is undefined.
Using 'return', as in
return $response_array;
and still the error saying the values are undefined.
I tried embedding the entire rest of my code within the class, just copy/paste it in right after the json_decode... but for some reason it can't seem to then make the database calls and other things it needs to do.
I've been reading about __construct but I'm not sure if it applies or how to use it in this case...
I want to stress that I AM able to see the results I want when I use var_dump and echo from within the class.. I need to access the array created by json_decode OUTSIDE of the instance of the class.
Any advice would be greatly appreciated. Thanks.
Assuming your FData::request method ends with something like this...
$response_array = json_decode($body, true);
return $response_array;
and you call it like this...
$response_array = $fdata->request(...);
You should then be able to use $response_array in the calling scope.
Extra note; you should be using prepared statements with parameter binding instead of injecting values directly into your SQL statements.

Unexpected behavior by CodeIgniter while loading views

I have a viewer helper function that loads the main content alongside the footer/header. The bug/unexpected behavior occurred when I loaded the array's key for the header that shares the same name for a variable in the main content view - the same array is loaded for both the header and main content.
I thought it's normal, since the same $data array was sent to the header and main content as-well(as mentioned before). So the variable will naturally be present in both views. But, well, it wasn't exactly like that. I unset the $data variable after sending the data to the header then re-created it when I wanted to send some data to the main view - but still the problem is not fixed.
I made a simple example for this bug/unexpected behavior:
Consider this view, named test:
<?php
echo $some_data;
And this controller:
class Test extends CI_Controller {
function index() {
$data['some_data'] = 'Some data.';
$this->load->view('test', $data);
/*
* Output:
* Some data.
*/
unset($data);
unset($data['some_data']);//Just to make sure it's not PHP's fault.
$this->load->view('test');
/*
* Output:
* Some data.
*
* Even though the $data variable is unsetted AND not passed!
*/
$different_data = array();
$this->load->view('test', $different_data);
/*
* Output:
* Some Data.
*
* Still outputs the same thing, even though
* I'm sending different array(and the $data array is unstted).
*
*/
}
}
Note: The whole code will output Some data. three times.
The only way to solve this issue is sending a different array and setting the array key(which is some_data) to something else which will override the old one.
So, is this a bug or something made by CodeIgniter's dudes?
we had the same problem like you and our Alien coworker found THE solution:
if file: codeigniter\system\core\Loader.php
find the code: (i think the line number is 806):
$this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
and correct it to:
$this->_ci_cached_vars = $_ci_vars;
best regards
This is expected behavior.
Once variables are set they become available within the controller class and its view files. Sending an array in $this->load->view() is the same as sending an array directly to $this->load->vars() before calling the view file. This simplifies things for most people using multiple views in a controller. If you are using multiple view files in a single controller and want them to each have their own set of variables exclusively, you'll need to manually clear out the $this->load->_ci_cached_vars array between view calls.
A code comment in the Loader class describes another situation showing why this is the desired default behavior:
//You can either set variables using the dedicated $this->load_vars()
//function or via the second parameter of this function. We'll merge
//the two types and cache them so that views that are embedded within
//other views can have access to these variables.
This is a CodeIgniter issue. The variables you are sending in seems to be cached until you override them. I have encountered this myself and can verify it.
$this->load->view('test', array('some_data' => NULL));

Using objects in Ajax calls PHP files

I have instantiated a class in my index.php file. But then I use jQuery Ajax to call some PHP files, but they can't use my object that I created in the index.php file.
How can I make it work? Because I donĀ“t want to create new objects, because the one I created holds all the property values I want to use.
Use the session to save the object for the next page load.
// Create a new object
$object = new stdClass();
$object->value = 'something';
$object->other_value = 'something else';
// Start the session
session_start();
// Save the object in the user's session
$_SESSION['object'] = $object;
Then in the next page that loads from AJAX
// Start the session saved from last time
session_start();
// Get the object out
$object = $_SESSION['object'];
// Prints "something"
print $object->value;
By using the PHP sessions you can save data across many pages for a certain user. For example, maybe each user has a shopping cart object that contains a list of items they want to buy. Since you are storing that data in THAT USERS session only - each user can have their own shopping cart object that is saved on each page!
Another option if you dont want to use sessions is to serialize your object and send it through a $_POST value in your AJAX call. Not the most elegant way to do it, but a good alternative if you don't want to use sessions.
See Object Serialization in the documentation for more informations.
mm, you should store in session, $_SESSION["someobj"] = $myobj;, and ensure that when you call the Ajax PHP file this includes the class necessary files which defines the class of $myobj and any contained object in it.
Could you be more specific? I can try.
This is how I create an object then assign it to a session variable:
include(whateverfilethathastheclassorincludeit.php)
$theObject = new TheObjectClass();
//do something with the object or not
$_SESSION['myobject'] = $theObject;
This is how I access the object's members in my Ajax call PHP file:
include(whateverfilethathastheclassorincludeit.php)
$theObject = $_SESSION['myobject'];
//do something with the object
If you don't want to move your object that is in your index.php, have your ajax make a request to index.php but add some extra parameters (post/get) that let your index.php know to process it as an ajax request and not return your normal web page html output.
You have not provided code, but what I guess is that you need to make your instantiated object global for other scripts to see it, example:
$myobject = new myobject();
Now I want to use this object elsewhere, probably under some function or class, or any place where it is not getting recognized, so I will make this global with the global keyword and it will be available there as well:
global $myobject;
Once you have the object, you can put it into the session and then utilize it in the Ajax script file.
As others have suggested, $_SESSION is the standard way to do it, in fact, that was one of the reasons, that sessions where invented to solve. Other options, i.e. serializing the object rely on the client side to hold the object and then return it untampered. Depending on the data in the object, it is not a good solution, as a) the object may include information that should not be available on the client side for security reasons and b) you will have to verify the object after receiving it.
That said, and if you still want to use the object on the client side, then JSON is an option for serializing object data, see JSON functions in PHP.
Based on most of the answers here, referring to storing the object in $_SESSION, is it more efficient to store only the individual properties that need to be accessed in AJAX as opposed to the whole object, or does it not matter?
E.g.
$_SESSION['object'] = $object;
vs
$_SESSION['property1'] = $object->property1;
$_SESSION['property2'] = $object->property2;
I know the OP is asking about accessing the entire object, but I guess my question pertains to if it's just a matter of only accessing certain properties of an object, and not needing to access methods of a class to alter the object once it's in AJAX.

Categories