Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Session Management in PHP4: A Followup
Example 2 (page2.php):

<?php

session_start
();
print
"Value of 'my_session_variable': $my_session_variable";

?>
Try this out, you'll see that the variable kept it's value across pages!
Using session variables for authentication in conjunction with a database is just as simple. Create a login-page gives the user a userid and password form and posts to another PHP page (this example uses mysql):

<?php

session_start
();

if (@
$userid && @$password) {
    
$res = @mysql_query("SELECT userid FROM users WHERE userid='$userid' AND password='$password'");
    if(@
mysql_num_rows($res) != 0) {
        
$verified_user = $userid;
        
session_register("verified_user");
    }
}

Header("Location: your_main_page.php");

?>
Now, on 'your_main_page.php', you call session_start() and then you can check the verified_user variable to see if the user has been authenticated (and who he is). As simple as that.
There's alot more uses for session variables though, I use it for letting the load of my database by caching certain values in the session rather than reading them from the database on each page access.
Another small addition to my previous article: You can store objects in session variables as of Beta 3 of PHP4. Several people pointed this out to me, so I thought I'd tell you all.

[Page 1]  [Page 2]  


Comments:
help regarding coverting session variablevishal verma12/22/05 05:59
RE: Ending a Sessionsteve08/07/05 22:20
RE: Session start ProblemLailey07/13/05 10:27
RE: session destroy not working with back buttonFrancis06/08/05 06:00
session thru frameunni03/14/05 07:14
RE: Session start ProblemJaiprakash03/01/05 05:24
RE: Warning: Page has Expired / Data MissingDharmendra Ajwani01/06/05 06:19
help regarding sessionAmudhan11/30/04 01:20
help regarding sessionAmudhan11/30/04 01:17
Windows php.ini for session_start() problemPedro Moreira12/19/02 18:03
RE: Warning: Page has Expired... Solutioncoled12/12/02 13:19
RE: Cannot send session cache limiter - headers lnguyen11/27/02 02:45
RE: Cannot send session cache limiter - headers darsoft11/15/02 22:54
RE: Warning: Page has Expired... Solutionraf10/11/02 05:25
Querying sessionsMark Kessler10/10/02 11:17
UPDATE CANN'T GO TO MYSQL DATABASEMary Pei10/04/02 05:38
RE: Ayudano!MaD09/22/02 16:04
RE: Ayudano!MaD09/22/02 15:42
AyudaAdalberto09/18/02 16:26
Cannot send session cache limiter - headers Baskar09/16/02 02:35
security an e-learning sitetutus09/09/02 11:17
Why wont this workEric Mulindwa08/29/02 10:58
RE: Php SessionNeeraja08/27/02 15:33
RE: Php SessionNeeraja08/27/02 15:28
RE: What this error message means..Trio Yulistianto08/24/02 05:25
RE: What this error message means..Trio Yulistianto08/24/02 02:06
RE: error messages when starting sessionGreg08/15/02 06:29
session handlingsss08/13/02 01:05
RE: Sessions and Browser HistoryJohn McBride08/12/02 07:18
RE: What this error message means..Do mInh Duc08/08/02 04:02
What this error message means..zainal tajuddin08/05/02 04:00
RE: session destroy not working with back buttonDo Minh Duc07/30/02 01:57
RE: session destroy not working with back buttonDo Minh Duc07/30/02 01:52
RE: session destroy not working with back buttonbav07/24/02 04:55
session destroy not working with back buttondsamsom07/10/02 13:36
RE: error messages when starting sessionThyme07/04/02 16:57
SESSION IDS??? confusion!!!blaksaga06/28/02 14:27
problem with setcookie()rajan kumar lamichhane06/28/02 02:15
error messages when starting sessionmike06/01/02 12:21
RE: Php SessionCorey05/30/02 14:35
RE: Php SessionIsrafel05/16/02 20:23
sessions and objectsKretsu05/08/02 06:50
cannot access session varOlli04/16/02 13:26
RE: Php SessionCarlos04/16/02 04:37
RE: this isn't secureWood04/15/02 13:38
RE: Warning: Page has Expired... SolutionBarak Shohat04/08/02 23:34
RE: SessionTimeoutMarcel Schindler04/05/02 07:48
help me ,please!shakespeare03/28/02 03:10
<delete session file>ecuboy03/18/02 07:02
RE: session.save_pathFabien02/22/02 05:36
RE: session.save_pathFabien02/22/02 05:34
session always starts...Crow02/20/02 18:26
SessionTimeoutdavood02/20/02 06:31
RE: ...also with frames ?Juan 02/19/02 20:41
...also with frames ?Oliver02/19/02 04:48
problem with sessionspralay desai02/19/02 00:50
problem with sessionspralay desai02/19/02 00:37
RE: Warning: Page has Expired / Data Missinghongshu02/14/02 09:23
RE: Problem with sessionsAlessandro02/11/02 16:10
RE: Session cache limiter problemfongming02/08/02 11:36
Session cache limiter problemCelso Correa02/05/02 13:15
Session problems with framesPete01/30/02 13:36
RE: Can't see value of registered variableNikhil01/25/02 02:34
TimeoutGreg01/24/02 06:16
RE: getting some warning messages in SessionTuan01/22/02 13:38
RE: Warning message in sessionsTuan01/21/02 15:12
RE: Problem with sessionsTuan01/21/02 14:43
getting some warning messages in SessionTuan01/21/02 14:17
RE: Php SessionMatthew Black01/18/02 17:18
logout problemPhlow01/16/02 07:30
RE: sessions and PHPEdmunir01/05/02 07:06
RE: SessionTimeoutDylan McNamee01/02/02 17:37
RE: SessionTimeoutDylan McNamee01/02/02 17:36
sessions and PHPEdantonio01/02/02 05:58
RE: unable to call function to set session?Gerald12/10/01 06:17
RE: Session_destroy doesnt work!check11/30/01 05:09
RE: Can't see value of registered variableJem11/21/01 01:25
RE: SessionTimeoutMarco Gonnelli11/16/01 12:08
unable to call function to set session?Andy11/07/01 14:34
unable to call function to set session?Andy11/07/01 14:34
2 browsers in one session?A.Sambuu10/28/01 11:02
RE: Session_Save_PathNeil Freeman10/17/01 04:44
RE: Session_Save_PathTipper Gore10/11/01 06:48
Session_Save_PathJohn Dietz10/09/01 15:30
good session example by phpMunir Abbas10/08/01 08:30
good session example by phpMunir Abbas10/08/01 08:06
RE: use a class as a wrapper for sessions?Tipper Gore10/08/01 06:03
RE: Big Errors What am I doing wrongTipper Gore10/08/01 05:42
only one user and passwordmatos10/04/01 13:58
Big Errors What am I doing wrongBen09/29/01 20:32
RE: use a class as a wrapper for sessions?Matt Giddings09/27/01 17:44
how to registere a variable in sessionSelvakumar09/15/01 07:24
RE: use a class as a wrapper for sessions?Tipper Gore09/13/01 03:44
RE: IT doesnt workR.C.09/11/01 07:08
use a class as a wrapper for sessions?Tipper Gore09/11/01 06:18
RE: Storing Objects in SessionsNathan Smith09/07/01 00:51
RE: Can't get Session Variables + WMLLance Winter08/30/01 03:18
RE: Can't get Session Variables + WMLLance Winter08/30/01 03:18
RE: Can't get Session Variables + WMLLance Winter08/30/01 03:18
RE: Can't get Session Variables + WMLnur08/29/01 04:33
RE: Can't get Session Variables + WMLnur08/29/01 04:33
RE: Can't get Session Variables + WMLnur08/29/01 04:27
RE: Can't get Session Variables + WMLnur08/29/01 04:27
RE: Can't get Session Variables + WMLnur08/29/01 04:27
RE: Can't get Session Variables + WMLnur08/29/01 04:27
RE: Can't get Session Variables + WMLnur08/29/01 04:27
Solution for session_start() problemSam07/27/01 08:11
Solution for session_start() problemSam07/27/01 08:11
Check variable (security)Marius07/18/01 05:26
Determine session exitGregory Thognard07/18/01 01:28
infinit loop problemFlorin Covaciu07/16/01 07:35
infinit loop problemFlorin Covaciu07/16/01 07:35
Warning: Page has Expired / Data MissingPatrick07/06/01 10:45
Can't get Session Variables + WMLDennis Bath06/20/01 19:53
RE: Session variable not recognizedDennis Bath06/20/01 02:21
Session_destroy doesnt work!Paul Weber06/18/01 10:09
RE: Can't update session vars Cornel Alexa06/16/01 11:30
RE: Can't update session vars Cornel Alexa06/16/01 11:23
Storing Objects doesn't workVolker Zoepfel06/16/01 11:13
RE: Can't update session vars Marco Bleeker06/16/01 07:13
Can't update session vars Marco Bleeker06/15/01 07:16
Warning: Page has Expired / Data MissingLee Hamblin06/14/01 06:36
Warning: Page has Expired / Data MissingLee Hamblin06/13/01 09:06
SecurityJim06/09/01 07:54
RE: this isn't secureJeremy Lunn06/04/01 05:58
session_register() and Netscape 4.xJennifer06/01/01 02:30
how spellcheck in phpashutosh05/25/01 21:45
PHPLIB session managment function in PHP4Ken05/25/01 19:08
PHPLIB problemWANG05/23/01 19:23
php4 session when php4 runs as cgiRolando05/23/01 06:52
This article doesn't really help muchJustin Flavin05/21/01 05:17
RE: unable to update session vars.Chunhui05/16/01 10:58
RE: Session variable not recognizedChunhui05/16/01 10:53
just cookies, RE: IT doesnt workChunhui05/16/01 10:45
SessionTimeoutHugo05/15/01 01:32
unable to update session vars.Thijs Volders05/12/01 09:42
RE: this helpedRoy Walter05/09/01 08:19
this helpedswativ05/07/01 17:05
keep vars trough pagesbozhan04/28/01 09:10
RE: Problem with sessionsKeith Nasman04/27/01 14:46
Problem with sessionsMax04/27/01 08:53
RE: session_start and ob_start => CONFLICTLarry04/20/01 22:02
Session unregister problemC.M.Lee04/18/01 07:40
RE: session_start and ob_start => CONFLICTEric Blade04/13/01 23:33
RE: HELP NEEDED session_start() Manoj04/12/01 11:07
RE: Session ProblemSonOfDad04/05/01 08:36
session_start and ob_start => CONFLICTjohan chauvet04/04/01 12:24
sessions-register_globalsoscar a04/04/01 08:41
Php SessionChunlai Chai`04/02/01 20:51
Session ProblemChunlai Chai`04/02/01 20:44
Session ProblemChunlai Chai`04/02/01 20:42
Can't see value of registered variableKevin03/30/01 06:55
RE: I am stuck on this one and it is URGENT!!Eric Blade03/26/01 12:58
Help plz with sessions (newbie)insight03/21/01 07:15
RE: Session start ProblemDave Forrest03/19/01 21:16
RE: Session variable not recognizedDave Forrest03/19/01 21:05
Session variable not recognizedWendell Myers03/17/01 16:29
Cookie error when performing Session_start()Andrew Proud03/16/01 03:47
Storing Objects in Sessionsgaston03/13/01 19:25
Dumb questionPeter Manchester03/12/01 07:36
RE: Does Php&Apache work with sessions on Win'98?Miguel Canedo03/08/01 00:45
Session start ProblemYogesh Patel03/07/01 02:12
RE: I am stuck on this one and it is URGENT!!dan donaldson03/06/01 10:47
RE: Session managementKermit03/05/01 14:58
sessions in PHP4sunitha03/04/01 22:28
Session managementsunitha03/04/01 22:24
RE: I am stuck on this one and it is URGENT!!!Proteus03/01/01 17:37
I am stuck on this one and it is URGENT!!!Fotis Paraskevopoulos02/17/01 12:59
Sessions and Browser HistoryDavid Hamdan02/12/01 11:38
Sessions and Browser HistoryDavid Hamdan02/12/01 11:30
script not working!carlos02/07/01 06:54
just cookies, RE: IT doesnt workPeter02/05/01 13:41
RE: Warning !!!!!!!Pubert01/31/01 20:17
RE: Warning !!!!!!!Pubert01/31/01 20:17
RE: Warning !!!!!!!Nicole01/31/01 16:05
Warning !!!!!!!Narayanan01/29/01 23:57
RE: HELP NEEDED session_start() laurent01/29/01 07:59
RE: HELP NEEDED session_start() laurent01/29/01 07:55
HELP NEEDED session_start() Tabish01/25/01 09:22
Sess. don't work across folders of same levelNeville01/22/01 23:00
RE: Ending a SessionLucas01/16/01 11:39
A little mistake that may cause big problemsDJ Rabbit01/16/01 09:32
RE: passing html form variables to your session ?smaical01/16/01 09:14
Session Problems: ONE possible SOLUTIONsmaical01/16/01 08:52
RE: IT doesnt workGhita Voda01/16/01 08:48
passing html form variables to your session ?neil bromehead01/12/01 03:47
RE: ARRGHH: i get an session errorzephir01/09/01 18:22
ARRGHH: i get an session errorWim Fikkert01/08/01 12:31
RE: I can hack session var @ URLNeville 12/27/00 01:02
I can hack session var @ URLdoc12/23/00 23:23
RE: session_start() gives an error messageMagnus12/21/00 10:18
RE: session_start() gives an error messageMagnus12/21/00 10:18
Not assigning value to a reg'd varCass Surek12/20/00 11:32
Not assigning value to a reg'd varCass Surek12/20/00 10:46
RE: session_start() gives an error messagetool12/19/00 19:58
Ending a Sessionloconet12/19/00 17:17
Ending a Sessionloconet12/19/00 17:17
Does Php&Apache work with sessions on Win'98?Adam Ulacha12/18/00 12:49
Does Php&Apache work with sessions on Win'98?Adam Ulacha12/18/00 11:43
RE: session_start() gives an error messagePhuong Nguyen12/16/00 09:10
session_start() gives an error messageFilipe Lopes12/15/00 09:23
RE: PHP4 Bug? Nothing else! Martin Schranz12/08/00 13:36
Sessions and checkboxesMichelle Grant Richardson12/06/00 12:17
RE: It works IF....Jason Dumler12/04/00 14:46
session.save_pathWeihong Hendricks11/29/00 11:16
RE: this isn't secureDavid11/22/00 13:44
RE: this isn't secureDavid11/22/00 11:47
RE: Not relying on cookiesMatthew Tippett11/21/00 01:17
session_register() and variable declarationDariusz Grabka11/17/00 11:17
RE: IT doesnt workCesare Quadalti11/09/00 18:10
Not relying on cookiesGera Makarov11/05/00 00:58
RE: this isn't secureJeff Lambert10/31/00 13:19
just cookies, RE: IT doesnt workEd Gerck10/28/00 23:29
RE: It works IF....Jason Lavigne09/26/00 11:32
It works IF....Christopher Aillon09/03/00 22:44
PHP4 Bug? Nothing else! Nick Semenov09/01/00 23:14
RE: IT doesnt workNick Semenov09/01/00 22:00
RE: IT doesnt workHazel Bridge08/24/00 09:07
RE: IT doesnt workVincent08/23/00 04:21
IT doesnt workAthaur Rahman08/18/00 15:42
Its not working Athaur Rahman08/18/00 15:37
RE: this isn't secureDon Prior08/17/00 10:55
this isn't secureJake Hofman08/15/00 02:58
these scripts don't work as advertised!Joseph Vaughan08/07/00 16:19
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.