How to unset session in Magento? - magento

I am using session in my magento custom module.Below is my code
$session = Mage::getSingleton("core/session", array("name"=>"frontend"));
$session->setData("day_filter", 'weeks');
$session->setData("days", '5');
$session->setData("next_delivery_date", '2012-05-12');
The above code is working fine, but now i want to unset or destroy all the value? Can you please provide me the solution how to unset all set values?

There are several ways to unset session variables in Magento. Most of these (not all) are defined in Varien_Object and so are avialable to all objects in Magento that extend it.
unsetData:
$session->unsetData('day_filter');
$session->unsetData('days');
$session->unsetData('next_delivery_date');
uns (which will be marginally slower and ultimately executes unsetData anyway):
$session->unsDayFilter();
$session->unsDays();
$session->unsNextDeliveryDate();
getData
Not a mistake! A relatively unkown method exists in Mage_Core_Model_Session_Abstract_Varien. The getData method in this class contains an optional boolean second parameter which if passed true will clear the variable while returning it.
So $session->getData('day_filter', true); would return the session variable day_filter and also clear it from the session at the same time.
Set to null:
$session->setData('day_filter', NULL);
$session->setData('days', NULL);
$session->setData('next_delivery_date', NULL);
unsetAll | clear
Finally you could use the nuclear option (BEWARE: This will unset ALL DATA in the session, not just the data you have added):
$session->unsetAll(); or $session->clear(); (both aliases of each other)

Related

change a autoloaded variable in codeigniter framework

I need to change a variable which is saved over the autoloader function.
Inside of a existing class (named "app") I can check if the variable is set.
$this->options[$name];
Now I want to change the value of these autoloaded variable, inside my app.php class in this way
---
echo"old value: ".$this->options[$name].")";
$this->options[$name] = $value;
echo "new value:".$this->options[$name];
return true;
...
for this, i get the correct new value.
The problem is, that it seems that this new value is not updated for the rest of the script!? If i access this variable later, i get the old value!?
What i do wrong?
The only "variables" that persist across different pages/loads/refreshes .etc. are those that are stored in a session variable, cookie or database.
To reiterate; this:
echo"old value: ".$this->options[$name].")";
$this->options[$name] = $value;
echo "new value:".$this->options[$name];
return true;
only affects the current instance at run-time (you can look at this as a page view). It will not persist. Same goes for config value changes in CodeIgniter.
Your only method is using some sort of file/database based storage.

Updating session vaules in CodeIgniter

Have a little problem, how to update single value in CI session.
I have
$data['jezik'] = $this->uri->segment(1);
$this->session->userdata('jezik',$data['jezik']);
$data['jezik']= $this->session->userdata('jezik');
But it does not change the value. Always the same!
Try:
$this->session->set_userdata('jezik',$data['jezik']);
You need to use set_userdata rather than userdata. There are two ways to do it. You can do it in a key/value type way
$this->session->set_userdata('jezik', $data['jezik']);
Or you can pass an array
$sessionData = array('jezik' => $data['jezik'])
$this->session->set_userdata('jezik',$data['jezik']);
You can read more about CodeIgniter sessions here
Like #Pattle said
$this->session->set_userdata('jezik', $data['jezik']);
Though I think he meant to pass in the array, like so:
$sessionData = array('jezik' => $data['jezik']);
$this->session->set_userdata($sessionData);

Best practice in handling invalid parameter CodeIgniter

Let's say I have a method at Controller named
book($chapter,$page);
where $chapter and $page must be integer. To access the method, the URI will look like
book/chapter/page
For example,
book/1/1
If user try to access the URI without passing all parameter, or wrong parameter, like
book/1/
or
book/abcxyz/1
I can do some if else statements to handle, like
if(!empty($page)){
//process
}else{
//redirect
}
My question is, is there any best practice to handle those invalid parameters passed by user? My ultimate goal is to redirect to the main page whenever there is an invalid parameter? How can I achieve this?
Using the CodeIgniter routing in config/routes.php is pretty useful here, something like this:
$route['book/(:num)/(:num)'] = "book/$1/$2";
$route['book/(:any)'] = "error";
$route['book'] = "error";
Should catch everything. You can have pretty much any regular expressions in the routes, so can validate that the parameters are numeric, start with a lowercase letter, etc..
The best logic here seems to be adding the default values:
book($chapter = 1, $page = 1);
and then checking if they are numeric
So it automatically opens the 1st page of the 1st chapter if there are parameter missing or non-numeric.

