sessions in ZF ?????

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

sessions in ZF ?????

Post by the_last_tamurai »

unfortunately , I think that the manual of Zend framework is incomplete or insufficient ...it requires some tutorials..
I found a powerful framework (really) but bad documented...
I spent all yesterday trying to apply sessions to private pages in my project ... but no way .... :(
try to search google and the forum but nothing ..
anyone have a tutorial or a link to ones which apply sessions using Zend_HTTP_Client
please post it here :roll:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

Post by the_last_tamurai »

The Ninja Space Goat : thank you ... I already read it ..but I need a tutorial ...just small example

I tried to make like the manual...but I have header already sent problem.....yes, manual still vague

and I can't :oops: do it from manual.....

ok I was about to edit my first post to add ur name :) because I read your topics and I know you're using ZF.

can you tell what I have to put ( about session ) in my bootstrap file and what in login page and what in protected pages.....maybe you'll say what a lazy man is this tamurai...but really I tried yesterday alot but..... :(
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

For the headers already sent issue, see this thread:
viewtopic.php?t=1157

Once you've fixed that issue, come back.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Headers Already sent should be actually fairly uncommon (in my opinion) when using MVC. If you have session_start() at the start of the index.php most of the work is done. Any other headers are going to be primarily redirections. These should be handled within your controller actions, before you call the echo of the view->render(template) which in all likelyhood is the last thing you do anyway.

(P.S. For tutorial see Akrabat Zend Framework Tutorial, I know it helped me when starting at v0.2. It is also updated fairly regularly.)
User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

Post by the_last_tamurai »

really , I'm sorry to disturb you with questions..
I realize that my problem is not understanding the session work properly so I returned to php.net ... but some concepts are vague to me..... I'll shift my search to old fashion sessions to understand it good...

my goal was to solve this problem as fast as possible because I have to deliver the site at thursday...
I reorder my steps ..
first to complete all pages of the project while I understand session and specially in zend
second I'll do this issue latter .....
is this logical or I must walk on other way????

Code: Select all

class beginner { 
private $name="the_last_tamurai";
function  beginner (){ 
echo "HELP".$name."  "; 
}
 }
zfdeveloper
Forum Newbie
Posts: 3
Joined: Fri Feb 23, 2007 7:59 am
Location: Romania

Post by zfdeveloper »

Check for lot of tutorials here:

Zend Framework Tutorials
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It might better serve you to learn how to use the tools that will eventually go into production. I am sure the session handling of the framework is not really the issue so much as how to use them. Remember, when dealing with native PHP sessions, or any cookie based session application, or cookies, or the header() function, you must always make calls to these functions before outputting anything to the browser. Always.

Once you get your mind around that, the rest becomes really easy to understand.
User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

Post by the_last_tamurai »

