store posted variables from a form long time - php

I have a form which is passing a number array like (56,78,98). I have stored that array in a variable called $array.
eg:
if(isset($_POST['submit']))
{
$checkbox_values=$_POST['checkbox_values];
$array= implode(',',$checkbox_values);
}
$_POST['checkbox_values] is values of check boxes from a form.
I need to retrieve value of $array after the if(isset($_POST['submit'])) condition. I am using zend frame work. How can I use $array after if condition? or how can I use session or cookie in zend?
Thanks!!!

you could assign the array directly to the session using Zend_Session_Namespace:
//you can initialize the session namespace almost anywhere.
$session = new Zend_Session_Namespace('form')
if(isset($_POST['submit'])) {
$checkbox_values=$_POST['checkbox_values'];
//will assign values to session namespace 'form' as key 'checkbox_values' as an array
//session namespace will also accept objects and scalar values.
$session->checkbox_values = implode(',', $checkbox_values);
//$array= implode(',',$checkbox_values);
}
you can use the array any way you choose. You can pass it to the view...
$this->view->checkboxValues = $session->checkbox_values;
or you can pass it to your favorite model, what ever you need to do you can do. the session will remain until you unset it or overwrite it.
Good Luck.

you are missing a single quote
$checkbox_values = $_POST['checkbox_values'];
otherwise everything looks fine.

Related

Undefined variable _POST if called dynamically [duplicate]

I'm attempting to dynamically access both the $_GET and $_POST arrays, among others, using variable variables. The reason I'm trying to do this is so that I can perform similar actions on multiple arrays without needing to declare specific iterations for each. I'm reasonably sure this is possible, as PHP's documentation says it is able to use variable variables to dynamically access an array, however I'm unable to get it to work. A simple demonstration is when I'm attempting to verify that a certain property has been set.
if(isset(${$this->_array}[$property])) { return ${$this->_array}[$property]; }
else { return null; }
When I run the above script I always get null, however when I statically seek the desired property, manually using $_GET or $_POST, I get the desired outcome. I have triple checked $this->_array and $property and they are returning the correct string values. Are these arrays unavailable for such access, or am I doing something wrong?
Superglobals (such as $_POST) can not be used as variable variables within functions.
You could say something like $post = $_POST; and then use 'post' and it'd work, but directly using '_POST' won't.
Superglobals cannot be referenced as variable variables inside of classes or methods, so this will work:
<?php
$var = "_GET";
print_r(${$var});
But this will not:
<?php
test();
function test() {
$var = "_GET";
print_r(${$var});
}
I suspect that there is a better way to do what you are trying to accomplish.
http://php.net/manual/en/language.variables.superglobals.php#refsect1-language.variables.superglobals-notes
Whatever you're doing wrong, using variable variables is probably making it worse. For your own sanity, please stop. They should never be deployed in production code under any circumstances. They're impossible to debug, and using them in your code is like trying to read something that someone else wrote with their feet. If they have particularly dexterous feet, then perhaps you can understand what they're doing. But 99.9999% of the time, it is better to just use normal arrays.
That being said, try $_REQUEST instead.
There's already an array that contains both $_GET and $_POST. It's named $_REQUEST. Having said that, it can also contain the contents of $_COOKIE depending on the request_order setting, but the default is just $_GET and $_POST.
You say you want to access both the $_GET and $_POST arrays, among others -- what are these 'others'? You can use $_REQUEST to check the contents of $_GET, $_POST, and $_COOKIE all at once.
you can do this but dont know if it is a good coding practice
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$method = '_POST';
}
else {
$method = '_GET';
}
$data = $$method;
You can create an associative array that references both arrays, and use that.
$params = [
'_GET' => $_GET,
'_POST' => $_POST
];
Then you can use
return $params[$this->_array][$property] ?? null;

How do I save an array into session in codeigniter?

I want to save an array in codeigniters' session helper but I keep getting
I want to save an array of arrays in a new session variable so that I can view that session in another controller if I need it or in another view.
my session is autoloaded in my config file.
this piece of code is in my view file.
$arr = array();
foreach($value->result as $val){}
if($val->somethinghappenedtrue){
$arr[] = array('data' => $thethingthathappened);
}
}
// since my session is autoloaded I don't need to initialize
//session if I'm not wrong $this->load->session etc...
$this->session->new_session_name($arr);
Fatal error: Call to undefined method CI_Session::new_session_name()
Let’s say a particular user logs into your site. Once authenticated, you could add their username and e-mail address to the session, making that data globally available to you without having to run a database query when you need it.
You can simply assign data to the $_SESSION array, as with any other variable. Or as a property of $this->session.
Alternatively, the old method of assigning it as “userdata” is also available. That however passing an array containing your new data to the set_userdata() method:
$this->session->set_userdata($array);
Your code should be
foreach($value->result as $val){}
if($val->somethinghappenedtrue){
$arr[] = array('data' => $thethingthathappened);
}
}
// since my session is autoloaded I don't need to initialize
//session if I'm not wrong $this->load->session etc...
$this->session->set_userdata($arr);
If you want to add userdata one value at a time, set_userdata() also supports this syntax:
$this->session->set_userdata('some_name', 'some_value');
If you want to verify that a session value exists, simply check with isset():
// returns FALSE if the 'some_name' item doesn't exist or is NULL,
// TRUE otherwise:
isset($_SESSION['some_name'])

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

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.

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

Zend Framework - How to modify a single parameter from the request object

A submitted form on my site returns an array of request data that is accessible with
$data = $this->getRequest();
This happens in the controller which gathers the data and then passes this array to my model for placing/updating into the database.
Here is the problem. What if I want to manipulate one of the values in that array? Previously I would extract each value, assigning them to a new array, so I could work on the one I needed. Like so:
$data = $this->getRequest();
$foo['site_id'] = $data->getParam('site_id');
$foo['story'] = htmlentities($data->getParam('story'));
and then I would pass the $foo array to the model for placing/updating into the database.
All I am doing is manipulating that one value (the 'story' param) so it seems like a waste to extract each one and reassign it just so I can do this. Additionally it is less flexible as I have to explicitly access each value by name. It's nicer to just pass the whole request to the model and then go through getting rid of anything not needed for the database.
How would you do this?
Edit again: Looking some more at your question what I am talking about here all goes on in the controller. Where your form`s action will land.
Well you have a couple of options.
First of all $_GET is still there in ZF so you could just access it.
Second there is:
$myArray = $this->_request->getParams();
or
$myArray = $this->getRequest()->getParams();
Wich would return all the params in an array instead of one by one.
Thirdly if the form is posted you have:
$myArray = $this->_request()->getPost();
Wich works with $this->_request->isPost() wich returns true if some form was posted.
About accessing all that in your view you could always just in controller:
$this->view->myArray = $this->_request->getParams();
edit: right I taught you meant the view not the model. I guess I do not understand that part of the question.
If you want to deal with the post data inside your model just:
$MyModel = new Model_Mymodels();
$data = $this->_request->getParams();
$data['story'] = htmlentities($data['story']);
$myModels->SetItAll($data);
And then inside your model you create the SetItAll() function (with a better name) and deal with it there.
Edit: oh wait! I get it. You hate sanytising your input one by one with your technique. Well then what I showed you about how to access that data should simplify your life a lot.
edit:
There is always the Zend_Form route if the parameters are really coming from a form. You could create code to interface it with your model and abstract all this from the controller. But at the end of the day if you need to do something special to one of your inputs then you have to code it somewhere.

Categories