Look up browser type in PHP

It’s sometimes useful and/or necessary to look up you viewer’s browser type. For example, Internet Explorer does not display the Twitter Profile Widget properly. It’s a bug in the Twitter widget and it works fine in Firefox. So, you should only display the Twitter widget if your viewer is using a browser other than IE. Here’s what you would do.

Use the following PHP code to look up the browser type.

<?php
$useragent = ($_SERVER["HTTP_USER_AGENT"]);
if (preg_match("|MSIE ([0-9].[0-9]{1,2})|",$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = "IE";
} elseif (preg_match( "|Opera ([0-9].[0-9]{1,2})|",$useragent,$matched)) {
    $browser_version=$matched[1];
    $browser = "Opera";
} elseif(preg_match("|Firefox/([0-9\.]+)|",$useragent,$matched)) {
        $browser_version=$matched[1];
        $browser = "Firefox";
} elseif(preg_match("|Safari/([0-9\.]+)|",$useragent,$matched)) {
        $browser_version=$matched[1];
        $browser = "Safari";
} else {
        // browser not recognized!
    $browser_version = 0;
    $browser= "other";
}
echo "browser: $browser $browser_version";
?>

Here is a demo using the exact script above:

browser: other 0
Then, use the following IF statement to display whatever you want.

<?php 
if ($browser != "IE") {
// print out the twitter widget only if NOT IE
}
?>

Let me know if this helps anyone by leaving a comment below.

Read More

How to Cache Your Website for Faster Page Load

Caching In

When it comes to site speed, faster is always better. Did you know that Google now includes site speed when considering page rank? It’s also more likely that viewers will leave a slow page out of annoyance and impatience. As a site owner, it’s in your best interest to have a fast site in order to improve page rank and maintain viewership. To speed up your site, you can ensure that your coding is clean, that your web host has updated and well-maintained servers, and that your images are all optimized. Caching is another effective option.

What is a cache?
All of your site’s data (script, images, files) are stored on a server. When a viewer wants to look at your site, they have to request to access the data from the server. Figuratively speaking, the viewer and the server are a great distance away from each other, and such a request could take a long time to fulfill. A web cache is located between the two, like a rest stop along a highway. When the initial transaction occurs between the viewer and server, the cache (a temporary storage unit) stores some of this data as it passes through. The next time the viewer makes the request, they don’t have to go all the way to the server, but instead can access the data from the nearby cache.

Benefits of caching
Caching is a great way to speed up your site. Because it’s located closer to the viewer with readily available information, it reduces the time that the viewer has to spend retrieving the data. Your site will load faster and the viewer is content. Caches are also beneficial for the site owner in that less bandwidth is being used since the cache is doing some of the work for the server. This is less taxing for the server and more cost effective for the site owner. Also, should your server ever go down, not all is lost with a cached site because viewers can at least still access the cached version of your site.

The method
You can customize your caching to function with your site. Generally, you want to cache static items and not the constantly updated items. You can even specify how long to cache each item. For example, images, javascripts, and style sheets are relatively permanent fixtures, so you could go ahead and cache them for a long time, whereas frequently updated content can be cached briefly. If you own a fairly static site (i.e. not a blog), you could even go ahead to cache the entire site.

Your options
I’m only addressing a few content management systems (CMS) here, but depending on what CMS you’ve decided to utilize, there are different ways to go about caching your site. It shouldn’t be too hard to seek out and implement the right resources. The most important takeaway here is that you cache your site.

WordPress
WordPress is rapidly growing in popularity and for good reason. It’s open-source, user-friendly, and has a large supportive community. However, by being database driven, WordPress can put some serious strains on your server. Every time a viewer visits your WordPress site, they’re pulling information from the database for each individual file. Not only is this tedious and slow, but it’s also taxing on the server. If you’ve chosen WordPress as your CMS, then you definitely want to cache your site. Although WordPress doesn’t have caching built in, there are many options in caching plugins. Install one of these, and you’ll see significant improvement in your site’s load time.

Drupal
Because Drupal is used to create dynamic sites, chances are that if you’re using Drupal, you don’t have a static page. But caching is still relevant here. Drupal makes it easy by offering built-in caching options with additional caching modules available.

Joomla!
Joomla doesn’t come with caching, but does have several caching plugins available.
Check out this article that goes into great detail the basics of Joomla caching.

Apache
You can enable caching directly on the server itself using an Apache module. This takes a little more expertise but allows you much more control in customizing your caching.

Cache is king
There’s no question that a faster site is better. There are several ways to go about speeding up your site – caching is one great option. Most of the well-developed CMSs have an easy way of going about implementing caches on your site. Once cached, viewers won’t have to reload your site from scratch each time they access it, but rather can quickly retrieve your site’s files from the cache instead. This will help save you money in bandwidth, preserve your server, and retain viewers. Cache is king!

Nina Wu writes for A Small Orange.

Read More

How to display another website on your site

Do you need to display someone else’s website on your website? How about displaying just a section of someone else’s website on yours? This is kinda tricky because you need to look for the HTML inside the source code of the website you want to display and decide on where you want to start and end. And, you should always keep an eye on this because if the other website’s developer decides to change the HTML, your script will be broken and you will need to update it. So, let’s get started.

The best way to do this is using cURL. No need to worry about the particulars as long as you’re on an Apache server and running PHP, you’ll be ok.

First, you’ll need to define the URL of the page you want to display on your site by using the following line of code. In this example, we’re going to display a small section of a very large web page.

$ch = curl_init("http://www.city-data.com/city/Altamonte-Springs-Florida.html");

Then, you’ll need to define the starting point (your choice) where you want to begin display by using the following line of code. In this case I’m using the following:

$bodyandend = stristr($retrievedhtml,"</table>\r\n<br/><h3>");

Notice the HTML between the quotes. The quotes need to remain. Update the HTML part from the source code (HTML) of the page you want to display.

Now you need to define the ending point (your choice). In this case I’m using the following:

$positionendendbodytag=strpos($bodyandend,"</ul>");

Notice the HTML between the quotes. The quotes need to remain. Update the HTML part from the source code (HTML) of the page you want to display.

Now, you’ll need to play around with the number on the following line to make sure you’re displaying the proper HTML. In this case I’m using -3.

$grabbedbody=substr_replace($grabbedbody ,"",-3);

The other lines of code should not be touched. See below for all the code that needs to be placed inside of your web page.

DEMO Below is a section of the following URL: http://www.city-data.com/city/Altamonte-Springs-Florida.html
The exact code to display this is below.

More Information to Come…

<?php
$ch = curl_init("http://www.city-data.com/city/Altamonte-Springs-Florida.html");
ob_start(); 
curl_exec($ch); 
curl_close($ch); 
$retrievedhtml = ob_get_contents(); 
ob_end_clean(); 
 
$bodyandend = stristr($retrievedhtml,"</table>\r\n<br/><h3>"); 
$positionendstartbodytag = strpos($bodyandend,">") + 1; 
$positionendendbodytag=strpos($bodyandend,"</ul>"); 
$grabbedbody=substr($bodyandend, $positionendstartbodytag, $positionendendbodytag); 
$grabbedbody=substr_replace($grabbedbody ,"",-3);
 
If ($grabbedbody != "") {
    echo $grabbedbody; 
}
else
{
    echo "More Information to Come...";
}
?>

Read More

How to redirect all pages on a website

It’s very easy to redirect all your old web pages to a new domain or URL using a .htaccess file.  This can only be done on an Apache server which most of you should have your site hosted on anyway.  Here is what you do. 

Download the .htaccess file in the root of your web server and paste the line below into it. If you don’t see one, (make sure these files are not hidden), create a new text file using a program like WordPad and name it “htaccess.txt”.  DO NOT use Microsoft Word or any other similar word processing program.  These do not save documents as plain text. 

Copy the following and and paste into your text document, replacing the http://my-new-domain.com/ with your new URL.

RedirectMatch permanent /.* http://my-new-domain.com/

Upload the file to your site root and rename it to “.htaccess” (without quotes). 

That’s all!

 

Read More