Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Wednesday, September 10, 2008

Combining PHP and HTML pages

A while ago, one of my clients gave me the source code for one of his websites for me to make some adjustments. The first thing that shocked me when I was going through this code, was the fact that all of the HTML code was inside the PHP pages.

This is just awful. It’s almost like having a toilet inside of your office (although many of you would like that). PHP pages must only contain the logic of your website and HTML pages must be used for the GUI (Graphical user interface).

There is an easy way to combine PHP and HTML into a single output: Smarty (http://www.smarty.net). Smarty is a template engine which allows you to parse a HTML page from a PHP page. You don’t need to install anything. Just download the PHP code and include it on your project.

Here’s a quick example:

index.php
 
<?php

// Smarty initialization
require_once('Smarty.class.php');
$smarty = new Smarty();

$smarty->display(‘index.html’);

?>

index.html

<html>
<head>
<title>Smarty test</title>
</head>
<body>
This is a test!
</body>
</html>

Of course, there are many other many advanced uses for this tool which you can read about from the Smarty Manual. Here are some examples:

Example 1: Variables

Let’s say you want the title of your page to be a variable which you can change from a PHP script. This is the way to do it using Smarty:

index.php

<?php

// Smarty initialization
require_once('Smarty.class.php');
$smarty = new Smarty();

$smarty->assign(“title”, “My title”);
$smarty->display(‘index.html’);

?>

index.html

<html>
<head>
<title>{title}</title>
</head>
<body>
This is a test!
</body>
</html>

You can also assign arrays or objects and use them on you HTML page as well.

Example 2: Loop sentences

Imagine you want to display a list of all your friends, which are stored in an array. This is how you could do that with Smarty:

index.php

<?php

// Smarty initialization
require_once('Smarty.class.php');
$smarty = new Smarty();

$myFriends = array(“Mike”, “Paul”, “Peter”, “John”);
$smarty->assign(“myFriends”, $myFriends);
$smarty->display(‘index.html’);

?>
index.html

<html>
<head>
<title>My Friends</title>
</head>
<body>
My friends are:
<ul>
{foreach from=$myFriends item=friend}
<li>{$friend}</li>
{/foreach}
</ul>
</body>
</html>
I hope this brief tutorial was useful for you. If you have any questions, please leave a comment and I’ll try to reply as soon as possible.

Friday, August 22, 2008

Obtaining a visitor’s country from its IP address

Sometimes, when you are building a site, you may want the content of the homepage or even the language to be used, to change depending on the country from which the visitor is accessing.

How can you do this? It’s actually pretty simple, just follow these steps:

1. Get an IP-Country database. Each row of these databases often has three columns: start and end range for the IP address and the country to which that range is associated (e.g. http://www.ip2location.com). Most of these databases are not free, and the ones that are, are generally a little outdated. I recommend purchasing a database, mainly because you can download a new updated version every month.

2. Import the IP-Country database into the database on your site. The IP-Country databases are generally in the .csv format (comma separted values), so you have to execute a query to import all the values. That query should look something like this:


LOAD DATA LOCAL INFILE '/ip-country.csv'
INTO TABLE ip_country
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(start_range, end_range, country);

3. Include the following PHP code on the page that you want to use to obtain the Country:

$IPaddr = "";
if (getenv("HTTP_X_FORWARDED_FOR")) {
$IPaddr = getenv("HTTP_X_FORWARDED_FOR");
} else {
$IPaddr = getenv("REMOTE_ADDR");
}
$ips = split ("\.", "$IPaddr");
$ip_address = ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
$sql = "SELECT country FROM ".TBL_IP_TO_CITY." WHERE ".$ip." BETWEEN IP_FROM AND IP_TO";
$res = $db->query($sql);
$row = mysql_fetch_row($res)
$country = $row[‘country’];

That’s it! You can now obtain the name of the country from which you visitor is accessing from and display the appropriate content.

P.S: If you think Ip2Location.com is paying me for each database bought from this Blog, you are wrong!

P.S2: If you are working at Ip2Location.com, please consider paying me for all the referrals!