background preloader

Articles

Facebook Twitter

Generating MIME Type in PHP is not Magic :: Chris Jean. Sorry about the late post everyone. I had a long week and went to bed before making sure a post was queued. Hopefully you’ll forgive me. I’m working on a project where I needed to generate a MIME type given a file name. Not only did I need to create a solution that worked, I also needed the solution to be compatible with PHP 4/5 and not require any additional software to be installed on the host. Problems with finfo_open I found a very helpful PHP, Mime Types and Fileinfo post on Jelly and Custard. PHP Fatal error: Call to undefined function finfo_open() in ….

Since I have PHP 5.2.6 running on my dev server, I was very confused since the post said that this was a “PHP 4.x” solution. Turns out that the Jelly and Custard post points to another post, Installing PECL Modules, where instructions are given for installing the Fileinfo PECL Package. This is not what I want. Problems with mime_content_type I then found the mime_content_type function. Initial Testing Final Solution The Code Examples. Towards RESTful PHP - 5 Basic Tips | Kris Jordan. What is REST? REST is an architectural style, or set of conventions, for web applications and services that centers itself around resource manipulation and the HTTP spec. Web apps have traditionally ignored the HTTP spec and moved forward using a subset of the protocol: GET and POST, 200 OKs and 404 NOT FOUNDs.

As we entered a programmable web of applications with APIs the decision to ignore HTTP gave us problems we’re still dealing with today. We have an internet full of applications with different interfaces (GET /user/1/delete vs. Tip #1: Using PUT and DELETE methods In PHP you can determine which HTTP method was used with: $_SERVER['REQUEST_METHOD']; From web browsers this will be either GET or POST. Tip #2: Send Custom HTTP/1.1 Headers PHP’s header function allows custom HTTP headers to be sent to the client. Header('HTTP/1.1 404 Not Found'); header('Location: true, 201); // 201 CREATED The first line is a generic way of setting the response status code. Design Patterns in Web Programming. By Nathan Wallace (March 8, 2000) 1 Introduction I was hacking code and searching through the PHP mailing list archives late one night when I stumbled across the message calling for Apachecon speakers.

It was 5am and the deadline for submissions about one hour away. Suddenly the idea came to me - "Wouldn't it be interesting to try and identify patterns in web programming". It's not until after the talk had been accepted (and I'd had some sleep) that I started to really think the idea through. So, think of this as the first step in an ongoing process. Design patterns generally arise through solving problems.

Patterns are usually obvious to experienced programmers. Finally we will construct some formal web programming patterns. 2 Introduction to Design Patterns According to Christopher Alexander: So a design pattern is a formal description of a problem and its solution. Design patterns can be used to assist us in solving related problems. 3 Common Web Programming Problems 3.3.1 Form Processing. S blog: Handling Plugins In PHP. These patterns are designed to handle communication between disjoint objects. Now, one key point to note here, is that every listener would need to bind to the object instance that it wants to track. Another key point is that the Subject does not pass along any information about the cause of the update. This makes the Observer Pattern extremely useful for situations where you want to bind the state of one part of the application to the state of another.

A key example of this is in a stateful MVC pattern, you can bind a view instance to observe a model instance. That way, when a controller makes changes to that model instance, it will notify all the views that are listening that it changed and they need to re-render themselves. However, it should also be noted that the Observer Pattern creates a moderate coupling between the two classes involved (the subject and the observer) as they need to directly call methods on each other. Mediator Pattern A quick example: Functional Handlers An example: Ko3 ORM, Connections and Modeling « Village Builder. My catch-all for musings and experimentation in the realm of making connections and modeling. It might be worthwhile to give a bit of landscape about the particular database I am evaluating this ORM for, we’ll call it “cupcakeshop”.

It’s a decently active db, with a need to support higher activity, and not overly large in footprint. The schema is pretty disorganized and misnamed in certain areas, running on a rather old version of mysql. Being able to hide some of this disorganization behind some ORM smoke and mirrors would be awesome, particularly to minimize the impact on the code as the db gets reorganized.

Being able to run on a master and multi read-only slaves is also of high interest. mysql and pg connection (special guest: table name prefixing)column definitiontable name and column aliasingdefining 1to1, 1toN, NtoNmulti db and failover Mysql and Postgres connectivity Mysql support comes native with Kohana3, but using postgres requires the pg plugin. Model: Controller call: Whoohoo. Create ZIP File :: Create ZIP file archives and serve for download | Rochak Chauhan::Unpredictably Exciting. Building a PHPDoc parser in PHP – Murray Picton. Building a PHPDoc parser in PHP This is the second in a series of posts about how I built Doqumentor – The Runtime PHP Documentor. If you haven’t already, check out the first how I used the PHP Reflection API to document your code. So, the next step in the process of building Doqumentor was to build the PHPDoc parser to parse the comments in my code and provide a useful result that is easy to use and provides the functionality required.

First of all, I think we should go over what PHPDoc comments are: What is a PHPDoc comment? PHPDoc comments are defined in the PHPDocumentor website and can be found here. First step is to retrieve my comment from my PHP file using the reflection API as described in my previous post. This returns a string of our comment including the /** start and */ end tags. Getting the lines from the comment Now I need to remove the *s for the start of each line and parse each line to get the variables out. Saving and parsing the variables Parsing the rest of the comment. PHP Caching for dynamic content. Eigene Fehlerseiten gestalten » Weblog - Michael Jendryschik. Die HTTP/1.1-Spezifikation definiert eine ganze Reihe von Fehlercodes, darunter die Fehler 401 (Unauthorized), 403 (Forbidden) 404 (Not Found) und 410 (Gone). Die im Falle eines Fehlers angezeigten Standardfehlerseiten reichen nicht aus, um Nutzer mit den notwendigen Informationen zur Fehlerbehandlung und weiteren Vorgehensweise zu versorgen.

