www.webdeveloper.com
Results 1 to 6 of 6

Thread: PHP - don't know how to retrieve session data

  1. #1
    Join Date
    Jun 2009
    Posts
    7

    PHP - don't know how to retrieve session data

    I'm trying to make a small script where if a user clicks on a desired language, the appropriate <div>'s inside the website will display:block; or display:none;

    The problem is, I would like to maintain the language the user has chosen, throughout his visit to the website; so i read here and there a session might do the trick.

    The plan is to include a code like this in each webpage; and retrieve the 'lang' variable from the session, but I can't seem to make it work
    Code:
    <?php
    if(!isset($_SESSION)) 
    {   session_start(); } //start a session if it's not started yet
    
    if(!isset($_SESSION['lang']))
    { $_SESSION['lang']="eng";} // if no language was saved, make default language english
    
    else{$_SESSION['lang']=$_SESSION['lang'];} // store ?previous? session data
    ?>
    later on, for making the right <div>'s appear and disappear; i keep using the variable from the session; $_SESSION['lang']

    would it work to put that code in a separate .inc.php file and then later on #include it everywhere (once it works)?

    all tips & tricks are very much appreciated

  2. #2
    Join Date
    Mar 2008
    Location
    Superior, WI
    Posts
    33

    Maybe

    Well your description is rather vague, but as far as I know PHP doesn't support onclick events so you would need set the display:block with javascript.

    with something like

    function showBlock(id){

    document.getElementById(id).style.display = 'block';
    }

    and call it in you html code with something like this

    <a href="#" onclick="showBlock('thisDiv')" >Show this div</a>

    <div id="thisDiv" >

    -- div contents ---
    </div>



    If you want a user to choose a language by clicking on something you have to either create a link for the language the basically links page to the page with the language choice appended to the url and then have PHP code at the top of your page that sets the session value based on the value of the get parameter, like below:


    language link -> <a href="thispage.php?language=english" >English</a>

    code at the top of thispage.php

    <?
    session-start()

    if(isset($_GET['language'])){
    $_SESSION['language'] = $_GET['language'];
    }
    elseif(!isset($_GET['language'])){
    $_SESSION['language'] = 'english';
    }

    ?>

    make sure you have session_start() at the top of every page that needs to refer to the $_SESSION['language'] variable. and also make sure that nothing else except for the <? of <?php tag comes before the your call to the session_start() function.
    Last edited by webmaster54880; 06-20-2009 at 09:37 AM.
    Make merry and eat jelly

  3. #3
    Join Date
    Mar 2008
    Location
    Superior, WI
    Posts
    33

    Opps okay try this

    The reason why your code above isn't working is because you can not call the session_start() function based on the state of a $_SESSION variable. The php engine will not even check if the $_SESSION['lang'] is indeed set without a call to the session_start() function FIRST. This is very important => the session_start() function tells the php engine to prepare to handle $_SESSION variable that are either being declared in the the page or the are being passed from other pages.

    change your code to this and see if it helps cleans up your problem

    <?php
    session_start(); // <- make sure there is no space before this line and

    if(!isset($_SESSION['lang']))
    { $_SESSION['lang']="eng";} // if no language was saved, make default language english

    ?>

    // AND BY THE WAY YOU DO NOT NEED THIS LINE OF CODE BECAUSE IT IS REDUNDANT.

    else{$_SESSION['lang']=$_SESSION['lang'];} // store ?previous? session data
    IF THE $_SESSION['lang'] variable was set in a previous page just include the code above at the top of every page and the $_SESSION['lang'] value will be retained for scripts that you have on other pages.

    The key to session handling in PHP is to remember that they are super globals like $_POST an $_GET and don't need to be re-initialized or redeclared like regular variables on each new page that uses them. Just remember to use the session_start() "FIRST", lol.

    Hope this helps.
    Last edited by webmaster54880; 06-20-2009 at 09:59 AM.
    Make merry and eat jelly

  4. #4
    Join Date
    Jun 2009
    Posts
    7
    yes i did do the display hiding with javascript inside php echo

    like this:
    Code:
    function changelang()
      {
                 
         
    if ($_SESSION['lang']=="nl") {
    echo "
    document.getElementById('contentENG').style.display='none'; document.getElementById('contentNL').style.display='block'; document.getElementById('date').style.display='none'; document.getElementById('datum').style.display='inline'; document.getElementById('menu').style.display='block'; document.getElementById('nav').style.display='none';";
    } else {
    echo "
    document.getElementById('contentENG').style.display='block'; document.getElementById('contentNL').style.display='none'; document.getElementById('date').style.display='inline'; document.getElementById('datum').style.display='none'; document.getElementById('menu').style.display='none'; document.getElementById('nav').style.display='block';";
    }
    }

    the code works really well inside 1 page now; but to pass the language to other pages, i add ?lang=nl or ?lang=eng to my navigation links?

    thanks alot for your help in any case

  5. #5
    Join Date
    Jun 2009
    Posts
    131
    Keep in mind that most servers are configured to destroy sessions after a certain amount of time. If you try to revive a day-old session, it will almost never work.
    Refreshed | Web Design
    Your Dreams. Our Reality. Refreshing, isn't it?
    Pierce@RefreshedWeb.com
    https://www.RefreshedWeb.com

  6. #6
    Join Date
    Jun 2009
    Posts
    7

    Almost works as it should

    tyvm for the help so far, but it seems like i can't quite get it to work as it should...

    i used the second method that was suggested, without posting the data inside the URL... since a session variable stays the same value across pages, unless you change it, that should be ok...

    somehow when i switch to another page (or refresh) , the session language resets itself to english...

    below are the codes i'm using: (i put JS alerts to see where it went wrong, but can't figure out why it goes wrong)

    in the very beginning of each page:
    Code:
    <?php
    session_start(); 
    
    if(!isset($_SESSION['lang']))
      { 
        
    $_SESSION['lang']="eng";
    } include('inc/lang.inc.php'); ?>

    lang.inc.php
    Code:
      function changelang()
      {
                 
         
    if ($_SESSION['lang']=="nl") {
    echo " document.getElementById('contentENG').style.display='none'; document.getElementById('contentNL').style.display='block'; document.getElementById('date').style.display='none'; document.getElementById('datum').style.display='inline'; document.getElementById('menu').style.display='block'; document.getElementById('nav').style.display='none';";
    } else {
    echo " document.getElementById('contentENG').style.display='block'; document.getElementById('contentNL').style.display='none'; document.getElementById('date').style.display='inline'; document.getElementById('datum').style.display='none'; document.getElementById('menu').style.display='none'; document.getElementById('nav').style.display='block';";
    }
    } function setlang($language) { $_SESSION['lang']=$language; }
    body tag:
    Code:
    <body onload="alert('<?php echo $_SESSION['lang']; ?>');<?php changelang(); ?>">
    hyperlinks:
    Code:
    <a href="#" onclick="<?php setlang("nl"); changelang(); ?>alert('<?php echo $_SESSION['lang']; ?>');" name="nl">NL</a> 
    <a href="#" onclick="<?php setlang("eng"); changelang();?>alert('<?php echo $_SESSION['lang']; ?>');" name="eng">ENG</a>

    the problem now is finding why the session language resets itself when opening a new page...

    in the links' href i set a # so that it would not refresh the page (and reset the language to english)...

    tips are very welcome

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles