12 January 2012 ~ 3 Comments

Request: Google Translate API v2 using PHP

I was asked the other day in a comment on my shop site the following quote.

Do you also have php version of Google Translate API v2 ?
I’m asking because i need to hide somehow the api key from public or do you know another way of doing it ?

Well even if that person didn’t hide the API key it wouldn’t matter because you need to set what sites are allowed to use the API key. No one can use it but the owner of that API key. It’s fixed for only the domains that are on the white list in the owners Google account.

Continue Reading

13 April 2011 ~ Comments Off

Detect Status of Skype User using PHP

If you wish to display your Skype status on your website, use this code to start of with.

<?php
function check_skype_status($skypeusername){
        $url = 'http://mystatus.skype.com/smallicon/' . $skypeusername;
        if(base64_encode(file_get_contents($url)) == "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAA3NCSVQICAjb4U/gAAAANlBMVEX////////29/fy8/Pu7+/l5+fh4+Pd39/a3d3W2NjMzMzDx8fAxMS6vr62u7uxtbWvtLSssLAwgBwiAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAK8AAACvABQqw0mAAAABV0RVh0Q3JlYXRpb24gVGltZQA4LzE4LzA1oj6Z/QAAACV0RVh0U29mdHdhcmUATWFjcm9tZWRpYSBGaXJld29ya3MgTVggMjAwNId2rM8AAAB6SURBVBhXXc9BDsQgCEBRUForjij3v2xBSdsMu/9CIgKA2sBntBwHf9D7LLVWDlLvy5q5LVHrM3ln7i5ay5UwMSekANs3SZhljC2VW0brOecC7lP+QAhzRnJZ73TrYUixMqVRF+nURGSf8nsnrlWO2ccuMvT/RD8IcAMCGwfk9MNc3gAAAABJRU5ErkJggg=="){
                $status = 'Offline';
        }
        else{
                $status = 'Online';
        }
        return $status;
}
echo check_skype_status("yourusername");
?>

Continue Reading

Tags:

03 April 2011 ~ Comments Off

Backing up your MySQL Database using PHP

Backing up your database is the far most important thing that you need to do. With out it your site does not work and loosing all of that data is a night mare. Most of the time you forget about backing up your database because you don’t regularly login to your web host account to do so unless there is a problem with your site or you need to tweak something on the server.

Show »

Well I have created a cron-job script that you can setup on your web host that will back up your database for you.

[...]

Continue Reading

23 March 2011 ~ Comments Off

Read and Fetch Hidden File Data

Ever wanted to fetch data from a file that is hidden if you were to access the file directly, well here is a piece of code that can do just that. You can display the information any where you want once the data has been fetched.

First of all you will need to copy two functions in to your website that will be called.

The first one opens the file you have asked the function to read in read only mode. This function reads the very top of the file for the headers you have in place. These headers must match the default headers you have set in the second function.

// This gets file data when called and reads the file in read only.
function get_file_data($file, $default_headers){
        // Just open for reading.
        $fp = fopen($file, 'r');

        // Pull only the first 8kiB of the file in.
        $get_data = fread($fp, 8192);

        // PHP will close file handle, but we are good citizens.
        fclose($fp);

        foreach($default_headers as $field => $regex){
                preg_match('/'.preg_quote($regex, '/').':(.*)$/mi', $get_data, ${$field});
                if(!empty(${$field})){
                        ${$field} = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', ${$field}[1]));
                }
                else{
                        ${$field} = '';
                }
        }

        $get_data = compact(array_keys($default_headers));

        return $get_data;
}

The second will need adjusting to what information you would like to fetch from that file. The headers can be what ever you want as long as you make sure the header is the same as the default headers in the function.

// This fetches the file data and returns back the information received.
function fetch_file_data($file){
        $default_headers = array(
                'Name' => 'Name',
                'Email' => 'Email',
                'URI' => 'URI'
        );

        // This calls the other function to read the headers from the file specified.
        $file_data = get_file_data($file, $default_headers);

        $file_data['Name'] = $file_data['Name'];
        $file_data['Email'] = $file_data['Email'];
        $file_data['URI'] = $file_data['URI'];

        return $file_data;
}

How do I use these functions?
You simply use the ‘fetch_file_data’ function and echo in place were you want to display the data from that file like so.

<?php
$file = 'Place the destination of your file here!';
$data = fetch_file_data($file);
echo $data['Name'];
echo $data['Email'];
echo $data['URL'];
?>

For the email and web url you can display them as actual links.

echo '<a href="mailto:'.$data['Email'].'">'.$data['Email'].'</a>';
echo '<a href="'.$data['URL'].'">'.$data['URL'].'</a>';

How do I write my hidden data at the top of the file?

<?php
/*
Name: John Smith
Email: [email protected]
URI: http://www.example.com
*/
?>