CakePHP clarification on using set() and compact() together. Will only work w/ compact()

I know compact() is a standard php function. And set() is a cake-specific method.
I am running a simple test of passing a value to a view generated with ajax (user render() in my controller), and it only passes the value from the controller to the view if my setup is like so:
$variable_name_to_pass = "Passing to the view using set() can compact()";
$this->set(compact('variable_name_to_pass'));
From reading the manual, it appears set() should work along w/out compact.
Can anyone explain why set() will not work alone? Like
$this->set('variable_name_to_pass');
According to the CakePHP API:
Parameters:
mixed $one required
A string or an array of data.
mixed $two optional NULL
Value in case $one is a string (which then works as the key). Unused if $one is an associative array, otherwise serves as the values to $one's keys.
The compact function returns an associative array, built by taking the names specified in the input array, using them as keys, and taking the values of the variables referenced by those names and making those the values. For example:
$fred = 'Fred Flinstone';
$barney = 'Barney Rubble';
$names = compact('fred', 'barney');
// $names == array('fred' => 'Fred Flinstone', 'barney' => 'Barney Rubble')
So when you use compact in conjunction with set, you're using the single parameter form of the set function, by passing it an associative array of key-value pairs.
If you just have one variable you want to set on the view, and you want to use the single parameter form, you must invoke set in the same way:
$variable_to_pass = 'Fred';
$this->set(compact('variable_to_pass'));
Otherwise, the two parameter form of set can be used:
$variable_to_pass = 'Fred';
$this->set('variable_to_pass', $variable_to_pass);
Both achieve the same thing.
Compact returns an array. So, apparently set is checking it's parameters and if it's an array. It knows that it's from compact. If not it expects another parameter, the value of variable.

codeigniter associative array in post

I normally name my db specific fields in my forms like this "objectname[columnname]", I tseems CI cant access these values using $this->input->post('objectname[columnname]'), what do I do? there is not a chance in hell im renaming 100+ form fields.. I am actually disliking CI, it really is getting in the way of progress by changing the de facto PHP norms...
And were you using $_POST['objectname[columnname]'] or $_POST['objectname']['columnname'] ?
Have you tried the equivalent for the latter
$obj = $this->input->post('objectname');
echo $obj['columnname'];
?
If it works, you can write you own helper to retreive that like post_val('objectname[columnname]').
I saw this post whilst looking for a similar issue, but worked out a CI way to do it, sorry if I'm resurrecting it, but it does appear fairly high on the Google results.
// Load the 'array' helper
$this->load->helper('array');
// Use the 'element' function to return an element from the array
echo element('ColumnName', $this->input->post('ObjectName'));
Hope this helps anyone who comes here in future.
HTML code:
<input type="text" value="" name="myPostArrayName[]">
<input type="text" value="" name="myPostArrayName[]">
Handling form with codeigniter:
$data = $this->input->post('myPostArrayName', TRUE);
You can access data in order like this
echo 'Value of the first element in the form array is '.$data[0];
echo 'Value of the second element in the form array is '.$data[1];
I think someone who has access to codeigniter documentation, had better to add a simple html post array handling example.
I seems I can rely on the $_POST var, but I thought this was reset?
You can cast the post array as an object and use method chaining to return sub-arrays (now properties) using PHP 5.3's method chaining all on one line.
Extend the input class by making a class called MY_Input and put the extended class in the application/core folder. CI 2.0 will automatically use the extended class with the MY_ prefix, and you can add methods to this new class. Extending the input class is cleaner than making helpers.
This method casts the post array, or a nested array (a sub array below the parent), as an object.
/* Cast an array from CI post as an object and return the object */
public function post_obj($key = null){
$post_return = $this->post($key);
if (false === $post_return)
return false;
return (object)$post_return;
}
Now I can retrieve nested values in one line of code using PHP 5.3's method chaining for objects.
$active = $this->input->post_obj('user')->active;
I just went with the $_POST['objectname']['colname'] option as i usually do even though this is probably not the CI way..

Resources