Php

Using iWatch to Automatically Run Unit Tests (Linux)

1 min read

iWatch is a perl script that uses inotify to monitor files directories. It’s similar to the watch tool, which can do all sorts of stuff if the files or directories it’s watching are modified or affected in pretty much any way at all.

Install iWatch

apt-get install iwatch

I’ve got this 1 liner in a file to quickly watch my directory and execute a PHP unit test .

#!/bin/bash
iwatch -c "phpunit $1" -t '.*php$' -r -v -x ./.git -e modify .

I run this with a my argument (a unit test) and then sit there and code away. When I save, it detects the change and automatically runs my test. It’s pretty awesome.

linux php tips
Read more

Setting Up Default Cron Path

1 min read

I’ve run into a ton of issues working with crons, mostly with the $PATH variable screwing things up. Scripts work when run manually on the command line, but fail when run in cron. Very annoying.

I’ve asked a bunch of Linux sys admins how to fix this - and the answer is always “put the full path in your scripts” which to me in unacceptable as it introduces the possibility of human error. Fixing the underlying problem is always preferred.

linux php
Read more

UIS PHP53, DOM Parsing, PHPUnit, Puppet Config Solution

1 min read

I’m using PHP53 package from the IUS Community repository. I’ve been trying to get phpunit to install, but it gives an error that it needs DOM install. It took me a little bit to figure this out, but I finally got it working. What you need is the php53-xml package. You can install it using

yum install php53-xml

or if you’re using puppet

package { ["php53-xml"]:
          ensure => present
        }

And finally, to get it to install, I used the below. I had to make it go to multiple lines to fit on the page but I have it all on 1 line in my puppet script:

linux php puppet
Read more

Working with DOM in PHP - Looking at a PHP HTML Parser

2 min read

So, lets assume you’ve got a PHP project where you’re scraping pages and trying parse fields out of the DOM.  Up till now, I’ve just used regular expressions because they’re easy.  I avoided trying to parse html as xml using SimpleXML because there’s just to many cases where it would fail due to invalid tags.

Well, I feel like an idiot.  It turns out there’s a great extension built into PHP to do just that, and it’s the DOM extension.  Using this, parsing HTML with PHP is just as easy as accessing the DOM using JQuery. (hint: very easy).

html php
Read more

Geek Links

1 min read

This looks like a pretty cool tool to get a good idea of what’s going on in your PHP script without having to install tools like XDebug and Webgrind / KCacheGrind.

I’m not sure how this is anything new, tons of applications have their own volume setting. Quicktime, VLC, Windows Media Player.

Doesn’t seem like such a bad idea to get out now. It’s essentially just a giant ad for Fox TV shows and movies.

apple links php
Read more

VMWare, Losing Time, and Sessions

1 min read

I’ll keep it short. In the last few days, login on our dev server broke. We hadn’t changed anything related to it, and everything looked good code wise. What we finally figured out was that our session cookie was set to expire 2 days into the future, and our VMWare image had lost 2 days of time.

Fix by doing the following:

ntpdate ntp.nasa.gov > /dev/null

php vmware
Read more

' PHP Warning: Memcache::set(): Failed to extract ''connection'' variable'

1 min read

I got this today. Solved by restarting the Memcached server. Move along.

Edit: this is actually a reoccurring bug we’re seeing with the memcache 2.2.3 stable build on 2 different boxes 2nd Edit: Actually it was a bug in my code. I wasn’t setting the server and ip correctly, there was a typo in my configuration

memcached php
Read more

How curl_exec and urlencode killed my single sign on

1 min read

If you do any work with single sign on, you’ll be familiar with the concept of exchanging tokens and validating against the authentication server using that token. One of the issues I’ve just run into which resulted in a huge headache is with urlencoding the result of a curl_exec that had a line ending. It’ easy to miss when it’s a longer string and you aren’t paying very close attention. This is a very simple example, and it still takes a second to realize there’s an extra character at the end.

curl php
Read more

Executing multiple curl requests in parallel with PHP and curl_multi_exec

2 min read

Let’s get one thing out in the open. Curl is sweet. It does it’s job very well, and I’m absoutely thrilled it exists.

If you’re using curl in your PHP app to make web requests, you’ve probably realized that by doing them one after the other, the total time of your request is the sum of all the requests put together. That’s lame.

Unfortunately using the curl_multi_exec is poorly documented in the PHP manual.

curl php
Read more

Getting phpsh to work on a mac

1 min read

I had an issue getting phpsh to work on my mac - I kept getting the following error:

Traceback (most recent call last): File “./phpsh”, line 20, in import readline

OK, seems easy enough. So I compiled python with readline support.

./configure –prefix=/usr/local/python –enable-readline

I change the PATH variable in my .bash_profile to point to the /usr/local/python directory first, and source’d it to get the new PATH settings. Still get the same error.

php python
Read more

ob_start() causes phpsh to hang, sort of

1 min read

If you manually call ob_start() at the beginning of your script, you might notice that you are unable to use phpsh. By commenting it out, I was able to fix the issue.

