Reputation: 10056
I have read..some articles on the internet, but i don't get it :|, can you guys give me an example, how to make something like this: $_SESSION['name'] = 'value'; and echo $_SESSION['name'].How can I create something like this, with ZF?
Best Regards,
Upvotes: 0
Views: 8671
Reputation: 6860
Put this at the top of your page, before anything else:
<?php
Zend_Session::start();
if(!Zend_Registry::isRegistered('session'))
{
$session = new Zend_Session_Namespace('YourSiteName');
Zend_Registry::set('session', $session);
}
?>
To modify your session:
<?php
$session = Zend_Registry::get('session');
$session->user_name = $user_name;
?>
To read your session:
<?php
$session = Zend_Registry::get('session');
echo 'Hello '.$session->user_name.' !';
?>
Or to see all values in the current session namespace:
<?php
$session = Zend_Registry::get('session');
foreach ($session as $index => $value)
{
echo "session->$index = '$value';<br />";
}
?>
Upvotes: 6
Reputation: 61597
You can do it (like the tutorial states) by this:
$defaultNamespace = new Zend_Session_Namespace('Default');
if (isset($defaultNamespace->name))
{
echo $defaultNamespace->name;
}
else
{
$defaultNamespace->name = "hi";
}
First, you need to create the Session Object using:
$defaultNamespace = new Zend_Session_Namespace('Default');
By defining a custom value in place of default, it means that none of your variables will mix with variables from other systems, or other parts of your system that use unique values in place of default.
After that, every variable can be accessed like a regular class variable
Any variable can be assigned using
$defaultNamespace->variable_name = value;
To get any value, simply get the same value;
$variable - $defaultNamespace->variable_name; // gets value
As Pascal noted, you also need to call
Zend_Session::start();
before all of this.
To find out more about it, use the Basic Usage Examples.
Upvotes: 2
Reputation: 401182
When working with Zend Framework, you should not work with $_SESSION
directly, but, instead, use the Zend_Session
class.
You will probably find answers to your question on the Basic Usage manual page.
For instance, to use a numberOfPageRequests
stored in session, you could use something like this :
$defaultNamespace = new Zend_Session_Namespace('Default');
if (isset($defaultNamespace->numberOfPageRequests)) {
// this will increment for each page load.
$defaultNamespace->numberOfPageRequests++;
} else {
$defaultNamespace->numberOfPageRequests = 1; // first time
}
It might seem a bit more complicated that working with $_SESSION
directly, I admit... but this gives you a coherent, object-oriented, interface -- that's at least something ^^
Of course, you might need to start the session before that, using
Zend_Session::start();
If you have additionnal questions, more specific, don't hesitate to ask !
Upvotes: 2