PHP

Jump Into Coding

Tagged:  
Jump Into Coding
Over at The Next Web blog they have put together a good resource for people wanting to learn to program. If you want to be a coder, but have no idea where to start this post will be a great help for you. The post discusses how to choose a first programming language and resources to learn about it. The post will be very helpful to anybody new to coding, but even those that code for a living may find a few things that are helpful to learn to program.

Check out the post and let us know what you found useful in the post in the comments below.

You can go to the referenced source of this post here.

Post written by Ajaxonomy using scovry

Making a Server Side Image Cache in PHP

Tagged:  

Yesterday, I wrote about an application that I wrote called scovry and I mentioned that in writing the application I used a lot of caching. This included caching of images from an external thumbnail service server.

I thought that I would post the code that I am using to do this and quickly go over how it works. This code might be useful to any application that loads images from another server, to lessen the dependency on the other server. Below is the code that caches the images.

<?php
$imageurl =urldecode( $_GET['imageurl']);

$cachefile = 'caching_folder/'.urlencode($imageurl).'.jpg';
$cachetime = 604800;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    header("Content-type: image/jpeg");
    readfile($cachefile);
    exit;
}
ob_start(); // Start the output buffer

$imginfo = getimagesize($imageurl);
header("Content-type: image/jpeg");
readfile($remoteImage);

// Cache the output to a file
$fp = fopen($cachefile, 'wb');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush(); // Send the output to the browser
?>

The above code is used like an image proxy, but it adds the benefit of doing the image caching. So, the first thing to notice is that in my application the imageurl variable that is passed in through the url query string is an encoded url, so we decode it. This variable will hold the url encoded url of the image that we want to load. You may also notice that this code assumes that the image is a JPEG image, because that is what I was always going to be dealing with. So, if you use this code, you may need to add code to handle other image types.

The next import variable is the $cachetime as it sets how long (in seconds) the image will be cached before the cache is refreshed. The if statement checks if it should load the image on disk. The important part here is that it sets the document header content type to be an image and then loads the image using the readfile() method.

Now If the file does not exist or if it needs to be re-cached it loads the file from the server and writes it to the output buffer. Once again the document header content type is set to an image value and it gets the file via the readfile() method. In addition to this it writes the file to disk using the fopen() and fwrite() methods.

That is basically all there is to it. You can take a look at it in action by checking out scovry.

What I Did on My Winter Holiday

This winter break (or as we used to call it when I was still in school, Christmas vacation), I worked on a little web application. The application was going to be for an automotive related blog that I write on, but once I started working on it I realized that the application was better if it wasn't restricted to just automotive information.

The application is called scovry and it is a web discovery application. You may notice that we at Ajaxonomy love spy applications (notice that in Ajaxonomy labs we wrote delicious Spy and TubeSpy). scovry is kind of a blend of these spy applications along with gathering information from many other social media sites and social networks. Beyond this the site adds a social element by allowing comments and easy sharing of items found on the site.

scovry

From a programming side, in order to load all of the data and keep the server happy I used a lot of caching. The caching code I used was based on the Easy Server Side Caching in PHP article that I wrote a while back. With some small changes, the biggest of which was using readfile() instead of include(). I even modified the caching script to cache images that are loaded from the thumbnail service to make loading much faster (not to mention to reduce requests for images).

You can check out scovry here.

Is PHP Good Enough for Your Startup?

Tagged:  

When it comes to web development people are in quite a few camps when it comes to web development languages. For the most part the major camps right now are .NET, Java, Ruby, Python and PHP. For many PHP is the lowest web development language on the list and some developers are somewhat embarrassed to admit that they program in it (many think it is a language for hacks).

I personally think that if PHP is done properly, it is a very good viable web development language. The question is if you think that PHP is good enough for your startup. Would you be embarrassed when others ask what your site or application is written in, or would you proudly proclaim that you used PHP?

The startup ChartBoost that deals in on-line advertising has written a good post about this subject. They use PHP and it is interesting to read their thoughts.

Below is an excerpt from the post.

