www.pokeroconnor.com

Wordpress site redirect

February13

I had the following problem lately: I added the typical rules to htaccess to redirect example.com/anything to www.example.com/anything, e.g.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*)$ http://www.example.com/$1 [L,NC,R=301]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

But when I went to example.com I got infinite redirects…reason being I had the siteurl field in the wp_options table of the database set to http://example.com.

So, you need to change that to http://www.example.com, and also edit wp-config.php as follows:

define('WP_HOME','http://www.example.com');
define('WP_SITEURL','http://www.example.com');

That should be it - see here if not.

Using CURL to access certain IP of a Domain

July23

Something I needed to do recently was get the contents of a certain url of a certain domain name, for each of the IP addresses associated with that domain name. In other words, there was a domain we shall call www.test-domain.com, which was hosted on 5 different servers, with IPs 1.2.3.100 through 1.2.3.104. I wanted to automate the getting of the contents of www.test-domain.com/file-name.php on each of these servers. I could set my hosts file of course, and point at each server, blah blah blah, but this had to be automated, for sanity.

The php curl library is fully awesome, but personally I find this page to be the most useful. There are surprisingly few examples of this anywhere on the web…just try googling this topic and you get nothing at all relevant!

The solution is simple enough, but believe me it won’t be easy to find on the web – thanks to a certain work colleague for the expert advice on this! Basically you need to inject a header using CURLOPT_HTTPHEADER, and then use CURLOPT_URL with the actual IP address.

For example:

$ch = curl_init();
$headers = array("Host: www.test-domain.com");

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "http://1.2.3.100/file-name.php");

curl_exec($ch);

That’s the engine of it, a very neat little solution!

Zend Form Password

May16

When using Zend Form, and you have a password field and confirm password field, it is very handy to use Validation Context. It is described in the Zend documentation, just search for “My_Validate_PasswordConfirmation”. As is typical in Zend docs, the example is shit, they just give a class definition. So, here’s a brief example of I used it.

Basically what you need to do is attach the validator to both the password and the confirm password field. 

So firstly, add the class:

class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
const NOT_MATCH = 'notMatch';

protected $_messageTemplates = array(
self::NOT_MATCH => 'Password confirmation does not match'
);

public function isValid($value, $context = null)
{
$value = (string) $value;
$this->_setValue($value);

if (is_array($context)) {
if (isset($context['password_confirm'])
&& ($value == $context['password_confirm']))
{
return true;
}
} elseif (is_string($context) && ($value == $context)) {
return true;
}

 

 

$this->_error(self::NOT_MATCH);
return false;
}
}

Then, in your form file, use it something like follows:

class RegisterForm extends Zend_Dojo_Form

