This blog is NOFOLLOW Free!
  • INCEPTION THEME

    Clean, contrasting WordPress theme built on the foundation of Whiskey Air.
  • WHISKEY AIR THEME

    Clean developer theme, suited for heavy modifications.
  • X5 TURBO THEME

    3 columns Turbo theme, now reloaded!
  • CLEAR APPLE THEME

    Almost magazine theme, without the hassle of administration.

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('/php_tutorials_2/include/config.html') // 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="/php_tutorials_2/otherfile_lang__lt_lang_gt_.html">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 = '/php_tutorials_2/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('/php_tutorials_2/counter.html'); ?>

5 Responses to PHP Tutorials (2)

  • 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

    Reply
  • 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.

    Reply
    • 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…

      Reply
  • 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 instead of .

    Reply
  • Pingback: Tutorials Overview | Butterfly Media Romania Blog - Marketing, SEO and WordPress

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>