Running a Modern Startup on PHP

In the modern world of agile startups and silicon valley, the buzz is all about Ruby, Python, and whatever the latest cool programming language or framework to come out is. Older technologies don’t get much love, and PHP especially has a bad reputation. In this post, I’m gonna go over why and how we use PHP as a modern technology, and the various other tools, techniques and development methodologies we employ to run as agilely and elegantly.

You can read the full post here.

So, what web development language is good enough for your startup?

Ajax File Upload Tutorial

Tagged:  

As you are writing an Ajax application, you may want to allow users to upload files through an Ajax form. The folks over at AnyExample.com have put together a good tutorial on the subject. The tutorial uses PHP for the back end, but the principal is the same for any server side language.

Below is an excerpt from the tutorial.

How IFRAME file uploading works?

  • There is a simple <form... which contains only <input type="file" ... > field.
    Target for this form is a hidden IFRAME (with "display: none;" CSS style) and
    OnChange event for the file field is set to JavaScript function which checks file extension (optional for this example, but
    very useful in general) and submits form.
  • Special part of the script (marked FILEFRAME, see comments) saves file upload, checks for uploading errors and outputs JavaScript code to that
    hidden IFRAME.
    The javascript code uses parent.window.document object, which allows to modify parent document (visible page, which users is viewing).
    It sets filename value and enables submit button on the other form using getElementById method.
  • The other form has 'description' text-field and hidden field 'filename'. User may fill 'description' field while file is uploading.
    When file uploading is finished, user press submit and "file information" page is generated (based on filename from hidden field and user's
    file description).

Possible drawback of this method is file garbage: files are uploaded even if user does not press submit button. You may need to write 'garbage file
collector' which will delete any unused file.

This example stores all uploaded file in filesystem folder. You need to specify it at the beginning of script, see variables $upload_dir and
$web_upload_dir. There is fail-checking which checks whether it is possible to write create files in upload directory.

We use following functions in this example:

PHP

  • move_uploaded_file - move file uploaded to web server
  • fopen - open file
  • fwrite - write to opened file
  • fclose - close file
  • str_replace - replace one substring by another
  • filesize - returns file size in bytes
  • filemtime - returns file modification time

You can read the full post here.

15 PHP regular expressions for web developers

Tagged:  

Regular Expressions can be very useful tools for web developers. However, they can be a bit tricky to use, especially when you are not very experienced in web or software development. Well, over at Cats who Code they have put together a list of 15 useful regular expressions for web developers. The code is written in PHP, but should be fairly easy to translate to other languages.

Below is an excerpt from the post.

For many beginners, regular expressions seems to be hard to learn and use. In fact, they're far less hard than you may think. Before we dive deep inside regexp with useful and reusable codes, let's quickly see the basics:

Regular expressions syntax

