URGENT need Help with PHP please

Discussion in 'PHP' started by allthegoodnamesweretaken, Feb 15, 2011.

  1. #1
    Hi. This is hard for me to explain but here goes. My site is http://bit.ly/einIPE There are two flags at the top, when they are clicked the language should change.

    So I'm using variables for the text eg:
    <?php echo $title; ?>
    PHP:
    instead of using actual hardcoded text. I am calling the php include at the top of the code:

    <?php include("gb.php"); ?>
    PHP:
    However I want to do the following:
    When the British flag is clicked, it should change to "gb.php"
    When the Swedish flag is clicked, it should change to "se.php"

    How would I go about doing this? Are there any other work arounds?
    Any help is GREATLY appreciated, i do hope it makes sense I'm in desperate need for this! :D
     
  2. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #2
    What you need to do is detect the language they've chosen, store the language in a cookie or a session so they don't have to keep clicking the language icons.. haven't tested the code below but should give you an idea.

    <?php
    // Store the possible language files in an array, gb, se, fi, fr etc.
    $languages = array(
    	'gb',
    	'se',
    );
    
    // Check if the user is requesting to change the language
    if(isset($_GET['lang']))
    {
    	// Check that the language they want to use actually exists 
    	if(array_key_exists($_GET['lang'], $languages))
    	{
    		// Store the language they want to use in the cookie for 7 days
    		setcookie('language', $_GET['lang'], 60 * 60 * 24 * 7);
    	}
    }
    
    // Check if the user has specified a language
    if(isset($_COOKIE['language']))
    {
    	// Make sure the language exists again, cookies can be modified and we don't want them including any other pages
    	if(array_key_exists($_COOKIE['language'], $languages))
    	{
    		// Include the language file.php
    		include($_COOKIE['language'] . '.php');
    	}
    }
    else
    {
    	// No language specified by the user so include the default language to use..
    	include('gb.php');
    }
    ?>
    PHP:
    Then to set the language you can use index.php?lang=gb or index.php?lang=se etc.
     
    crazyryan, Feb 15, 2011 IP
  3. allthegoodnamesweretaken

    allthegoodnamesweretaken Peon

    Messages:
    374
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    WOW that's more help than I would have ever expected.

    I'm having a slight problem though, and I would also like to send you a little something for your trouble. So check your inbox please :)