1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I need some help

Discussion in 'PHP' started by web@master24, Jul 22, 2009.

  1. #1
    I was wondering how I could make users of a website change the language of the site. If for example I would have 2 language
    files:

    lang_eng.php
    lang_spa.php

    with all the language variables in there. I was thinking about having a drop down box on top of the site where users could change the language of the site. Im search in google and find this:

    make the dropdown form, action="changeLang.php"

    next, you will need to either use a cookie/session to store their language choice, or put it in the URL


    /* changeLang.php */
    $_SESSION['Language'] = $_POST['Language'];
    // setcookie("Language", $_POST['Language'], time() + 3600);
    // URL method will require you to add the QUERY STRING to all pages. might get tough
    if($Language == 'English') {
      include 'English.php';
    PHP:
    I have the languages files and the code for call the language, but how i can add this function to the dropdown menu and create the changeLang.php, please somebody can help me? thanks
     
    web@master24, Jul 22, 2009 IP
  2. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #2
    OK on the form where people changes there language you add the action attribute. So it would be something like this:

    <form action="changeLang.php" method="post">
    //Add your drop down box and submit button here
    </form>

    Then on changeLang.php you create a session or a cookie whicever method you choose, and store their language choice on there.
    Then on all the pages where you change the language you check what language is stored in the session/cookie and include the file based on this choice.
     
    wd_2k6, Jul 22, 2009 IP
  3. web@master24

    web@master24 Active Member

    Messages:
    418
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    90
    #3
    I sent you a PM
     
    web@master24, Jul 22, 2009 IP
  4. web@master24

    web@master24 Active Member

    Messages:
    418
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    90
    #4
    HI, i created the form, but how can i add the cookie in changeLang.php, the code is: $_SESSION['Language'] = $_POST['Language']; but how can i add for works. Sorry for my english im mexican.
     
    web@master24, Jul 22, 2009 IP
  5. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK firstly don't forget that for Sessions to work, you must have:

    session_start();

    as the first line on ALL of the pages you want to use your sessions on or transfer sessions to.

    Secondly on changeLang.php you are correct you would have somethinglike:
    
    $_SESSION['Language'] = $_POST['Language'];
    
    Code (markup):
    Then on all pages you have a language option you would do:

    
    if($_SESSION['Language']){
    //Then a language has previously been selected include the language here
    }
    else{
    //None has been selected, use the default language.
    }
    
    Code (markup):
     
    wd_2k6, Jul 22, 2009 IP
  6. web@master24

    web@master24 Active Member

    Messages:
    418
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    90
    #6
    ok, the page where i go to put the language is the header, because all page take the language from header, now, im add this code to the header :

    include_once($web_root."includes/sources/init.php");
    if($_SESSION['English']){
    include_once($web_root."includes/english.php");
    }
    if($_SESSION['Espanol']){
    include_once($web_root."includes/spanish.php");
    }
    else{
    include_once($web_root."includes/spanish.php");
    }


    But only what still dont work is the changeLang.php because dont load or created any cookie or session
     
    web@master24, Jul 22, 2009 IP
  7. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi,

    Please paste your changeLang.php as it is now..

    And also please paste the form where people can choose the language

    Please paste both files exactly !!

    Thanks
     
    wd_2k6, Jul 22, 2009 IP
  8. web@master24

    web@master24 Active Member

    Messages:
    418
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    90
    #8
    The changeLang:

    <?php
    $value = "Language";
    
    // send a cookie that expires in 24 hours
    setcookie("Language",$_POST['Language'], time()+3600*24*30);
    ?>
    
    PHP:
    The form:

    <form action="changeLang.php" method="post">
      <label>
      <select name="language" size="1" id="language">
        <option value="es">Español</option>
        <option value="en">English</option>
          </select>
      </label>
      <label>
      <input type="submit" name="submit" id="submit" value="Cambiar Idioma">
      </label>
    </form>
    PHP:
    The header:

    <?php
    session_start(); 
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
    header("Cache-Control: no-cache, must-revalidate" ); 
    header("Pragma: no-cache" );
    
    if(file_exists('../includes/constants.php')){
    include_once "../includes/constants.php";
    include_once "../includes/rating_functions.php";
    } else {
    include_once "includes/constants.php";
    include_once "includes/rating_functions.php";
    }
    
    ?>
    <script src="<?=$web_path;?>/topbar/top1.php" type="text/javascript"></script>
    <script src="<?=$web_path;?>/peels/peel/peel.js" type="text/javascript"></script>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
    <link href="<?=$web_path;?>css/music.css" type="text/css" rel="stylesheet" />
    <title><? if (isset($_GET[search])) { echo "$search - "; } ?><?=$settings['site_title'];?></title>
    <meta name="keywords" content="<?=$settings['site_keywords'];?>" />
    <meta name="description" content="<?=$settings['site_desc'];?>" />
    <link rel="stylesheet" href="<?=$web_path;?>css/pop.css" media="screen,projection" type="text/css" />
    <link rel="stylesheet" href="<?=$web_path;?>css/style.css" media="screen,projection" type="text/css" />
    <link rel="stylesheet" href="<?=$web_path;?>css/rating_style.css" media="screen,projection" type="text/css" />
    <?
    include $web_root."css/style.php";
    include $web_root."css/rating_style.php";
    include $web_root."includes/functions.php";
    include_once($web_root."includes/sources/init.php");
    if($_SESSION['English']){
    include_once($web_root."includes/english.php");
    }
    if($_SESSION['Espanol']){
    include_once($web_root."includes/spanish.php");
    }
    else{
    include_once($web_root."includes/spanish.php");
    }
    ?>
    <script language="JavaScript" type="text/javascript" src="<?=$web_path;?>js/ajax_search.js"></script>
    <script type="text/javascript" src="<?=$web_path;?>js/prototype.js"></script>
    <script type="text/javascript" src="<?=$web_path;?>js/pop.js"></script>
    <script type="text/javascript" src="<?=$web_path;?>js/tooltip.js"></script>
    <script type="text/javascript" src="<?=$web_path;?>js/display.js"></script>
    <script type="text/javascript" src="<?=$web_path;?>js/email.js"></script>
    <script type="text/javascript" src="<?=$web_path;?>js/load.js"></script>
    
    </head>
    <script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
    try {
    var pageTracker = _gat._getTracker("UA-5762938-2");
    pageTracker._trackPageview();
    } catch(err) {}</script>
    <body>
    
    <!-- Main Container -->
    <div class="clear_main">
    
    	<div class="topband">
    		<div class="top_left"></div>
    
    		<div class="top_menu">
    		<? if($session->logged_in){?>
          <span><?=$lang['Welcome'];?> <? echo $session->username; ?></span>
    			<span>|</span>
    			<a href="<?=$web_path;?>userinfo.php?user=<? echo $session->username; ?>"><?=$lang['AccountHome'];?></a>
    		     	<span>|</span>
          <? if($session->isAdmin()){?>
          <a href="<?=$web_path;?>mu" class="white"></a>      <a href="<?=$web_path;?>admin/admin.php"><?=$lang['AdminPanel'];?></a> <span>|</span> 
          <? } ?>
    			<a href="<?=$web_path;?>myplaylist"><?=$lang['MyPlaylist'];?></a>
    	     		<span>|</span>
    		  <a href="<?=$web_path;?>mu" class="white"><?=$lang['DescargaMV'];?></a>
              <span>|</span>
              <a href="<?=$web_path;?>/old/tutoriales" target="_self">Tutoriales</a></li>
              <a href="<?=$web_path;?>faq.php" class="white"> |
    		  <?=$lang['FAQ'];?>
    		  </a>
    		     	<span>|</span>
    	    <a href="<?=$web_path;?>process.php" class="white"><?=$lang['Logout'];?></a>	
    	    <? } else { ?> 
    	    <?=$lang['NotaMember'];?> <a href="<?=$web_path;?>register.php"><?=$lang['Signup'];?></a>
              <span>|</span>
          <a href="<?=$web_path;?>main.php"><?=$lang['MemberLogin'];?></a>
              <span>|</span>
              <a href="<?=$web_path;?>/old/tutoriales" target="_self">Tutoriales</a></li>
              <span>|</span> </a>      <a class="white" href="<?=$web_path;?>faq.php"><?=$lang['FAQ'];?></a>
          <? } ?>
    		</div>
    
    		<div class="top_right"></div>
    	</div>
    PHP:
     
    web@master24, Jul 22, 2009 IP
  9. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #9

    should be:

    
    include_once($web_root."includes/sources/init.php");
    if($_SESSION['Language']=="English"){
    include_once($web_root."includes/english.php");
    }
    if($_SESSION['Language']=="Espanol"){
    include_once($web_root."includes/spanish.php");
    }
    else{
    include_once($web_root."includes/spanish.php");
    }
    
    PHP:
     
    killaklown, Jul 22, 2009 IP
  10. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #10
    OK i'm going to do the example based on Sessions and not Cookies.

    change changeLang.php to this:

    
    <?php session_start(); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php
    if($_POST){
    	//Then somebody has submitted the form
    	$lang = $_POST['language'];
    	//Test Text
    	echo "<h1>You want to change your language to $lang</h1>";
    	
    	//Create Session Variable
    	$_SESSION['lang'] = $lang;
    	
    	//Test Session Variable was created
    	echo "<h2>We have just created a Session with the value".$_SESSION['lang']"</h2>";
    }
    ?>
    </body>
    </html>
    
    PHP:
    Then change the language part in your header from:

    
    if($_SESSION['English']){
    include_once($web_root."includes/english.php");
    }
    if($_SESSION['Espanol']){
    include_once($web_root."includes/spanish.php");
    }
    else{
    include_once($web_root."includes/spanish.php");
    }
    
    PHP:

    to this:

    
    if($_SESSION['lang']){
    	//A session has been created with a selected language
    	echo "<h1>A Language has previously been chosen with the value:".$_SESSION['lang']."</h1>";
    	
    	if($_SESSION['lang'] == "en"){
    		echo "<h2>This user has previously selected English as their language choice</h2>";
    		include_once($web_root."includes/english.php");
    	}
    	elseif($_SESSION['Espanol']){
    		echo "<h2>This user has previously selected English as their language choice</h2>";
    		include_once($web_root."includes/spanish.php");
    	}
    	else{
    		echo "<h2>An unknown choice is stored in the lang</h2>";
    	}
    }
    else{
    	//A language has not yet been selected by the user,  so use the default language
    	include_once($web_root."includes/spanish.php");
    }
    
    PHP:
    Note: It will output messages, I have done this on purpose so we know what's going on at each stage, we can delete these messages after it is working.
    Let me know if it is working, or if not what messages are being shown and when..
     
    wd_2k6, Jul 22, 2009 IP
  11. web@master24

    web@master24 Active Member

    Messages:
    418
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    90
    #11
    Only show me an error in changelang:

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/mimp3/domains/mimp3.net/public_html/changeLang.php on line 21
     
    web@master24, Jul 22, 2009 IP
  12. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Sorry changeLang.php:

    
    <?php session_start(); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php
    if($_POST){
        //Then somebody has submitted the form
        $lang = $_POST['language'];
        //Test Text
        echo "<h1>You want to change your language to $lang</h1>";
        
        //Create Session Variable
        $_SESSION['lang'] = $lang;
        
        //Test Session Variable was created
        echo "<h2>We have just created a Session with the value".$_SESSION['lang']."</h2>";
    }
    ?>
    </body>
    </html>
    
    PHP:
    Just missed a fullstop
     
    wd_2k6, Jul 22, 2009 IP
  13. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #13
    
    <?php session_start(); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php
    if($_SERVER['REQUEST_METHOD']=="POST"){
        //Then somebody has submitted the form
        $lang = $_POST['language'];
        //Test Text
        echo "<h1>You want to change your language to $lang</h1>";
        
        //Create Session Variable
        $_SESSION['lang'] = $lang;
        
        //Test Session Variable was created
        echo "<h2>We have just created a Session with the value $lang</h2>";
    }
    ?>
    </body>
    </html>
    
    PHP:
     
    killaklown, Jul 22, 2009 IP
  14. web@master24

    web@master24 Active Member

    Messages:
    418
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    90
    #14
    Works, thanks man, you are awsome, only i want delete all messages, and when the user select the language send to index.php

    And how much time the session is started,
     
    web@master24, Jul 22, 2009 IP
  15. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #15
    For redirecting to index just do:
    
    <?php session_start();
    
    if($_SERVER['REQUEST_METHOD']=="POST"){
        //Then somebody has submitted the form
        $lang = $_POST['language'];
        
        //Create Session Variable
        $_SESSION['lang'] = $lang;
        
        header('location: index.php');
    }
    ?>
    YOUR HTML
    
    PHP:
     
    killaklown, Jul 22, 2009 IP
  16. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Glad it is working :) Congratulations my Mexican friend!
     
    wd_2k6, Jul 22, 2009 IP
  17. web@master24

    web@master24 Active Member

    Messages:
    418
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    90
    #17
    In what file make this, form, header or changelang?

     
    web@master24, Jul 22, 2009 IP
  18. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #18
    changelog.php
     
    killaklown, Jul 22, 2009 IP
  19. web@master24

    web@master24 Active Member

    Messages:
    418
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    90
    #19
    Thanks to both, works great, only i have a question, its possible created the session by a link and no a drop menu?
     
    web@master24, Jul 22, 2009 IP
  20. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #20
    killaklown, Jul 22, 2009 IP