PHP Tutorials (2)

2 PHP tutorials. The first one helped me A LOT, when it came to upgrading a large site, and the client wanted a multilanguage site engine. The second one is a simple flat counter

How to switch languages on a multilingual web site

1. First create a language directory, we will call it languages.

2. Create two language files, let’s say english.php and romanian.php and populate them with variables:

english.php

$txt['headtext'] = 'Welcome to our site';

romanian.php

$txt['headtext'] = 'Bine ati venit in pagina noastra';

3. Create an include directory, we will call it include.

4. Create a configuration file:

config.php

<?php
$languages = array(
'en' => 'english',
'ro' => 'romanian',
);

if (isset($_GET['lang']) AND array_key_exists($_GET['lang'], $languages)) {
include ‘./languages/’ . $languages[$_GET['lang']] . ‘.php’;
}
else {
include ‘./languages/english.php’;
}
?>

5. Create a file called index.php and fill the next lines in:

<?php
// begin includes
include_once('include/config.php') // configuration file
// end includes
?>

<?php
echo $txt['headtext']; // some text
?>

<form action=”<?php echo $PHP_SELF;?>” method=”get”>
<select name=”lang” size=”1″ onchange=”this.form.submit()”>
<option value=”">Select your language</option>
<option value=”ro”>Romana</option>
<option value=”en”>English</option>
</select>
</form>

<a href=”otherfile.php?lang=<?=$lang?>”>otherfile.php</a>

6. That’s all! End of tutorial. It’s really that simple. Just remember to refer to all links site wide as file.php?lang=<?=$lang?>:

<a href="otherfile.php?lang=<?=$lang?>">otherfile.php</a>

And the second one,

How to create a simple flat-file counter

1. First create a counter data file, we will call it counter.txt.

2. Create the counter PHP script, let’s say counter.php, as follows:

counter.php

<div align="center">Visitors:
<?php
$file = 'counter.txt';

if(!file_exists($file))
{
$handle = fopen($file, ‘w’);
fwrite($handle, 0);
fclose($handle);
}

$count = file_get_contents($file);
$count++;

if(is_writable($file))
{
$handle = fopen($file, ‘w+’);
fwrite($handle, $count);
fclose($handle);
}
else
{
echo ‘Unable to increment the counter!<br />’;
}

echo number_format($count).”;
?>
</div>

3. That’s all! Remember to chmod your data file – counter.txt – to 777. You can now include this script in your page with the following line:

<?php include('counter.php'); ?>

 
 
 

3 Comments

  1. Bill says:

    does anyone know how to display the count for another page on a different page using php?

    I want a counter on one page that will display how many hits a completely different page has gotten. I am using a graphic PHP counter and want to continue using this counter, as it impliments graphics that I have created, but I can’t seem to get this counter to do just this.

    Any help would be appreciated.

    - Bill

  2. Vrettos says:

    I think there is something wrong in the multilingual script.
    The link fot otherfile.php doe not define the language.
    It opens a window with url http://www.domain.com/otherfile.php?lang=
    and doe not fill the selected lang.
    Thank you in advance.

  3. Chip says:

    I have been using this script successfully on some of my web sites. There must be something wrong with your server. Maybe global variables. Try < ? php echo $lang; ? > instead of < ? =$lang; ? >.

Leave a Reply





 
CommentLuv Enabled
 
  • John William: Very informative article! I was wondering that can we submit more arti...
  • CSS Gallery: Great research on these resources! We noticed you didn’t include ht...
  • ramyad: Great! Finally, found a solution. I've been scratching my head on this...
  • san diego seo: There is a new guy on my soccer team who is an Englishman. From time t...
  • Spyke: Thanks for posting this. Many people don't realize that bridge does th...
    follow me on Twitter

    Last searched terms

    Top Browsers

    Top OS

    Site Stats

    Ads

    Ads

    Ajax CommentLuv Enabled 7cc703d7a351bbc1b554fad7c2b1c542 | Tots2Teens | Cariera Piatra Constanta | Verighete