Regular Expression Will match...
foo The string "foo"
^foo "foo" at the start of a string
foo$ "foo" at the end of a string
^foo$ "foo" when it is alone on a string
[abc] a, b, or c
[a-z] Any lowercase letter
[^A-Z] Any character that is not a uppercase letter
(gif|jpg) Matches either "gif" or "jpeg"
[a-z]+ One or more lowercase letters
[0-9.-] ?ny number, dot, or minus sign
^[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number or _
([wx])([yz]) wy, wz, xy, or xz
[^A-Za-z0-9] Any symbol (not a number or a letter)
([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers

 

PHP regular expression functions

Function Description
preg_match() The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise.
preg_match_all()

The preg_match_all() function matches all occurrences of pattern in string.
preg_replace() The preg_replace() function operates just like ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.
preg_split() The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.
preg_grep() The preg_grep() function searches all elements of input_array, returning all elements matching the regexp pattern.
preg_ quote()

Quote regular expression characters

You can read the full post here.

A Linux Web based tool completed in just 2 weeks

A new Visual WebGui RIA Platform showcase is published:
A Linux (Mono) Web based OS Deployment tool completed in just 2 weeks

The company needed to develop a solution which would operate as an OS Deployment tool that is customized for the needs of their Datacenter according the specific design parameters given.

The team decided not to use PHP but to use Mono (mod_mono) with Visual WebGui because of the short timeframe they had for deploying the solution and since Visual WebGui provided the easiest and quickest graphical development possible.

The web development process was straight forward. While the final goal was well defined, some of the concepts and functions were being defined while coding the solution. The Visual WebGui unique approach enabling easy integration of UserControls and intuitive graphical development allowed for this unstructured development cycle which led to an extremely shortened process as stated by Axel Westerhold: "Because of the easy graphical development environment and the integration into Visual Studio we were able to roll out the product within 2 weeks."

Go to the full case study.

Having Fun with Pligg - Installing

Last time I told you that I was setting up a Pligg site for my new video game site, wastingtimegames.com. This is second in a series of posts where I will explain how to get a working customized Pligg site.

To install Pligg you will first need to download the files from pligg.com. The most current release is 1.0.0 RC2. Once you download the .zip file you will need to unzip all of the files into a folder.

To get the rest of the software installed you will just need to follow the below directions, taken from the Pligg readme file.

  1. Create a mysql database. If you are unfamiliar with how to create a mysql database, please contact your web host or search their support site. Please pay careful attention when creating a database and write down your database name, username, password, and host somewhere.
  2. Rename settings.php.default to settings.php. Do the same for /libs/dbconnect.php.default.
  3. Upload the files to your server (please note that your server will need to be running PHP 4.3.0 or higher).
  4. CHMOD 755 the following folders, if they give you errors try 777.
    • /admin/backup/
    • /avatars/groups_uploaded/
    • /avatars/user_uploaded/
    • /cache/
    • /cache/admin_c/
    • /cache/templates_c/
    • /languages/ (And all of the files contained in this folder should be CHMOD 777)
  5. CHMOD 666 the following files
    • /libs/dbconnect.php
    • settings.php
  6. Open /install/index.php in your web browser. If you are reading this document after you uploaded it to your server, click on the install link at the top of the page.
    • Select a language from the list.
    • Fill out your database name, username, password, host, and your desired table prefix.
    • Create an admin account. Please write down the login credentials for future reference.
    • Make sure there are no error messages!
  7. Delete your /install folder.
  8. CHMOD 644 libs/dbconnect.php
  9. Open /index.php
  10. Log in to the admin account using the credentials generated during the install process.
  11. Log in to the admin panel ( /admin ) and you will then be presented with information intruducing you to Pligg.
  12. Configure your Pligg site to your liking. Don't forget to use the Modify Language page to change your site's name.

Once you are done with these steps you'll have a basic Pligg site running. Next time I'll explain how to change the language features and install modules.

Having Fun with Pligg

Tagged:  

Pligg is a content management system that has evolved from a Digg clone to a full feature content management system. I've been working on some on-line video games and have been working on a site to host them. After some thought about the site, I decided to use Pligg and to allow users to submit their own game content as well as post my games.

The Pligg install is very easy to get up and running. So, my next few posts will be about the Pligg install and how I will modify the template and add modules to the site.

You can see the site that I've setup at wastingtimegames.com. The site is fully functional although I will be adding new features over the next few weeks and will be posting my games soon. So, stay tuned to see how to get your own Pligg site running quickly.

You can learn more about Pligg here.

Eclipse Announces PDT 2.0 Release

Tagged:  

The Eclipse Foundation has announced the 2.0 release of the Eclipse PHP Development Tools project (PDT). The project is sponsored by Zend and forms the basis of the commercial product Zend Studio.

Major features of this release include:

  • A Hierarchy view for navigating the PHP object hierarchy.
  • Easy searching for PHP types.
  • Override indicators for PHP methods that override other methods.
  • Performance improvements from using a new indexing and caching engine from the DLTK project.
  • Improved code completion.

The PDT project is one of Eclipse's most popular downloads, with more than 1 million downloads to date. The plug-ins are available from the download page or through the download site at:

http://download.eclipse.org/tools/pdt/updates

Syndicate content