A blend of programming and seo

Category — howto

How to create a zip archive using PHP

The following is a library that allows you to generate zip file archives using php.


<?php
include('ziplib.php');

$zipfile = new Ziplib;
$zipfile->zl_add_file('This is a test file','path/to/file','g9');
//You can stream the ZIP file or write it in a file on your server
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename=\'testfile.zip\'');
echo $zipfile->zl_pack('zip file comments');
?>

This script will dynamically create a zip archive using the files specified with zl_add_file and output it to the browser (the final zip file will be named: testfile.zip).

Options

zl_add_file allows you to specify the compression level of the file file that will be added to the archive.

  • n (none)
  • b (bzip)
  • g (gzip)

Download

The php zip library can be downloaded Here

June 17, 2009   9 Comments

How to install Alternative PHP Cache

What is APC cache?

The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. It was conceived of to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

Installing

APC Cache is not included with the latest release of php (although it is planned on being included with version 6). It says on the main PHP website that the .dlls can be downloaded from the pecl extensions package, but after downloading and extracting it, I realized that it is not there. I have found the correct .dlls and other files that are needed and they can be download from my server here.

After downloading and extracting the above zip file, complete the following steps:

1) copy the proper php_apc.dll (depending on your version) into your php extensions directory

2) add the following to your php.ini: extension=php_apc.dll (this should be placed under the other extension lines)

3) add the following to your php.ini:

apc.shm_segments=1
apc.optimization=0
apc.shm_size=128
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1

Note: APC needs a temp path to exist, and be writable by the web server. It checks TMP, TEMP, USERPROFILE environment variables in that order and finally tries the WINDOWS directory if none of those are set.

4) copy apc.php (included with the .zip file) to directory on your web server and launch it.

You should see the following:

apc screen How to install Alternative PHP Cache

APC Cache options

Here is some more information about the various options that can be used to setup apc cache.

apc.enabled This can be set to 0 to disable APC. This is
primarily useful when APC is statically compiled
into PHP, since there is no other way to disable
it (when compiled as a DSO, the zend_extension
line can just be commented-out).
(Default: 1)

apc.shm_segments The number of shared memory segments to allocate
for the compiler cache. If APC is running out of
shared memory but you have already set
apc.shm_size as high as your system allows, you
can try raising this value.
(Default: 1)

apc.shm_size The size of each shared memory segment in MB.
By default, some systems (including most BSD
variants) have very low limits on the size of a
shared memory segment.
(Default: 30)

apc.optimization The optimization level. Zero disables the
optimizer, and higher values use more aggressive
optimizations. Expect very modest speed
improvements. This is experimental.
(Default: 0)

apc.num_files_hint A “hint” about the number of distinct source files
that will be included or requested on your web
server. Set to zero or omit if you’re not sure;
this setting is mainly useful for sites that have
many thousands of source files.
(Default: 1000)

apc.ttl The number of seconds a cache entry is allowed to
idle in a slot in case this cache entry slot is
needed by another entry. Leaving this at zero
means that your cache could potentially fill up
with stale entries while newer entries won’t be
cached.
(Default: 0)

apc.user_ttl The number of seconds a user cache entry is allowed
to idle in a slot in case this cache entry slot is
needed by another entry. Leaving this at zero
means that your cache could potentially fill up
with stale entries while newer entries won’t be
cached.
(Default: 0)

apc.gc_ttl The number of seconds that a cache entry may
remain on the garbage-collection list. This value
provides a failsafe in the event that a server
process dies while executing a cached source file;
if that source file is modified, the memory
allocated for the old version will not be
reclaimed until this TTL reached. Set to zero to
disable this feature.
(Default: 3600)

apc.filters A comma-separated list of POSIX extended regular
expressions. If any pattern matches the source
filename, the file will not be cached. Note that
the filename used for matching is the one passed
to include/require, not the absolute path. If the
first character of the expression is a + then the
expression will be additive in the sense that any
files matched by the expression will be cached, and
if the first character is a – then anything matched
will not be cached. The – case is the default, so
it can be left off.
(Default: “”)

apc.enable_cli Mostly for testing and debugging. Setting this enables APC
for the CLI version of PHP. Normally you wouldn’t want to
create, populate and tear down the APC cache on every CLI
request, but for various test scenarios it is handy to be
able to enable APC for the CLI version of APC easily.
(Default: 0)

apc.max_file_size Prevents large files from being cached.
(Default: 1M)

apc.stat Whether to stat the main script file and the fullpath
includes. If you turn this off you will need to restart
your server in order to update scripts.
(Default: 1)

apc.write_lock On busy servers when you first start up the server, or when
many files are modified, you can end up with all your processes
trying to compile and cache the same files. With write_lock
enabled, only one process at a time will try to compile an
uncached script while the other processes will run uncached
instead of sitting around waiting on a lock.
(Default: 1)

apc.rfc1867 RFC1867 File Upload Progress hook handler is only available
if you compiled APC against PHP 5.2.0 or later. When enabled
any file uploads which includes a field called
APC_UPLOAD_PROGRESS before the file field in an upload form
will cause APC to automatically create an upload_
user cache entry where is the value of the
APC_UPLOAD_PROGRESS form entry.
(Default: 0)

The offical PHP manual page can be found here.

June 9, 2009   4 Comments

How to use the digg API

digg How to use the digg API

This article will show you how to use the digg API using PHP. I have also written a library in PHP that maps all of the endpoints documented in the API to easily accessible functions (it is also compatible with PHP 4).

