Sei sulla pagina 1di 6

LearnDooPHP

Login Home About Contact Forum SUBSCRIBETORSS LearnDooPHP:HighperformancePHPframework 22Sep2010

byMilosKovacki 9Comments Demos&Snippets,Tutorials

HandlingsessionswithoutApachesessions
YouareallfamiliarthatApachesessionsareslowandsometimesinsecure.InthistextIwilltrytoimplementwayof handlingsessionswithallDoo::cachemechanismsthatareavailableinDooPhp. Sooursessionswillbestoredinsomeofthecachemechanisms.Firstwewillstartwithwrittingoursessionclassthat willstartandendsession,wewillsaveitinprotected/classfolder. HereisourSession.class
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . < ? p h p / * * * S e s s i o n h a n d l e r * * @ a u t h o r M i l o s K o v a c k i < k o v a c k i @ g m a i l . c o m > * @ c o p y r i g h t M i l o s K o v a c k i 2 0 1 0 < k o v a c k i @ g m a i l . c o m > * / c l a s s S e s s i o n { p u b l i c s t a t i c $ _ s e s s i o n I d = N U L L p u b l i c s t a t i c $ s e s s i o n = a r r a y ( ) / * * * S t a r t s e s s i o n * / p u b l i c s t a t i c f u n c t i o n s t a r t S e s s i o n ( ) { s e l f : : $ _ s e s s i o n I d = ( i s s e t ( $ _ C O O K I E [ ' s e s s i o n _ i d ' ] ) ? $ _ C O O K I E [ ' s e s s i o n _ i d ' ] : N U L L ) i f ( ( ! s e l f : : $ _ s e s s i o n I d ) | | ( ! ( s e l f : : $ s e s s i o n = D o o : : c a c h e ( D o o : : c o n f ( ) > s e s s i o n C a c h e T y p e ) > g e t ( ' s e s s i o n _ ' . s e l f : : $ _ s e s s i o n I d ) ) ) ) { 2 0 . / / C r e a t e n e w s e s s i o n 2 1 . s e l f : : $ _ s e s s i o n I d = m d 5 ( $ _ S E R V E R [ ' R E M O T E _ A D D R ' ] . t i m e ( ) . r a n d ( 0 , 1 2 8 ) ) 2 2 . s e l f : : $ s e s s i o n [ ' i p ' ] = $ _ S E R V E R [ ' R E M O T E _ A D D R ' ]

2 3 . s e l f : : $ s e s s i o n [ ' c r e a t e d ' ] = t i m e ( ) 2 4 . } 2 5 . s e t c o o k i e ( ' s e s s i o n _ i d ' , s e l f : : $ _ s e s s i o n I d , ( t i m e ( ) + 3 6 0 0 * 2 4 * 9 0 ) , ' / ' ) 2 6 . } 2 7 . 2 8 . / * * 2 9 . * E n d s e s s i o n 3 0 . * / 3 1 . p u b l i c s t a t i c f u n c t i o n e n d S e s s i o n ( ) { 3 2 . $ s e s s i o n S t o r e d = D o o : : c a c h e ( D o o : : c o n f ( ) > s e s s i o n C a c h e T y p e ) > s e t ( ' s e s s i o n _ ' . s e l f : : $ _ s e s s i o n I d , s e l f : : $ s e s s i o n ) 3 3 . } 3 4 . 3 5 . }

YouallnoticedvariableDoo::conf()>sessionCacheTypesoweneedtoadditinourprotected/config/common.conf edititandadd:
1 . $ c o n f i g [ ' s e s s i o n C a c h e T y p e ' ] = ' a p c '