Continue Reading

Tags: ,

21 February 2011 ~ Comments Off

Translating Strings

Translating your website can be the most time consuming thing to do if you are supporting other languages for a more global audience and to do that, you require a system that you can easily keep building and translate your strings later without having to go back and replace all the strings that requires a function to load the language the visitor wishes to read your website in.

WordPress is one example of a system that allows developers to provide either there themes or plug-ins in several languages by simply translating a file that stores all the strings side by side (original / translation) of each other. If a string is not translated it will load the original string.

I have created a similar function that you can use for your site if it is coded in PHP.

Simply copy and paste this function in the file you wish to store it.

function text($pages,$string){
        global $page;

        if($page[$pages][$string] == ''){
                $loadString = $string;
        }
        else{
                $loadString = $page[$pages][$string];
        }
        echo $loadString;
}

To use it, simply write:

<?php text('pagename', 'Your text string goes here.'); ?>

Now you are wondering what do I change ‘pagename’ too. That’s easy, if the string is on the homepage only then you would put ‘homepage’ in place of ‘pagename’. If the string is something that is shared or loaded on every page like your menu navigation then you would put ‘mainnavigation’ in place instead.

How do I translate my strings?

First you need a new file created and saved as the default strings you are going to produce translations of i.e. ‘default.php‘ .

Now you write your strings like so:

<?php
$page['homepage']['Welcome to my website.'] = 'Welcome to my website.';
?>

Copy the default strings file and save it as another language i.e. ‘japanese.php‘ . Now you can translate the strings in the new language like so:

<?php
$page['homepage']['Welcome to my website.'] = '私のウェブサイトへようこそ.';
?>

And that’s it. How you decide to load your language files on your site is up to you.

Good luck.

If you liked this post, why don’t you share it with others using Twitter, Facebook, Digg or Stumbleupon on the right hand side or subscribe below to receive new posts in your email.

Continue Reading

Tags: ,

30 November 2010 ~ 1 Comment

XML Parsing using PHP

I had wanted to write this tutorial sometime ago but never got time to finish it. Now I have. This is a tutorial I hope will help others who have had similar problems parsing XML data using php. [...]

Continue Reading

Tags: ,

24 November 2010 ~ 2 Comments

Tim Thumb Positioning Mods

Many of you want to display your images just right on your site and the majority of you use Tim Thumb script for quick easy cropping and quality control. One of the flaws in Tim Thumb script is that you can’t position the image exactly were you want to crop, you only get a compass cropping system.

  • North = t, tl, lr, tr, rt
  • South = b, bl, lb, br, rb
  • West = l, tl, lt, bl, lb
  • East = r, tr, rt, br, rb

The script basically calculates the height and width of the image and then by the size of the image you want it to be. I find my self in needing something a bit more specific that will allow me to crop a large image to a specific area.

So i decided to add my own combination’s by adding a divider. I’m still testing this out my self and so far it is working so if you wish to use it for yourself you are quite welcome to add the following code to the Tim Thumb script.

Simple open the timthumb.php and locate positional cropping. On the last break; before default: add the following and then save.

case 'ml2':
$src_x = $width/2; // Divides the width by 2
break;
case 'ml3':
$src_x = $width/3; // Divides the width by 3
break;
case 'ml4':
$src_x = $width/4; // Divides the width by 4
break;
case 'ml5':
$src_x = $width/5; // Divides the width by 5
break;
case 'ml6':
$src_x = $width/6; // Divides the width by 6
break;
case 'ml7':
$src_x = $width/7; // Divides the width by 7
break;

This allows you to position the image exactly were you want it in the middle from the left.

To add this mod to your image simply add a=ml0 in your html image string. Change the zero to the number you want to use.

I will add other combination’s in the future.

Enjoy.

Continue Reading

Tags: , ,

22 November 2009 ~ Comments Off

Detecting IE with PHP

I needed something to help me detect just IE6 so that I can have the site load things that only work on IE6 and not everything. So I came across this and thought I share it with those who also need it. It also detects IE that don’t exist yet.

function iever($compare=false, $to=NULL){
 if(!preg_match('#MSIE (\d)#', $_SERVER['HTTP_USER_AGENT'], $m)
 || preg_match('#Opera#', $_SERVER['HTTP_USER_AGENT']))
 return false === $compare ? false : NULL;

 if(false !== $compare
 &amp;&amp; in_array($compare, array('&lt;', '&gt;', '&lt;=', '&gt;=', '==', '!='))
 &amp;&amp; in_array((int)$to, array(5,6,7,8,9,10))){
 // if IE isn't either a) long gone
 // or b) fixed permanently from needing this function by version 10 I am retiring
 return eval('return ('.$m[1].$compare.$to.');');
 }
 else{
 return (int)$m[1];
 }
}

