Best practice for setting/checking Cookies

In a recent WordPress project, I created a semi-basic Poll form. Because WP uses actions, I was able to detect that my form ran, and updated my Poll Results in the database. In the SAME page load, however, you cannot check your $_COOKIE node for your cookie. PHP Globals are not altered (besides $_SESSION) except for when the page first loads, so your $_COOKIE value will not be set until you refresh, or go to another page.

The practice to check for your value is quite simple – when you set your cookie, set it to a global variable as well. That way you do not have to force a refresh/redirect to get your $_COOKIE value.

Setting the Cookie:

1
2
setcookie( 'my_cookie_name', $my_cookie_value, time() + 60*60*24*30, '/' );
$my_cookie = $my_cookie_value;

or

1
2
$my_cookie = $my_cookie_value;
setcookie( 'my_cookie_name', $my_cookie, time() + 60*60*24*30, '/' );

or (if you are just storing true/false)

1
$my_cookie = setcookie( 'my_cookie_name', true, time() + 60*60*24*30, '/' );

Checking for your cookie later in the page

1
2
3
if( isset( $my_cookie ) || isset( $_COOKIE['my_cookie_name'] ) ) {
    // do stuff
}

Date Comparisons in PHP

The best way to compare dates is by using strtotime(). My reasoning is simple – keep everything on the same level. Especially when dealing with user input, you don’t know a couple things.

  1. The formatting – dash(-) separators vs. colon(:) vs. backslash(/)
  2. The specificity – days, hours, minutes, seconds?

Now you can format these with regex, or PHP splits and joins,  to make sure that they meet PHP standards, and then use strtotime – comparing strings will not work in many cases.

1
2
3
if( strtotime( $date1) > strtotime( $date2 ) ) {
echo '$date1 is later than $date2';
}

Using WordPress core externally

I’m setting up a cron job at server level, but I wanted to be able to leverage WordPress functionality, but still separate my cron process from the WordPress installation. After a little digging around, I found this thread on StackExchange, which explains the setting up how to access WP core externally.

The following code will allow you to use all WordPress Core functionality!

1
2
3
<?php
// All that is required to bootstrap into WordPress.
require('../wordpress/wp-load.php');

Test it

1
2
3
4
if( function_exists( 'update_option' ) ) 
    echo 'We have WordPress support';
else
    echo 'no WordPress support';

Fit Britt – Fitness and Nutritional information and inspiration

FitBritt.ca is a site created for anyone wanting tips on nutrition and fitness, whether it’s healthy recipes, great workouts, tips or motivation.

Brittany posts her workout routines weekly, explains hurdles she comes across in her path to fitness, and shares any tips on healthy eating that she comes upon along the way.

fitbritt

Vertical-alignment for TextFields – ActionScript 3.0

As many of you may know, Adobe introduced TLF Text along with Flash Player 10. If you are working with general consumer projects, however, chances are you are targeting Flash Player 9 as a minimum.
Before TLF TextFields, there was no direct way to vertically align TextFields.

That being said, you can easily accomplish this by using the TextLineMetrics object, or more directly, TextField‘s getLineMetrics().

TextLineMetrics allows us to access basic values for any typeface, such as ascent, descent, leading, width and height. Using ascent, we can determine a TextField’s baseline Y position.

see more

Conditional includes in ActionScript 3.0

This morning I have looked for a way to include another .as file depending on a variable. ActionScript 3′s include does not allow you to use variables, as it only accepts string literals.

The reasoning for this is that include is fired on compile time, so any variables are not set, determined, or even defined yet.

The Solution

Reading an article, I came across the solution — Config Constants. These are defined in your ActionScript 3.0 settings (File > ActionScript Settings > Config Constants ). Here you can define compile-time constants, and have functionality depend on them.
see more

Transferring a WordPress Multisite

I have run into the situation where I needed to transfer a WordPress Multisite twice now, My first experience was horrific, but it seems I have a better handle on this now.

Step 1: Back everything up

Should be pretty self-explanatory — back up your files and database in preparation to transfer. DO NOT copy the .htaccess and wp-config.php files, it’s better practice to set these up from scratch.

Step 2: Copy the files to your new server/location

Send over your files to the new location via FTP, SSH, SVN, whatever. Create your database and set up your wp-config.php file.

Step 3: Go through WordPress installation

Before you import your database, go through the steps to set up the WP site from a clean install. Go through the steps to setting up a WPMU. This way the code you need to stick into your .htaccess and wp-config.php files will have the proper rewrite rules, as well as DOMAIN_CURRENT_SITE and PATH_CURRENT_SITE definitions.
see more

PHP phone number validation – revisited

Several months ago, when I was first getting familiar with regular expression, I made a post talking about PHP form validation for phone numbers and email addresses.

A couple of days ago, I revisited that post and realized how inefficient my function actually was. The entire point of regex is to recognize multiple patterns within 1 statement. Here is my revised regular expression.
see more

Secure-Shell (SSH) Reference Sheet – Subversion

Hello, all! This is a continuation of my Secure-Shell Reference Sheet series. This one focuses on interacting with subversion (svn) on a remote/local Linux server.

Check out the others:
Secure-Shell (SSH) Reference Sheet – Basic
Secure-Shell (SSH) Reference Sheet – Database

Check the status of all files
1
svn st

or

1
svn status
Add files to subversion
1
svn add *folder/file.php* *another-folder/file2.php*

or

1
svn add *.php

Note: Using svn add * inside a directory with items already added to the working copy will result in warnings/errors. It’s best to specify the files, or use the following technique:
see more

Secure-Shell (SSH) Reference Sheet – Database

This is the continuation of my Secure-Shell Reference Sheet – Basic.

Note: Anything surrounded by *asterisks* is where your input is required.

Accessing a Database
1
mysql -u *username* -p *password* -h *hostname* *database*

This will log in directly

1
mysql -u *username* -h *hostname* *database*

This will prompt you for the database user’s password
see more