Forum Moderators: coopster

Message Too Old, No Replies

Correct handling of multiple sessions

Method works – but is it the best way?

         

kiwibrit

10:45 pm on Nov 10, 2005 (gmt 0)

10+ Year Member



For accessibility reasons I have replaced data being derived from a javascript triple drop down menu on a form feeding to a php page for processing. The replacement method uses a series of pages. A form with radio buttons in an htm page feeds into a similar php page where a session is created - and feeds through to another similar page where a second session is created – with the posted information and the 2 sessions being fed to the final php page for processing.

I did not want to use viewers’ cookies – they are often disabled, I’m told.

It works. But I am brand new to sessions, and wondering if I have done this correctly.

Page1.htm contains a normal radio button form, which posts ”size”.

Page2.php has the following at the very top (pre Doctype):


<?php
session_name('thisn');
session_name('whattype');
session_start();
header("Cache-control: private"); // IE 6 Fix.
?>

In the body:


$_SESSION['whattype'] = $_POST['whattype'];

The form from Page2.php posts a value “whatsize”.

Page3.php starts:


<?php
session_name('whattype');
session_name('whatsize');
session_start();
header("Cache-control: private");
?>

In the body


$_SESSION['whatsize'] = $_POST['whatsize'];

Page4.php again starts:


<?php
session_name('whattype');
session_name('whatsize');
session_start();
header("Cache-control: private");
?>

The data is processed in the body, with the conclusion:


session_destroy();

Which, I hope, wipes all session information.

Am I handling the sessions correctly?

jatar_k

3:51 pm on Nov 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



why create the second session? why not just add information to the already existing one?

kiwibrit

9:48 pm on Nov 12, 2005 (gmt 0)

10+ Year Member



Thanks for coming back to me.

The part of the final page that processes the data can use a built-up single session. But there is also text in the last page that calls up the individual variables posted in the earlier pages.

jatar_k

6:43 am on Nov 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could essentially add all of that to the single session

unless there is something I don't understand

Anyango

8:41 am on Nov 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I might be totally wrong in this assumption.

That said, i think kiwibrit is confusing "session" with "session variable". with one client at a time, you usually and more then 90% of the time establish only one session at a time, however one session can have any number of session variables as and when needed. So basically as Jatar_k has said, you could register all variables with one session without even needing to name session or this n that.

And just for information, even if you are seemingly assigning different names to the session, the basic session remains the same, try

session_id()

it will return same throughout your current code, that means those arent different sessions, thats one session.

so thats essantially one session with multiple session variables.

<?
session_start();
$_SESSION['var1']="anyvalue";
$_SESSION['var2']="anyvalue";
$_SESSION['var3']="anyvalue";
$_SESSION['var4']="anyvalue";
$_SESSION['var5']="anyvalue";
// and so on
?>

as far as your code is concerned, that looks perfectly fine to me, just that if i would write it, i wouldnt use

session_name('whattype');
session_name('whatsize');

no need for em.

you can simply use

$_SESSION['whattype']="anything" //as you have done.

I am sorry if this doesnt help.

Regards
Kami

kiwibrit

11:49 am on Nov 13, 2005 (gmt 0)

10+ Year Member



Anyango, you spotted my ignorance beautifully. Yes, I think that knocks it on the head.

Thanks very much to you and jatar_k.