With that function, you can do the things like:

if(iever()) // this is a version of IE
if(iever('&lt;', 8)) // is IE less than 8
if(iever('&gt;=', 7)) // is IE greater or equal than 7
if(iever('!=', 6)) // isn't IE6

and more. I will leave the rest for you to figure out.

Original source: http://shru.us/160

Continue Reading

Tags: ,

04 September 2009 ~ Comments Off

Redirect User for iPhone, iPod and Opera Mini

How to redirect a user on your website that is using either the iPhone, iPod or Opera Mini is easy to do. All you have to do is is add the following code to your header of your “index” page.

// Redirect to mobile site if it's the iPhone or iPod
if(strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'], 'iPod')){
 header('Location: http://iphone.domain.com');
 exit();
}

// Redirect to mobile site if it's the opera mini
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
if(stristr($user_agent, 'Opera Mini')){
 //its opera mini, try the X-OperaMini-Phone-UA
 $user_agent = isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) ? $_SERVER['HTTP_X_OPERAMINI_PHONE_UA'] : $user_agent;
 header('Location: http://mobile.domain.com');
 exit();
}

Simply change the header address to where you want to redirect the user and your done.

Continue Reading

Tags: ,

26 April 2009 ~ Comments Off

Part 2. Multi-Language Site – Storing the Languages in a Database

Now if you were to have more than one language, you will want an easy way of storing them. So to do that you will need to create a database to store the information of the languages you are going to have for your website.

Simply copy the code below, access your database control panel (phpMyAdmin or something else) and paste into a SQL.

CREATE TABLE IF NOT EXISTS `language` (
`lang_code` char(2) NOT NULL default '',
`lang_filename` varchar(32) NOT NULL default '',
`lang_image` varchar(32) NOT NULL default '',
`is_active` set('Y','N') NOT NULL default '',
`name` varchar(32) NOT NULL default '',
`charset` varchar(32) NOT NULL default '',
`image_data` text NOT NULL,
`mime_type` varchar(255) NOT NULL default '',
`is_default` char(1) NOT NULL default '',
KEY `lang_code` (`lang_code`),
KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='This stores the languages for the site.';

INSERT INTO `language` (`lang_code`, `lang_filename`, `lang_image`, `is_active`, `name`, `charset`, `image_data`, `mime_type`, `is_default`) VALUES
('en', 'en_gb.php', 'gb.png', 'Y', 'English', '', '', '', 'Y'),

Run the code and you should then have a table to store the languages plus the default language already inserted.

In the next tutorial I will show you how to administer the languages, add another, translate them and more.

End of Part 2, Part 3 Coming Soon!

Continue Reading

26 April 2009 ~ Comments Off

Part 1. Multi-Language Site – Creating the Language Files

This is the first part of the tutorial Multi-Language Site. This is a simple way of how to have language files that allow you to easily display your site in multiple languages. Even if you only wish your website to use one language, using language files is a must for any big or growing website because it allows for changing text across the whole site, from just one simple file. This example only shows the concept to do it.

It would be up to you to enable multiple language selection options. First file, call it: en_gb.php. Inside this file we will place two language definitions:

< ?php

$messages['test_one']    = 'I am test one!';
$messages['test_two']    = 'I am test two!';

?>

Ok, now for the function we will use to always access the language file. Call this file: lang.function.php
Inside this file we put the following code:

< ?php
function getLang($lang)
{
global $messages;
$display_message = $messages[$lang];
return $display_message;
}
?>

We make $messages be a global because it is easier to access the $messages within the en_gb.php file this way. The $lang is what determines which message we want from our en_gb.php file.

Ok, now make a file called: test.php and within this file include this:

< ?php
include('en_gb.php');
include('lang.function.php');

//test one
echo getLang('test_one');
echo "<br />";

//test two
echo getLang('test_two');
?>

And there you have it! A simple easy way to contain text on your website to one central location. Now just do the same again for another language.

End of Part 1, Part 2!

Continue Reading

Tags: ,

26 April 2009 ~ Comments Off

Introduction to Multi-language Site Tutorial

I am going to be posting for the first time a Three part tutorial on how you can make your website have more than one language available for the visitor. The first part will show you how to create a language file and how to include the language into your site. The second part will show you how to store the languages in a database and allow the user to switch between the languages that you provide. The third and final part will allow you to control the languages on your site and let you decide which language you want as default.

Part 1. Multi-Language Site – Creating the Language Files

Part 2. Multi-Language Site – Storing the Languages in a Database

Part 3. Multi-Language Site – Administrating the Languages

Continue Reading

Tags: ,
One of 347 websites proudly supporting Earth Hour.    Use WordPress? Get the plugin.
Performance Optimization WordPress Plugins by W3 EDGE