PHP Sessions
Page 1 of 1
sTo0z
[Moderator] Babysitter



Posts: 7449
Location: USA
PostPosted: Mon, 31st Jan 2005 08:36    Post subject: PHP Sessions
Can anyone send me to a good php session tutorial? I've googled but I haven't found any good ones...

I just want to restrict people from visiting pages unless they are logged in. As far as I know, this is what sessions are going to do for me.

I've done all the registration and login, except for starting and maintaning sessions, and using them to block a page unless being logged in.

Any help in this matter would be much appreciated (either info or a link to a nice tutorial/article/book).

Thanks in advance.


Back to top
[mrt]
[Admin] Code Monkey



Posts: 1338

PostPosted: Mon, 31st Jan 2005 14:34    Post subject:
www.php.net - Sessions Online Manual. It has everything you need. Basicly you need to start/restart the session at the begin of every php file by using session_start(). After that you can freely do what ever you want with the $_SESSION super-global. There is one more little thing tho, you need to keep passing the session id to every request either via the URL parameters or cookies. Read up on the page i pasted above.

As for the implant-ation...there are a number of ways todo this, the easiest way would be to set a variable when a user logs in and checking it where needed then doing the appropriate action (either restrict him or display the page).


teey
Back to top
sTo0z
[Moderator] Babysitter



Posts: 7449
Location: USA
PostPosted: Tue, 1st Feb 2005 02:13    Post subject:
I am using the send cookie to users method.

Still not sure how to use the session id to my advantage... :\

The problem is I don't know how to reject a user that doesn't have a good session....

Any variable I create and pass will only exist if I pass it.

So if they try to get to the page without a session, the PHP will error with an unidentified variable, because it was not passed from a session.


Back to top
[mrt]
[Admin] Code Monkey



Posts: 1338

PostPosted: Wed, 2nd Feb 2005 00:29    Post subject:
//
// Login.php
if(LoginSuccessfull)
$_SESSION['loggedin'] = TRUE;
else
<do something else...>


//
// SomeProtectedFileThatNeedsALogin.php
if(!isset($_SESSION['loggedin']) || !$_SESSION['loggedin'])
<bitch to user>
else
<let him in>


Thats about it heh. You bypass that undefined error with calling isset() on it first.
Back to top
sTo0z
[Moderator] Babysitter



Posts: 7449
Location: USA
PostPosted: Wed, 2nd Feb 2005 01:39    Post subject:
lol damn, I swear I did that... oh well, thanks mrt!


Back to top
desiredfx




Posts: 27

PostPosted: Sat, 9th Apr 2005 20:02    Post subject:
chk your email ....
Back to top
toeffy
VIP Member



Posts: 1501

PostPosted: Sun, 10th Apr 2005 01:51    Post subject:
make sure to set the cookie first thing...even before you start rendering the html.


jesus christ was a gangsta rapper. they killed him. he came back and made a platinum album.
Back to top
sTo0z
[Moderator] Babysitter



Posts: 7449
Location: USA
PostPosted: Sun, 10th Apr 2005 03:20    Post subject:
desiredfx wrote:
chk your email ....


Hah, thanks, checkin' 'em out now. Smile


Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group