OK, now I understand the sessions.....and I know how to fix headers problem
my problem now with English :( (I'm not english native speaker) ..I can't understand the language describes how to register what you want (id or username) in sessions in zend framework

so....tomorow I'll deliver the site to the client :lol:
no sessions yet , so HACKERS ARE WELCOMED ...no no no, hackers?? any internet users will access what he wants.

to moderators : why there's no crying emoticons here ?????? :?:

-->the zftutorials site directs me to zend site in session tutorial :roll:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

the_last_tamurai wrote:OK, now I understand the sessions.....and I know how to fix headers problem
my problem now with English :( (I'm not english native speaker) ..I can't understand the language describes how to register what you want (id or username) in sessions in zend framework[/qoute]
Have you reviewed the docs for the Zend framework?
the_last_tamurai wrote:so....tomorow I'll deliver the site to the client :lol:
no sessions yet , so HACKERS ARE WELCOMED ...no no no, hackers?? any internet users will access what he wants.
Not having sessions in place is no excuse to say that hackers are welcome. Your site should not be a hackerfest just because you are not using sessions.
the_last_tamurai wrote:to moderators : why there's no crying emoticons here ?????? :?:
You mean this one --> :cry: ? Use : cry : without the spaces in it.
the_last_tamurai wrote:-->the zftutorials site directs me to zend site in session tutorial :roll:
What does that tell you, if anything?
User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

Post by the_last_tamurai »

Everah : I didn't notice the cry emoticon ,thank you , I'll use it a lot :cry:

I meant from my words not hackers , any internet guest can make anything...NO SESSIONS...I want to catch user ID ..how with out it...I can't send it in the url every page :)
YES,I already make my filters to assert right users input data..so hackers will find difficulty

about zf tutorials ... really I tried to understand it but bad english stop me :( ...I just can't understand it.....

no one in my country ....really.... start with Zend yet....so no one here can help me.
I used to learn from tutorials and examples but I can't find examples uses sessions yet ...
zftutorials site ->sessions tutorials, leads me back to zend official site
I have a small question .....how can I register the user identity in a session to access his data in db along with member section???...
if my question is vague or incomplete please tell me that so I can make it more clear... :?:
I'm really sorry for that but really I'm in trouble :(
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Just a quick example. Could I ask what your native language is?

The important thing to remember is that Zend_Session wraps the entire session, but that at the developer level you will deal with sub-arrays primarily.

Code: Select all

/**
 * Make sure the Zend parent directory is in your include_path!
 */

require_once 'Zend/Session.php';

/**
 * Start the Session for this request.
 * Always call BEFORE sending headers or output to the browser.
 * It is best practice to call this BEFORE instantiating a new
 * Zend_Session_Namespace object.
 */
Zend_Session::start()

/**
 * Create a new $_SESSION sub-array. Each can be handled like a separate
 * environment with individual locking, time limits, etc. Hence it's
 * referred to as a Namespace.
 */
$session1 = new Zend_Session_Namespace('default');

/**
 * Use this as you would any session object. Just remember you can handle
 * multiple Namespace objects since each is just a sub-array of $_SESSION
 * in this case its $_SESSION['default']. Even though it's an array, you
 * can access it like any object with public fields.
 */
$session1->userName = 'Maugrim';
$session1->userId = 1;

/**
 * Need to make a session "time-out" after 10 minutes?
 * This means if no requests received from the user for 10 minutes (600 seconds)
 * this Namespace will be deleted from the next request AFTER 10 minutes.
 */
$session1->setExpirationSeconds(600);

/**
 * A handy feature. To grab the "default" namespace from anywhere in the
 * application, you can a) pass it as a parameter or in a Registry, or
 * b) make a second instance call (automatically mirrors pre-existing
 * persistent namespaces.
 */

$session1->id = 123;

$session2 = new Zend_Session_Namespace('default');

/**
 * This will PASS since $session2 references the same object in memory
 * as $session1. In a way its the Singleton pattern, just a indirect type
 * of implementation.
 */
assert($session2->id, 123);
User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

Post by the_last_tamurai »

Maugrim : thank you for your fast reply
1- my native language is arabic....I know english very well but when merging live english with technical articles I have some problems ( my php study is in english ) I'm trying to come over that soon....

2- Look how you make it easy in your reply ... why zend official manual didn't make it like that...

3- I suffered from zend framework translation to arabic for :
a - Not completed yet.
b - In comparison with english one they clip alot of data.
c - May be if I continue with ZF ...I'll try to make one 8) (I think it's not very big , and it'll make me expert in it)

what am understand from your code :
1- Zend_Session::start() in my bootstrap.

2- In login action I'll make new instance of Zend_Session_Namespace('anything'); or I can make it my bootstrap and edit it later in login action ????

3- put what I want in it with $sessionObject->memberID = 'member ID';

4- use the array anywhere by two means :
a- put in registery.
b- make another instance of session_namespace and it will have the first instance array

If something I didn't understand it correctly , please tell me
--> really I can't understand the second part of the last comment (from In a way its the Singleton pattern....)

thank you again, I'll update my code [/u]
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

I'll answer yes to all the above ;). It's not very hard, but yes the manual is a bit vague at times. It's the intention after releasing ZF 1.0 to move more towards writing tutorials and examples to complement the briefer technical documentation.

The singleton comment - it means that every call to "new Zend_Session_Namespace('default')" will return the same original "default" object. It's like having code that uses "getInstance()", i.e. the Singleton Design Pattern. The ZF uses a few Singletons like the Zend_Controller_Front::getInstance() method among others.

Glad to be of help! :)
User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

Post by the_last_tamurai »

thank you `Maugrim` --> in my language (arabic) your name is .......:? if you want to know tell me :) ( not good )

about grabing the session I tried the two ways but just the second working (using new instances )
I can't register the object.....I'm sure it's my fault but where I don't know
but anyway It's working now...

I have another small question....
in membersController for example...should I make a new instance of the session_namespace in every action??
if yes, is this affect performance????????
Post Reply