[Is this right about writting sessions] - Computers



Search Tech-Forums - link takes you to our Forum's search page.

Note: The following is only a text archive!


To view the actual forum discussion, please visit our website at http://www.tech-forums.net

Pages:1



Is this right about writting sessions

(Click here to view the original thread with full colors/images)



Posted by: real_in

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 !



Posted by: Vormund

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!

[i]The more desired example[/i]:

index.php:
[code]<?php
session_start();
$_SESSION['Hello'] = 'Do I know you?';
?>[/code]

then after changing or refreshing pages -

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



Posted by: Skuzzle

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.



Posted by: Vormund

[QUOTE][i]Originally posted by Skuzzle [/i]
[B]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. [/B][/QUOTE]

No, it's the magic of PHP! :) The most user-friendly scripting language in existence.



Posted by: real_in

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



Posted by: Vormund

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

[QUOTE][i]Originally posted by real_in [/i]
[B]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.. [/B][/QUOTE]
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][i]Originally posted by real_in [/i]
[B]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.. [/B][/QUOTE]
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. :)



Posted by: real_in

Hey thanks a lot mann.. all clear :)





vBulletin Copyright ©2000 - 2003, Jelsoft Enterprises Limited.


PPC Management
vB Easy Archive Final - Created by Xenon