Dieser Artikel zeigt, wie Sie Ihre Besucher auf selbst gestaltete Fehlerseiten leiten und welche Informationen diese enthalten sollten. Einleitung Die erste Zeile (Statuszeile) des HTTP-Response eines Webservers entspricht stets folgendem Schema: Bei einer korrekt ausgelieferten Ressource sieht die Statuszeile in den meisten Fällen wie folgt aus: Die HTTP/1.1-Spezifikation definiert in Abschnitt 10 alle in Frage kommenden Statuscodes, darunter auch eine Handvoll Fehlercodes.

Zum Seitenanfang Standardfehlerseiten vermeiden Jeder Webserver bietet die Möglichkeit, eigene Fehlerseiten anzulegen. Die Direktive ErrorDocument Richtlinien für Fehlermeldungen. How To Optimize Your Site With HTTP Caching. I’ve been on a web tweaking kick lately: how to speed up your javascript, gzip files with your server, and now how to set up caching. But the reason is simple: site performance is a feature. For web sites, speed may be feature #1. Users hate waiting, we get frustrated by buffering videos and pages that pop together as images slowly load. It’s a jarring (aka bad) user experience. Time invested in site optimization is well worth it, so let’s dive in. What is Caching? Caching is a great example of the ubiquitous time-space tradeoff in programming. In the case of websites, the browser can save a copy of images, stylesheets, javascript or the entire page. Here’s a quick refresher on how a web browser gets a page from the server: 1.

(The actual HTTP protocol may have minor differences; see Live HTTP Headers for more details.) Caching’s Ugly Secret: It Gets Stale Caching seems fun and easy. Wrongo. We’ve got a problem. So why bother caching if we can’t be sure if the file is good? 1. 1. 1. 10 super useful PHP snippets. Super simple page caching When your project isn’t based on a CMS or framework, it can be a good idea to implement a simple caching system on your pages. The following code snippet is very simple, but works well for small websites. <? Php // define the path and name of cached file $cachefile = 'cached-files/'.date('M-d-Y').'.php'; // define how long we want to keep the file in seconds. I set mine to 5 hours. $cachetime = 18000; // Check if the cached file is still fresh. » Credit: Wes Bos Calculate distances in PHP Here is a very handy function, which calculate the distance from a point A to a point B, using latitudes and longitudes.

Usage: echo distance(32.9697, -96.80322, 29.46786, -98.53506, "k"). " kilometers"; » Credits: PHP Snippets.info Convert seconds to time (years, months, days, hours…) This useful function will convert a time in seconds to a time in years, months, weeks, days, and so on. » Credits Force file download » Credit: Alessio Delmonti Get current weather using Google API. Screenshots von Webseiten erstellen mit PHP | PHP Gangsta - Der PHP Blog. Blog Tutorial with Fat-Free Framework | Richard Willis-Owen. View the blog tutorial updated for version 3 here I recently read an excellent tutorial on ‘Rapid Application Prototyping in PHP Using a Micro Framework‘, this used the Slim micro framework but one thing that bothered me was it required 5 packages before development could start.

These were: Slim (the micro framework), Slim Extras (additional resources for Slim), Twig (template engine), Idiorm (object-relational mapper) and Paris (Active Record implementation). It struck me that if you need an extra 4 packages alongside Slim for a basic blog then maybe it is a little too skinny and I set out to find a micro framework that could do the same thing without these dependencies. I found the Fat-Free Framework. It is condensed into a single 55KB file and has a host of features, you can find out about these on their web site so I won’t repeat it here. Instead I’ve reproduced the Slim tutorial to create a simple blog site, but using Fat-Free Framework instead.

Step 1: Setup Step 2: Bootstrapping. Compress PHP, CSS, JavaScript(JS) & Optimize website performance. Since few days we have been registering heavy traffic spikes on our website. This lead to performance issues. As this site is currently hosted on a shared hosting server, it is very difficult to optimize the performance of the site. We are using as CMS for this blog, hence we decided to install for WordPress to improve the performance. This plugin will create static HTML files from your blogs post and other pages and save them on web server. These HTMLs are served to client whenever consecutive requests are made.

Hence this greatly improve the performance as it reduce PHP parsing and database connections. Bandwidth control is an important task to be followed when your traffic is increasing. So how can you enable GZip compression and compress your websites output? GZip compression in Tomcat, JBoss server You can find a full post explaining this trick . GZip using mod_gzip, mod_deflate and htaccess You can enable compression for text and html by adding following lines in your htaccess file. PHP Fat-Free Framework: Getting Started. Readme.md A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast! Condensed in a single ~60KB file, F3 (as we fondly call it) gives you solid foundation, a mature code base, and a no-nonsense approach to writing Web applications. Under the hood is an easy-to-use Web development tool kit, a high-performance URL routing and cache engine, built-in code highlighting, and support for multilingual applications.

It's lightweight, easy-to-use, and fast. Most of all, it doesn't get in your way. Whether you're a novice or an expert PHP programmer, F3 will get you up and running in no time. F3 supports both SQL and NoSQL databases off-the-shelf: MySQL, SQLite, MSSQL/Sybase, PostgreSQL, DB2, and MongoDB. That's not all. Unlike other frameworks, F3 aims to be usable - not usual. Table of Contents Version 3.2 Is Finally Here! The latest official release marks a major milestone in the development of the Fat-Free Framework. Getting Started.