I don’t think it technically hangs, it just sits there with the data in a buffer waiting to be flushed.

php
Read more

'PHP: An Array of Months'

1 min read

I’ll be honest, this isn’t very useful. The goal was to have an array of months in the least amount of code.

for($i = 1; $i <= 12; $i++) $months[$i] = date('F', strtotime("{$i}/01/2000"));

php
Read more

Reading Raw Post Data in PHP

1 min read

Perhaps you’d like to just sent XML over post, and not have xml=your_nodes_and_stuff_here. No sweat.

$xml = urldecode(file_get_contents('php://input'));

Enjoy.

This can now be called with:

curl -d "some_xml_here" http://yoursite.com/pagename

php
Read more

Motorola RAZR and 406 Not acceptable

1 min read

In trying to get a site working on cell phones, I got a “406 Not acceptable” only on the Motorola RAZR. The site loaded fine on my Treo 755 and every Blackberry I’ve tried.

It seems that the RAZR doesn’t handle Multiviews very well, or at all for that matter. If you’re using Multiviews and Apache, you’ll have to specify the full URL: so /login.php rather just /login.

apache mobile php
Read more

'PHP 4: session_start(): Failed to initialize storage module'

2 min read

I’m migrating a PHP 4 site right now. It makes me realize how far PHP has come in the last few years.

I ran across a weird bug where code was working on our dev server, but not production. I was getting the error:

session_start(): Failed to initialize storage module

We’re using memcache to store our sessions, so it’s a user storage module.

Luckily, I stumbled across this helpful post. [edit: I only checked the page on our production box directly (not going through the load balancer) after I made this change, so it seemed to fix the problem, but the issue was really caused by the fact that I wasn’t defining the custom session handler when it was hit by the LB]

php
Read more

'PHP: Setting Up XDebug with KCacheGrind'

2 min read

KCacheGrind is a very useful tool to identify bottlenecks in your applications. This will explain the steps to using it to find issues with your PHP scripts. For me, the scripts are all web pages.

I’m already assuming you’re running a current version of PHP. I did this using PHP 5.2.1. These instructions are based on a Unix/Linux server, if you’re running Windows I can’t help you.

**Step 1: Install XDebug. **

linux php
Read more

PHP compile error on CentOS Install

1 min read

I installed CentOS 5 on my VMWare a few days ago. I installed gcc via yum, compiled and installed libxml2. I then tried to install PHP 5.2.3 and received this error:

configure: error: installation or configuration problem: C++ compiler cannot create executables.

It took me forever to figure this out, but I had to install the g++ library, then it compiled fine.

yum install gcc-c++

linux php tips
Read more

10 (very) Basic Tips for PHP Web Application Security

2 min read

Barebones list for the things you should be looking for when developing a web app in PHP. Some of it applies to all web apps, not just PHP.

  1. Use HTML_Safe (or a similar javascript stripping library) to check for and remove javascript when you’re accepting data that will be output to a page.

  2. Check every get and post variable for validity.

Every web site has url like “dosomething.php?id=3”. Make sure that id you’re accepting is actually a number (or whatever type you’re allowing).

apache php tips
Read more

What caused that load spike?

1 min read

Every now and then, we find that we will have a sudden increase in the number of apache processes, load average will spike up, and then go back down to normal. In rare cases, we will see the same thing happen, and the load avg spike WAY up, all queries appear locked up, and the server must be rebooted. I am looking for ways of determining what caused this. I should note that it happens extremely rarely, and has never shown up in a load test.

apache linux mysql
Read more

Calling a function from within preg_replace

1 min read

Regular expressions are awesome. However, sometimes doing everything in them is extremely difficult, or impossible. Luckily, we can flex the power of preg_replace’s ’e’ option to execute the replacement string as PHP code.

The reason why I found this is was I was looking for a suitable URL auto-linker that could shorten the url to a certain length. I found several examples on PHP.net, but unfortunately none of them did a good job of shortening the URL, or formatting it in a special way. This particular example is a simplified version of my auto-linker.

php
Read more

Pretty URLs using Multiviews

2 min read

I saw a post on digg.com about how it’s nice to have “pretty urls” that are easy to tell people. While i’m not sure anyone would remember rustyrazorblade.com slash some ridiculous post name, it’s nice to have for search engines. Unfortunately, it didn’t really go into detail about how to accomplish the urls. A lot of people suggested mod_rewrite. Yes, it’s something you can use, but kind of a pain to set up.

apache php tips
Read more

PHP 5.1.4 Install on Intel Mac (Bug?)

1 min read

I noticed after installing PHP 5.1.4 on my brand new MacBook Pro that my scripts weren’t executing. After about 3 hours of messing around, I realized that short_open_tag was turned off in php.ini.

Location: /usr/local/php5/lib/php.ini

Should be: short_open_tag = On

Not sure if this is a common issue, or if the PHP defaults have been changed. I installed the package distributed by Marc Liyanage, found at his homepage.

_Edit: According to the php web site, this should be on by default.  _

php tips
Read more