Posts in the “php” category

Solution for PEAR or PECL "is using a unsupported protocal (sic)"

Summary: A solution for "pear/pecl is using a unsupported protocal (protocol)".

While trying to install the APC Opcode Cache on a server yesterday, when I tried to run this command:

pecl install apc

I kept getting this error message:

pecl.php.net is using a unsupported protocal - 
This should never happen.
install failed

After doing some research, it looks like you can also get a similar error message with PEAR:

PHP ping script examples

While working on a shared hosting server, I found that the company I'm working with has disabled my access to the Unix/Linux ping command. Not to be deterred, I found several different ways to run a ping command with a PHP script.

Use the Net_Ping module

The first thing you can try to do is use the PHP PEAR Net_Ping module to get around this problem. In short, you install it like this:

pear install Net_Ping

Then use it like this:

Get a web client's IP address with PHP

I just ran into a situation for a Drupal/PHP client where I wanted to log some access information. There was some unusual access activity on the website, and I want to log IP addresses and URLs for a few days.

As part of that, I wanted to get the web client's IP address, and in PHP you get the client IP address like this:

$ip = $_SERVER['REMOTE_ADDR'];

That returns something like this:

192.168.100.10

If you just need to get a web client's IP address from a PHP script, that's all you need.

How to display the APC Opcode Cache information page

I just finished moving the alvinalexander.com website to a newer, faster web server, and in the process I had to install the APC Opcode Cache on the new server. After installing APC, I like to be able to test that it's working, so I wanted to be able to display the APC information web page (APC test page).

To display the APC test/information page, just:

Free Flash charts and graphs (PHP charts)

Free PHP Flash charts FAQ: What do you know about Flash charts (Flash charts and graphs tools that can be used in PHP applications), especially free Flash charting and graphing tools (or interactive charts and graphs)?

As I keep digging into free PHP charts and graphs tools, I've found that several of these tools special in "Flash charts", typically interactive Flash charts. Here's a quick review of what I've learned about these Flash charts.

Free PHP Flash charts and graphs tools (Open Source)

Open Flash Chart Project

A PHP preg_match integer regex pattern matching example

PHP integer FAQ: Can you demonstrate a regular expression pattern to match an integer?

Interestingly, while reading a book titled Advanced PHP Programming, the author mentions that the PHP is_int function doesn't work exactly the way you expect it to. It just tests to see whether a given variable is typed as an integer or something else, so if you give it a string like "123", it will return false. This is a bad thing when you're trying to test web form data, for example.

CakePHP Auth login - User login information

CakePHP Auth user login FAQ: Help, how do I get access to the CakePHP user object (user information) after using the CakePHP Auth component for the recommended CakePHP user login process?

CakePHP find conditions (plus find all, find list, and find count)

CakePHP find FAQ: Can you share some "CakePHP find conditions" examples?

One thing you need to get used to when working with CakePHP is the CakePHP find method. To that end, I thought I'd put together this page of CakePHP find conditions and find query examples.

CakePHP find conditions

To get started, here's a list of the possible CakePHP find conditions you can use:

CakePHP field required validation rule

CakePHP field required validation rule - How to make a CakePHP view field a required field.

I'm still very new to CakePHP, and as a result I just spent thirty minutes fighting with a CakePHP field required rule. In short, if you want a text field to be required, your CakePHP validation rule should look something like this:

PHP cannot redeclare function error message

PHP function FAQ: Help, I'm getting a PHP cannot redeclare function error message, how do I fix it?

Assuming you don't really have two PHP functions defined with the same name, this "cannot redeclare" function error message is usually caused by using the require or include functions to include the same common file (and therefore its functions) more than once.

CakePHP jQuery - A default input focus onLoad solution

CakePHP jQuery FAQ: How do I get jQuery to work with CakePHP? (For example, to put default input focus on a textfield when a page loads (the onload event)).

If you haven't already done it, the first thing you need to do is install jQuery to work with CakePHP. Here's a quick CakePHP jQuery installation primer.

CakePHP jQuery installation/configuration

Before beginning, it is important to note that I'm working with CakePHP version 1.3.6, and jQuery version 1.4.4.

A CakePHP multiple layouts solution

CakePHP multiple layouts FAQ: How do I use multiple layouts in CakePHP? Also, is there an easy way to switch between different layouts in my CakePHP controllers?

To use multiple layouts in CakePHP, the CakePHP docs tell us to switch layouts in our CakePHP controller functions (actions), like this:

A CakePHP readonly text field

CakePHP readonly text field FAQ: Can I make a readonly textfield in CakePHP (a CakePHP Form input field, or Form text field)?

I haven't worked it all out yet, but I first tried to create a CakePHP readonly text field like this, using the CakePHP Form text object, which doesn't seem to work:

# this doesn't work
echo $this->Form->text('complexity', array('readonly' => 'readonly'));

When that failed I kept poking on the CakePHP Form input object, and was able to create a readonly text field like this:

CakePHP, Drupal, and LAMP/PHP job market - 2011

CakePHP and Drupal jobs, 2011 - Way back in December, 2009, I first started looking at the PHP job market, specifically CakePHP, Drupal, and LAMP/PHP job openings. I forgot to run the queries again this past December, but I just took a few moments to update them, so ... here is a quick look at the PHP developer job market in January, 2011.

Did Drupal account for all new PHP jobs in the last year?

I wish I had more statistics to look at this, but at a preliminary glance, it looks like Drupal has accounted for all new PHP job openings in the last twelve months. I just published a little CakePHP, Drupal, and LAMP PHP jobs survey, and while the good news is that the PHP job market appears to have grown well in the last twelve months, the case can be made -- based on these statistics -- that Drupal accounted for almost all of the new PHP job openings.

CakePHP require, notEmpty, allowEmpty, and form validation

CakePHP require, notEmpty, and allowEmpty FAQ: Help, I'm trying to edit my CakePHP model $validate field to make sure a CakePHP form field is required, but 'require' doesn't seem to be working as expected.

After fighting with a CakePHP form I just got this information beat into my head, so I thought I'd share it here. In short, if you want to make a CakePHP form field required, what you really want to use is the CakePHP notEmpty rule, like this:

The CakePHP form save, validate, and debug problem

CakePHP form save/validate/debug FAQ: How do I debug problems in the CakePHP form save/validate process?

One problem I've run into with CakePHP is that while the form validation approach works very well 95% of the time, when it fails it can be very hard to debug. Because the form validation process happens when you invoke the save() method, much of this debugging process seems to be out of your control.

A CakePHP jQuery fade out flash success message

CakePHP flash fade out success messages: If you'd like to show fade out success messages in your CakePHP applications using jQuery, like the ones currently used by applications such as Twitter and Facebook, I thought I'd share the formula I came up with this weekend. While it includes touching a few different files, it's pretty easy, and once you've done the basic setup work you can display CakePHP fade out success messages in all of your web pages, and web applications.