What can you do with the digg API?

  • Get all popular stories submitted after January 1, 2007 sorted by promotion date
  • Get the most recent 100 upcoming stories in the topic “Apple”
  • Get the first 20 stories that were promoted on or after December 25, 2005
  • Determine whether a story, identified by its URL, has been submitted to Digg and, if so, get details like the number of Diggs and comments it has received
  • Get the details of a story, identified by its URL on Digg
  • Get all stories submitted today from nytimes.com
  • Get all stories submitted by Kevin Rose (or another Digger)

How it works

Requests are sent to the digg servers through URLS that are described in the API doc (here).

Here is an example:

http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com

This will return all stories. By default only 10 will be returned at a time (which can be increased to a maximum of 100). Also, you may notice an appkey=XXX argument. This is a unique identifier required by the digg servers. It is recommended to use the urlencoded refereral URL. Other return types are also available, which can make parsing easier depending on the language used.

response types:

  • XML: Easily parsed in many languages on many platforms. It is particularly easy to use in Flash applications.
  • JSON: May be directly eval’d in Javascript, and also can be parsed in many languages.
  • Javascript: Useful as the source of a script tag, it passes JSON response to the Javascript callback function you specify.
  • Serialized PHP: Easily unserialized in PHP to create objects, to which the programmer can attach custom methods. Or the programmer can directly access the response data through the public properties of the objects.

example:

http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com (will return XML by default)
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=json
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=php
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=javascript

Other basic arguments

    count
    Number of events to retrieve.
    Event count integer
    Default: 10, Maximum: 100.
    offset
    Offset in complete events list.
    Event offset integer.
    Default: 0.

PHP code

The following is a function that will allow you to connect to the digg service and parse the results. It also will automatically generate the appid based on the current location of the script.


function connect($hostname) {
    //this is an ID that is unique to your app.  It is auto-generated, based on the calling URL
    $appid = urlencode(&quot;http://&quot;.$_SERVER[&quot;HTTP_HOST&quot;].$_SERVER[&quot;REQUEST_URI&quot;]);
   
    $host = $hostname.&quot;&amp;appkey=&quot;.$appid;
    echo $host;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_URL,$host);
    curl_setopt ($ch,CURLOPT_USERAGENT,"Test library");
    curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,60);
    $response = curl_exec ( $ch );
    curl_close($ch);
   
    return $response;
}


Download

The digg API toolkit can be downloaded Here

May 6, 2009   7 Comments

5 ways to extend firefox tabs

The following are 5 Firefox extensions that will extend tab functionality.

1) IE tabs

ietab 5 ways to extend firefox tabs

This is a great tool for web developers that allows you to view web pages in Internet Explorer within a firefox tab.

Download Here

2) Informational tab

informtab 5 ways to extend firefox tabs

This extension provides thumbnail-style preview for each tab, does progress meter, and indicates unread status.

Download Here

3) Tab Mix Plus

tabmix 5 ways to extend firefox tabs

Tab Mix Plus enhances Firefox’s tab browsing capabilities. It includes such features as duplicating tabs, controlling tab focus, tab clicking options, undo closed tabs and windows, plus much more. It also includes a full-featured session manager with crash recovery that can save and restore combinations of opened tabs and windows.

Download Here

4) TabSidebar

Features:

  • Provides navigation options for each tab including history, stop and reload.
  • Allows you to move tabs around with drag and drop.
  • You can drop links, local files and bookmarks anywhere you like in the tab list.
  • Displays the security status of tabs.
  • Automatically refreshes the tab preview whenever the page changes.
  • Lets you hide the main tab bar when the sidebar is open.
  • Bidirectional support making the sidebar work correctly in right-to-left languages.
  • Works well with other tab-related extensions allowing you to use their context menu additions from the sidebar.

Download Here

5) Tab Effect

tabeffect 5 ways to extend firefox tabs

This adds a tab switching cube effect to Firefox.

Download Here

May 4, 2009   No Comments

How to break the DiggBar

Recently, digg.com added a new feature called the “DiggBar”.

It frames your page and auto-generates a short url for your content. The issue many people have with this is that the user never leaves the Digg website, which is bad for SEO. This is because:

  • Users will most likely link to the shortened Digg URL, and not your site
  • Even when a user clicks on your links, it still seems like it is part of digg, not a separate, external link.
  • Eventually, they could place advertising on this toolbar, competing against any ads on your page

digg How to break the DiggBar

Recently, because of many criticisms, Digg has changed the behavior of the bar (taken from the Digg blog):

1. New treatment to the behavior of Digg short URLs. All anonymous users, on or off Digg will be taken directly to the publishers content via a permanent redirect (301), no toolbar, straight to the site. Logged in users that have not opted out will continue to see the DiggBar (200). These changes ensure that content providers receive full search engine ‘juice’ or credit for all links on and off Digg. They also ensure that Digg short URLs won’t appear in the indexes of any major search engines.

2. Because we want to ensure the best user experience, the DiggBar will soon only be shown to you when you are logged into Digg. While the vast majority of Digg users find the DiggBar valuable (only a very small number of users have disabled the feature or hit close with any frequency) we understand that many folks were confused when opting out. We want you to be able to have the option to permanently disable the DiggBar with ease. For registered Digg users receiving the bar, we are also making a few changes to make the process more obvious.

However, people can still get to your content through their service if they are logged into their account when viewing.

The following code will remove the DiggBar (and auto-redirect to your actual content) the user to the actual link. To use, place in the header of your website. It will also prevent other sites from displaying your content in an iframe.


<script LANGUAGE="javascript" type="text/javascript">
if (top!= self) top.location.href = location.href;
</script>

April 28, 2009   No Comments