YoucanchooseanytypethatissupportedinDoo::cache,nowwewilleditourindex.php,hereismybootstrap:
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . < ? p h p / * * * B O O T S T R A P * / / / i n i _ s e t ( ' d i s p l a y _ e r r o r s ' , 1 ) i n c l u d e ' . / p r o t e c t e d / c o n f i g / c o m m o n . c o n f . p h p ' i n c l u d e ' . / p r o t e c t e d / c o n f i g / r o u t e s . c o n f . p h p ' i n c l u d e $ c o n f i g [ ' B A S E _ P A T H ' ] . ' D o o . p h p ' i n c l u d e $ c o n f i g [ ' B A S E _ P A T H ' ] . ' a p p / D o o C o n f i g . p h p ' # U n c o m m e n t f o r a u t o l o a d i n g t h e f r a m e w o r k c l a s s e s . f u n c t i o n _ _ a u t o l o a d ( $ c l a s s n a m e ) { D o o : : a u t o l o a d ( $ c l a s s n a m e ) } D o o : : c o n f ( ) > s e t ( $ c o n f i g ) # r e m o v e t h i s i f y o u w i s h t o s e e t h e n o r m a l P H P e r r o r v i e w . i n c l u d e $ c o n f i g [ ' B A S E _ P A T H ' ] . ' d i a g n o s t i c / d e b u g . p h p ' D o o : : a p p ( ) > r o u t e = $ r o u t e D o o : : l o a d C l a s s ( ' S e s s i o n ' ) S e s s i o n : : s t a r t S e s s i o n ( ) D o o : : a p p ( ) > r u n ( ) S e s s i o n : : e n d S e s s i o n ( ) ? >

AsyoucanseewestartsessionbeforewerunDooPhpappandthencloseitaftersoallvariableswechangeinsideour sessionarestoredincachemechanismasarray. Nowinsideourcontrollerwecandothis:


1 . S e s s i o n : : $ s e s s i o n [ ' u s e r n a m e ' ] = ' D o o P h p ' 2 . S e s s i o n : : $ s e s s i o n [ ' f o o ' ] = ' b a r '

Andeverythingwehadisstoredinsidecachemechanism,youaccesssessionasstaticarrayvariablesoyoucanuse issetandunsetfunctionstomanipulatewithsessionarraydata. ThisisgoodwaytoimplementfastsessionmechanismandpassusingApachesessions. Thankyouforreading.Pleaseaskquestions

9Comments
PreviousPost NextPost

athear 9Oct,2010

HiMilos, Imgettingthiserror: Fatalerror:Calltoundefinedfunctionapc_fetch()online42 Itslikemissingfunction,CMIIW.. thanks

James 13Oct,2010

IliketoknowmoreaboutDooPHPsessionifitcanbeusedtosecurelystoretemporaryencryptedstringlike userpassword/sessionandisnotaccessiblebyclients?

Proyb3 13Oct,2010

HowsecuredoesDooPHPhandlesession?Canthetemporarygeneratesessionkeybestoreinyourframework vsplainPHPsession?

MahmoudJalajel 21Oct,2010
Search...
Random Share JoinChat

Hi,itsaverynicepost!

CanyoudescribewhataretheproblemsintheApachesessionsthatthismethodwillsolve. Thanks

Danni 9Nov,2010

MyhostingcompanydoesnotsupportAPC,etc.IsusingPHPwouldbefine?e.g. $config['sessionCacheType']=php

Danni 9Nov,2010

AlsowhatsthedifferentwithDooSessionanyway?

MilosKovacki 15Nov,2010

Diffrenceisyoudontuseapachesessionsbutratheryoustoresessionvariablesinsideapccacheengine, youcanimplementanycacheengineyouwant.

JaromirMuller 22Nov,2010

Haveyouanyperformancecomparison?Whyshouldweavoidtoclassicapachesesionhandling?

MilosKovacki 21Dec,2010

Itsallabouttheperformance,thisisthemainreasonwhyIwrotethis. Itsmuchfasterwhenyouarehavinglargenumberofsessionsforexample20,000
Search...

OurSponsors ChooseLanguage
English Russian Deutsch Espaol Arabic Portugese

PopularTags

FindTutorial
Easy Normal Difficult

Categories
Demos&Snippets(4) News&Updates(2) Screencast(1) Tutorials(17)

RecentComments
kukatonIntrotoDooPHPslides(PHPMalaysiameetup2011) MilosKovackionUsingDooTranslatorfortranslation

WebdevGreeceonUsingDooTranslatorfortranslation MilosKovackionHandlingsessionswithoutApachesessions TonCreateasimpleToDoListinDooPHPPart2

Blogroll
DevelopmentBlog Documentation Forum GoogleCode Home About Contact Forum AllcontentscopyrightLearnDooPHP.Allrightsreserved.ThemedesignbyWebKreation.

Potrebbero piacerti anche