Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8

Thread: Sessions

  1. #1
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    This one is a little hard to explain...

    I have a multiple language website and on the index page a session is started and the variable $_SESSION['lang'] is given the value 1 (English). There are flag images in the header (which is included on all pages) to change the language.

    Now, I have two questions...
    1. How do I alter the value of the session variable?
    2. If the user goes back to the index page, will the variable be set back to 1?


    Thanks, I hope I have made some sense.
    JR

  2.  

  3. #2
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    You should first check in your index file that the language has not yet been set. If that is true, then you can assign it 1.

    PHP Code:
    session_start();
    .
    .
    .
    if(!isset(
    $_SESSION['lang']) 
        || !
    in_array($_SESSION['lang'], array(1,2,3,4,5,6,7)) )
    {
      
    $_SESSION['lang'] = 1;

    It just checks to see if 'lang' is not set or it is not a valid language, if either one of them is true, then set the language to 1.
    There and Back Again :Ogre:

  4. #3
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Thanks.

    Could you please explain to me what...

    PHP Code:
    || !in_array($_SESSION['lang'], array(1,2,3,4,5,6,7)) ) 
    ..is all about?
    JR

  5. #4
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    That is just checking that the language currently set is a valid language.

    in_array takes two parameters, a non-array variable and an array, and searches for single value within the array and returns true if found and false if not. This is just a convinient way to check whether something is with a list of acceptable values.
    There and Back Again :Ogre:

  6. #5
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Right thanks...

    Now the tricky part...

    I was thinking of linking the flags to a PHP page where there is some code that changes the value of the variable. I am not quite sure how to do this, would it just be the same as changing a value for a normal variable?
    JR

  7. #6
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    Yup, it is same as if you were updating a normal variable. e.g. $_SESSION['lang'] = 4; However, make sure that session has been started using session_start() before you try to update session variables.
    There and Back Again :Ogre:

  8. #7
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    All done, thanks for the help. I am sure I coulda figured that one out though, but you still get the 10 TD for pointing me in the right direction
    JR

  9. #8
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    LOL thanks
    There and Back Again :Ogre:


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:38 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com