{

public function init()

{
$form = new Zend_Form();
....
....
....
$custom_pass = new MyValidatePasswordConfirmation();

...
...
$password = $form->createElement('password', 'password')
->addValidator('StringLength', false, array(1,24))
->setLabel('Choose your password:')
->addValidator('Alnum')
->addValidator($custom_pass)
->setRequired(true);

 

 

$password_confirm = $form->createElement('password', 'password_confirm')
->addValidator('StringLength', false, array(1,24))
->setLabel('Confirm your password:')
->addValidator('Alnum')
->addValidator($custom_pass)
->setRequired(true);

It’s fairly straightforward, but of course there’s very few examples I could find, this is probably the best.

posted under MySQL, php | 1 Comment »

Zend Form Validators

May1

I have another post with info on Zend validators, but here’s a very handy class to specify a maximum and minimum value to validate against. E.g. you have a form value that must be between 1 and 10 – Zend Validate doesn’t have a validator for that, but this class will allow you to. Just set $minimum and $maximum to your limits.


class MyValidNumericBetween extends Zend_Validate_Abstract
{
const MSG_NUMERIC = 'msgNumeric';
const MSG_MINIMUM = 'msgMinimum';
const MSG_MAXIMUM = 'msgMaximum';

public $minimum = 1;
public $maximum = 45;

protected $_messageVariables = array(
'min' => 'minimum',
'max' => 'maximum'
);

protected $_messageTemplates = array(
self::MSG_NUMERIC => "'%value%' is not numeric",
self::MSG_MINIMUM => "'%value%' must be at least '%min%'",
self::MSG_MAXIMUM => "'%value%' must be no more than '%max%'"
);

public function isValid($value)
{
$this->_setValue($value);

if (!is_numeric($value)) {
$this->_error(self::MSG_NUMERIC);
return false;
}

if ($value < $this->minimum) {
$this->_error(self::MSG_MINIMUM);
return false;
}

if ($value > $this->maximum) {
$this->_error(self::MSG_MAXIMUM);
return false;
}

 

 

 

return true;
}
}

And to use this class, assuming you have a normal form instantiated with $form = new Zend_Form(), just do:


$custom_valid8r = new MyValidNumericBetween();

$ball1 = $form->createElement('text', 'ball1')
->addValidator($custom_valid8r)
->setRequired(true);

And to set a custom message here are 2 links with good examples, some of them re-printed below.
Firstly:

$validator = new Zend_Validate_StringLength(8);

$validator->setMessage(
'The string \'%value%\' is too short; it must be at least %min% ' .
'characters',
Zend_Validate_StringLength::TOO_SHORT);

if (!$validator->isValid('word')) {
$messages = $validator->getMessages();
echo current($messages);

// "The string 'word' is too short; it must be at least 8 characters"
}


Secondly:

$validator = new Zend_Validate_StringLength(8, 12);

$validator->setMessages( array(
Zend_Validate_StringLength::TOO_SHORT =>
'The string \'%value%\' is too short',
Zend_Validate_StringLength::TOO_LONG =>
'The string \'%value%\' is too long'
));

$validator = new Zend_Validate_StringLength(8, 12);

 

 

if (!validator->isValid('word')) {
echo 'Word failed: '
. $validator->value
. '; its length is not between '
. $validator->min
. ' and '
. $validator->max
. "\n";
}

posted under php | 2 Comments »

Free SMS Gateways

April27

Will fill in more info later, but here’s the basics so far: got this amazing free sms Gateway, playSMS. Great install instructions, such as:

playSMS Web Interface:

1.  It is important to meet all minimum requiments above

2.  Setup a system user named ‘playsms’ to manage playSMS

    # adduser playsms

    # passwd playsms

    Note: on some Linux distributions adduser and passwd combined (Debian, Ubuntu and maybe others)

3.  On most Linux distributions actions (2) will create system user and group named ‘playsms’

    with home directory /home/playsms, but you will install playSMS in different directory

4.  Create playSMS web root, spool and log and set ownership to user www-data or web server user

    # mkdir -p /var/www/playsms

    # mkdir -p /var/spool/playsms

    # mkdir -p /var/log/playsms

    # chown -R www-data /var/www/playsms

    # chown -R www-data /var/spool/playsms

    # chown -R www-data /var/log/playsms

5.  Extract playSMS package somewhere (Usually in /usr/local/src)

    # tar -zxvf playsms-x.x.x.tar.gz -C /usr/local/src

    Note: x.x.x may vary according to the package name you’ve download

6.  Copy files and directories inside ‘web’ directory to playSMS web root and set ownership again to 

    user www-data or apache web server user

    # cd /usr/local/src/playsms-x.x.x/web

    # cp -rR * /var/www/playsms

    # chown -R www-data /var/www/playsms

    Note: assumed your web server user is www-data

7.  Setup database (import database)

    # mysqladmin -u root -p create playsms

    # mysql -u root -p playsms < /usr/local/src/playsms-x.x.x/db/playsms.sql

    Note: you dont need to use MySQL root access nor this method to setup playSMS

    database, but this is beyond our scope, you should read MySQL manual’s for custom

    installation method or howto insert SQL statements into existing database

8.  Copy config-dist.php to config.php and edit config.php

    # cd /var/www/playsms

    # cp config-dist.php config.php

    # mcedit config.php

    or 

    # vi config.php

    Note: please read and fill all required fields with coutious

9.  Enter bin directory, copy playsms, playsmsd, playsmsd.php, playsmsd_start to directory default

    # cd /usr/local/src/playsms-x.x.x/bin

    # cp playsmsd playsmsd.php playsmsd_start /usr/local/bin/

    # cp playsms /etc/default/

    Note: please note the different between playsms and playsmsd

10. Look for rc.local on /etc and its subdirectories (usualy /etc, /etc/init.d or /etc/rc.d/init.d)

    Edit rc.local and put: 

    ”/usr/local/bin/playsmsd_start” (without quotes)

    on the bottom of the file (before exit if theres exit command). This way playsmsd_start 

    will start automatically on boot. 

    Note: you need ‘root’ access to do this

11  Browse http://localhost/playsms/ and login using default administrator user

    username: admin

    password: admin

12. At this point you should be able to login to playSMS web interface and manage playSMS

There are quite a few requirements though, notably you must have PEAR DB installed, and also Lynx browser. More to follow…
To install wget, get it here, read this. For the lynx install, I used /usr/share/libtool/config.guess instead of /usr/libexec/config.guess. 
posted under MySQL, php | No Comments »

Server Timezones

April19

You can override your server’s timezone setting using php’s date_default_timezone_set. It is a really good idea, in my opinion, to use this at the start of your scripts that work with timestamps. Reason being, you can transfer scripts between servers and they will always work, which wouldn’t necessarily be the case if they depend on a servers timezone e.g if you are calculating a unixtime value in your script.

For example, I had an Ajax Calendar app that used Unixtime values to calculate which data to retrieve from a MySQL db. This worked fine on my dev server, whose server timezone was GMT, but when I transferred the code to my production server, which runs on E.T, it was not retrieving the correct data! This was a major pain to debug, as the code itself was fine of course, and was solved with a single line of code:

date_default_timezone_set(”GMT”);

Place this at the start of any script that has a dependency on the server timezone, and you can port your code to any server regardless of its timezone. If you don’t know your server’s timezone, date_default_timezone_get() will give it to you.

posted under MySQL, php | No Comments »

XPath and php starters

April17

Quick starting point on XPath and php would be the W3C Xpath pages. All the syntax info you really need to build queries are there. One other example i found useful was this one.

Then to use XPath with your php scripts, I use xpath(). A basic example would be the following (explained underneath) – say you have an <item> node which has multiple <title> children nodes, and you want to get a list of all the first children <title> nodes from that xml doc, you could do this:

<?php

$xml = simplexml_load_file(’path/to/file/xml-file.xml’);

    $matches = $xml->xpath(”//item/title[1]“);

     foreach ($matches as $match){

echo $match[0] . “<br>”;

}

?>

So firstly we use simplexml_load_file to load our xml file. We then build the query, saying for every item element that appears anywhere in the xml file, select all title elements that are the first (the title elements that appear ‘top’ in the list) children of item elements.

posted under php | No Comments »

MySQL datetime Formatting

April10

Say you are using MySQL datetime as the format for your dates, so when you output it you get 2009-03-21, which isn’t exactly user friendly nice to read. Say you want instead to have March 21st, 2009. There are of course two main ways to do this, convert the datetime field in php using mktime() to Unix Time, and then format with date() from there. Bit of a pain, and heavier processing then the second way, which is to use MySQL’s DATE_FORMAT. Check out that page, seriously – the list of date modifiers is very comprehensive, and listed here for your pleasure at the bottom of the page.

There’s a great forum post here, mentioning both methods – its well worth a look as the examples are simple and clear.

Quick example would be what we mentioned above, i.e. getting your date in the format of March 21st, 2009. Look how quick it is in MySQL compared to PHP:

SELECT DATE_format(olddate, ‘%M %D, %Y’) as newdate FROM table;

‘olddate’ is the name of your date field in datetime format, and ‘newdate’ is the new var representing the nicely and newly formatted date!

Specifier Description
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th1st2nd3rd, …)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the week
%u Week (00..53), where Monday is the first day of the week
%V Week (01..53), where Sunday is the first day of the week; used with %X
%v Week (01..53), where Monday is the first day of the week; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal “%” character
%x x, for any “x” not listed above
posted under MySQL, php | No Comments »

Http Authentication with curl_setopt

April10

If you want to access a web page that requires http authentication via a php script, a very handy way to do it is the following:

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, ‘http://my.url.com/password.asp’); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 

curl_setopt($ch, CURLOPT_USERPWD, ‘user_name:password’);

 

$data = curl_exec($ch); 

curl_close($ch);

 

echo “<pre>”; 

print_r($data); 

echo “</pre>”;

posted under php | No Comments »

Zend Framework Helpers

April8

If you want to have user defined helpers, here is a brilliant article on them. Well worth checking out, really useful stuff.

posted under php | No Comments »
« Older Entries