views:

93

answers:

3

Thanks a lot and sorry for bothering.. I'm still a PHP rookie, I am having a few problems trying to understanding/finding out how to keep multi session live together works.. here my existing sessions e.g.

session_start();
// If no session exists, create one
if (!session_is_registered('lesson')) {
    $_SESSION['lesson'] = new lesson;
} 
$lesson = $_SESSION['lesson'];

and then i want to add new session from code at below:

if (!isset($_POST['languageselect'])) { 
    $languageselect = $standardlanguage; 
} else { /* set standard language */
    $languageselect = $_POST['languageselect']; 
} //endif
if($languageselect == 1) {  /* My Language Pack */
    $languagearray = array (
1=>"one",
2=>"two");
} else  {   /* English Language Pack (=standard) */

    $languagearray = array (
1=>"one",
2=>"two");

function printoptionbox($boxname, $cssclass, $elementsarray, $kataktiv=1) {
    echo "<select name='$boxname' class='$cssclass'>";
    while (list($key,$value) = each($elementsarray)) {
        if ($key == $kataktiv) {    
            $SELECTED = "SELECTED";
        } else {
            $SELECTED = "";
        } //endif
        echo "<option $SELECTED value='$key'>$value</option>";
    } //endwhile
    echo "</select>";
} 

Here the HTML code from dropdown language options:

<td class="arial" width="210"><?php printoptionbox("languageselect", "languageselect", $language_array, $kataktiv=$languageselect); ?></td>                                         <input type="submit" name="languageselectsubmit" value="OK" width="30" style= "width: 30px; font-size:10px" class="submitbutton">

but i most confuse, how to create the session from language code above and then would be work for another page (different code), because the multiple language options just work in first page but will resetting for next page... Thank you so much for any advice on this

+1  A: 

First of all, I am not entirely sure you fully understand the concept of session and how it works on PHP. Session in PHP refers to all variables (language, lesson, etc.), not to single variables. So what you really are looking for is not how to keep "multi session live", but rather on how to "store multiple variables into a session".

You can save multiple variables into a single session by saving them as keywords inside $_SESSION. For instance you can save language setting to session by invoking $_SESSION['lang']='en';. Similarly you can add "lesson" variable to session by invoking $_SESSION['lesson']=1;.

I would strongly suggest reading through a tutorial on how PHP sessions work. For instance, take a look to PHP Sessions on w3schools.com.

Taking it to the level of your code, there are several points that you could consider taking a look into:

  • Before calling printoptionbox, make sure you have initialized the session by calling session_start();. Based on your code examples that is not entirely obvious. If you don't call session start, you will not be able to restore the correct variable value.
  • Make sure to pass the value of the variable to printoptionbox from $_SESSION.

As a quick test you could try replacing your call with:

<?php printoptionbox("languageselect", "languageselect", $language_array, $_SESSION['language']); ?>

Also:

  • Make sure you save the language selection from user to $_SESSION!

As a side note:

  • Use of function session_is_registered may potentially also cause problems, since it has been deprecated in the latest versions of PHP. Also, you can replace it straight away with isset instead:
// If no session exists, create one
if ( isset($_SESSION['lesson']) ) {
   ...
}
jsalonen
Thank You Jaakko... hm... i am sorry if my explanation just confuse you... but please feel free to read my all last comment and hope you will know what i mean..
jones
I completely re-edited my answer. Please, see if it helps you more!
jsalonen
+2  A: 

I feel like maybe I didn't quite follow your intention here, but I want to mention that 1) you will not be able to maintain two sessions at the same time and 2) I don't think you would really need to anyhow.

The session can store array data to be retrieved later. If you are sure you want to store that data in session then just add your array and draw it back out when needed. What you need to do is write your code so that if they choose a different language the new language is swapped out in the session by overwriting the old array.

You may find it easier (as I have very recently) to store your language arrays in flat files serialized on the server and then draw them out based on users preferences. This is very low on overhead, very quick, and you gain the ability to 'cache' your language so that there is little to no processing needed.

angryCodeMonkey
Hi Shane.. i Welcome you for joining my page, much appreciated.. i try to tell all in the clearly way. i hope you catch what i mean. but i am sorry if it still not clear yet for you. Session of $lesson = $_SESSION['lesson']; instead already works.. visitor able to choose what kind of lesson they want from beginning page, and then i try to prepare multiple languages options for the lesson, by create the code for it. so visitors have a chance to change depend on their language in any page.. for now my code above work only in a page but when go to next page the language will be reset.
jones
have any idea depend on my code already exist? it would be welcome and thanks a lot..
jones
+2  A: 

I think you misunderstood the concept behind the $_SESSION global. It's really just a global associative array that is used to store values between different requests.

Everything you put into that global during execution will be saved until the next request takes place or the session times out.

So, consider this example:

// file1.php
session_start();

$_SESSION['lesson'] = new lesson();

// file2.php
session_start();

$_SESSION['language_code'] = 1;

// file3.php
session_start();

// dumps both values we packed into the session from the previous requests
var_dump($_SESSION);
aefxx
Hi, ok nice advice to assist me..by following your samples code above can i insert them in one page together? e.g <? // file1.phpsession_start();$_SESSION['lesson'] = new lesson();$_SESSION['language_code'] = 1;?>and then call them in another page e.g.<? //page2.phpsession_start();var_dump($_SESSION);?>the $_SESSION['lesson'] = new lesson(); has fixed set in page1.php and the should be able destroy let say in page4.php.but unlike $_SESSION['language_code'] = 1; it's must be able call in any page. that mean my visitor have any chance to change the language in any page they want..
jones
btw tHank for your intentions, i Much Appreciated.
jones
@jones: Yes you can do anything you want to with the $_SESSION global. Every value you put into it will be saved with "the session" and once you don't need a value anymore, you simply unset it in the $_SESSION global.
aefxx