Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Is this right about writting sessions
Closed Thread
Old 02-06-2007, 01:35 AM   #1 (permalink)
 
Super Techie

Join Date: Aug 2004

Posts: 277

real_in

Send a message via Yahoo to real_in
Default Is this right about writting sessions

well guyss.. i am learning php these days and i was planning to go on with sessions..

i donn know waht is confusing me in sessions..
till now what i got is, when a if condition is satisfied, i have to assign a session variable for which i can let user browse as many pages until his session is valid ..

well now practically saying i m unable to do so.. a kind of pseudo code is :-

if (username and password = true)
then

start_session();
$_SESSION['id']=SESSIONID();

ELSE

redirect login-fail.php
ENDIF


Now when i goto next page..

what do i need to do ???

i mean do i need to include this session variable in some file or cookie or URL ??

i started with my first attempt my including session.php file in every page which i want user to browse..

yeah yeah i know that was the dumbest thing i tried, cause anyone directing to that page, gets authenticated cause session script exectues..

now please gimmie a quick response.. i cant find it in some tutorials.

or i m not able to understand..

thanks and regards..

Realin !
__________________
Realin Wuz here ...

real_in is offline  
Old 02-06-2007, 02:26 PM   #2 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

I'm not sure of the exact terms, but you do not need to store the session ID generally speaking, unless the user has cookies disabled, as PHP automatically uses them to store the session id, and if the user has 'em blocked - you'd then need to send it either via the _GET or _POST methods (however, that isn't the case here!).

In order to access the session information you previously created, all you have to do is re-start the session,
---> session_start();
before trying to access any information from it (your variables). A lot of the time, people use the session on every page as a means to pass data, and not just base it around a login. Like...you could set a session variable determining if the user is logged in (but that's beyond the point)...anyway, sorry for the rambling!

The more desired example:

index.php:
Code:
<?php
session_start();
$_SESSION['Hello'] = 'Do I know you?';
?>
then after changing or refreshing pages -

test.php:
Code:
<?php
session_start();
echo $_SESSION['Hello']; // Do I know you?
?>

__________________
Vormund is offline  
Old 02-06-2007, 06:03 PM   #3 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 164

Skuzzle

Default

basically you could use

session_start();
$_SESSION['data'] = $data;

for the assigning to the session variable

Then the next page to retrieve would go

session_start();
$data = $_SESSION['data'];

Tis that simple.
The session_start used to confuse me as I was like.. no i want to continue a session, not start a new 1, but its just the syntax.
__________________
Skuzzle - Powered by Badgerbyte Web & IT Services
www.drive4fun.co.uk check it out
Skuzzle is offline  
Old 02-06-2007, 10:25 PM   #4 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

Quote:
Originally posted by Skuzzle
The session_start used to confuse me as I was like.. no i want to continue a session, not start a new 1, but its just the syntax.
No, it's the magic of PHP! The most user-friendly scripting language in existence.
__________________
Vormund is offline  
Old 02-10-2007, 11:33 AM   #5 (permalink)
 
Super Techie

Join Date: Aug 2004

Posts: 277

real_in

Send a message via Yahoo to real_in
Default

sorry for the very late reply..

but i wanted to ask, suppose i set session variables in the index.php and do i need to include this index.php in everypage where i need session data ?? but if i will do that then it will let the user to directly point onto that page and generate the data..


also i want to know that why i need to generate session IDs ?
i mean is it any way mandatory to generate them ? and do i need cookies or query string in URL to maintain them, or it is jus session_start which gonnna do eevry thing..

if session_start gonna do all, then why i need cookies
__________________
Realin Wuz here ...

real_in is offline  
Old 02-10-2007, 11:41 AM   #6 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

Quote:
Originally posted by real_in
if session_start gonna do all, then why i need cookies
You don't...I just said way more then I should, so please ignore that part...sorry!

Quote:
Originally posted by real_in
but i wanted to ask, suppose i set session variables in the index.php and do i need to include this index.php in everypage where i need session data ?? but if i will do that then it will let the user to directly point onto that page and generate the data..
No, you don't need to include the index.php page. To use sessions, all you need to do is this:

On every page, put this at the top: session_start();

As long as you have that, you can always set and access variables, for example:
On a login page you might do this:
$_SESSION['username'] = 'Fred'; // sets the variable

Then on every page after, you might want their username, so you'd get it like this:
$username = $_SESSION['username']; // gets the username

PHP will handle all session-related details for you, as long as you have the session_start(); at the top!

Quote:
Originally posted by real_in
also i want to know that why i need to generate session IDs ?
i mean is it any way mandatory to generate them ? and do i need cookies or query string in URL to maintain them, or it is jus session_start which gonnna do eevry thing..
You don't need generate session IDs, PHP will do that for you. session_start(); will do the trick. Try it out...PHP is magical like that.
__________________
Vormund is offline  
Old 02-13-2007, 10:40 AM   #7 (permalink)
 
Super Techie

Join Date: Aug 2004

Posts: 277

real_in

Send a message via Yahoo to real_in
Default

Hey thanks a lot mann.. all clear
__________________
Realin Wuz here ...

real_in is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On