getButterfly Logo getButterfly

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:


[php]$txt[‘headtext’] = ‘Welcome to our site’;[/php]


[php]$txt[‘headtext’] = ‘Bine ati venit in pagina noastra’;[/php]

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

4. Create a configuration file:


$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:

// begin includes
include_once(‘include/config.php’) // configuration file
// end includes

echo $txt[‘headtext’]; // some text

<form action="" 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>

<a href="otherfile.php?lang=<?php echo $lang;?>">otherfile.php</a>[/php]

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=<?php echo $lang?>:

[php]<a href="otherfile.php?lang=<?php echo $lang;?>">otherfile.php</a>[/php]

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, as follows:


[php]<div align="center">Visitors:
$file = ‘counter.txt’;</code></p>
$handle = fopen($file, ‘w’);
fwrite($handle, 0);

$count = file_get_contents($file);

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

echo number_format($count).”;

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]<?php include(‘counter.php’);?>[/php]

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

5 thoughts on “PHP Tutorials (2)

  1. 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. 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
    and doe not fill the selected lang.
    Thank you in advance.

    1. Hi Vrettos,

      I read your comment and I tried the same with the same problem. Were you able to solve this problem of not showing the language code?
      It’s been some time but I hope you remember…

      Best regards…

  3. 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

Your email address will not be published. Required fields are marked *