views:

759

answers:

3

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,

+1  A: 

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.

Chacha102
And if I want to use the session, in another page?I need to register the session with Zend_Registry, or?
Uffo
No, just call the same line `new Zend_Session_Namespace('Default')`
Chacha102
Well, yeah I know, but my action it's accesed by a jquery plugin called uploadify, and I don't know, why doesn't set a session, i have also tried with $_SESSION, and Zend_Registry, and for example if I try in another action, to call the session, or the registry name, doesn't work :|
Uffo
And, yeah I have Zend_Session::start(); on my bootstrap, and I'm using session, in another parts of my website, mabe the problem is that script...
Uffo
+1  A: 

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 !

Pascal MARTIN
+1  A: 

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');
    }
?>

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 />";
    }
?>
lo_fye
Doesn't work.<br /> <b>Fatal error</b>: Uncaught exception 'Zend_Exception' with message 'No entry is registered for key 'session'' in C:\wamp\www\whitestarbooking\Zend\Registry.php:145 Stack trace: #0 C:\wamp\www\whitestarbooking\application\admin\controllers\UploadsController.php(253): Zend_Registry::get('session') #1 C:\wamp\www\whitestarbooking\Zend\Controller\Action.php(512): Admin_UploadsController->artistsphotosuploadAction() #2 C:\wamp\www\whitestarbooking\Zend\Controller\Dispatcher\Standard.php(288): Zend_Controller_Action->dispatch('artistsphotosup...') #3 C:\.......
Uffo
try changing: if(!Zend_Registry::get('session'))to: if(!isset(Zend_Registry::get('session')))
lo_fye
Use Zend_Registry::isRegistered('session') to test whether a key is set
David Caunt