Encoding jump anchors for Facebook shares – via sharer.php

Encoding jump anchors for Facebook shares proved to be an annoying, but simple task! Try it out for yourself.

https://www.facebook.com/sharer/sharer.php?u=http://example.com#jumplink 

Post to “Only Me” publicity if you wish (quite handy for developing). You will see when you take a look at facebook, he actually keeps the #jumplink anchor when you get redirected back to your facebook.com. It’s pretty silly.

The solution is a simple one – Facebook (or possibly how they are retrieving their $_GET parameters) is removing (or ignoring) our jump link. To fix this, we need to encode the special character to it’s ASCII equivalent – %23.

{code type=php}

$url = ‘http://example.com#jumplink’;
$sharer_url = ‘https://www.facebook.com/sharer/sharer.php?u=’ . str_replace( ‘#’, ‘%23’, $url );

// http://example.com%23jumplink

{/code}

The Result

Opening in a new window

Encoding jump anchors to Facebook shares when using JavaScript to create a new popup window is a very similar approach. You just use JavaScript’s replace method.

{code type=php}
var url = ‘http://example.com#jumplink’;
var sharer_url = ‘https://www.facebook.com/sharer/sharer.php?u=’ + url.replace( ‘#’, ‘%23’);

// http://example.com%23jumplink
{/code}

Best practice for setting/checking PHP Cookies

PHP cookies are great tools, when used properly (and implemented properly). 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. This means  your $_COOKIE value will not be set until you refresh, or go to another page. So I explored the Googlesphere for a fair chunk of time, and discovered the best practice for setting/checking PHP cookies within the same page load (no redirect).

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:

{code type=php}
setcookie( ‘my_cookie_name’, $my_cookie_value, time() + 60*60*24*30, ‘/’ );
$my_cookie = $my_cookie_value;
{/code}
or
{code type=php}
$my_cookie = $my_cookie_value;
setcookie( ‘my_cookie_name’, $my_cookie, time() + 60*60*24*30, ‘/’ );
{/code}
or (if you are just storing true/false)
{code type=php}
$my_cookie = setcookie( ‘my_cookie_name’, true, time() + 60*60*24*30, ‘/’ );
{/code}

Checking for your cookie later in the page

{code type=php}
if( isset( $my_cookie ) || isset( $_COOKIE[‘my_cookie_name’] ) ) {
// do stuff
}
{/code}

There you have it, the best practice for setting/checking PHP Cookies. If anyone has other methods/practices, please comment!

Date Comparisons in PHP – using strtotime()

Date Comparisons in PHP are tricky. 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.

{code type=php}
if( strtotime( $date1) > strtotime( $date2 ) ) {
echo ‘$date1 is later than $date2’;
}{/code}

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.

{code type=php}
/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i
{/code}

This single function detects for all the patterns in the original post and more.

Example

{code type=php}
$phone_numbers = array(
‘555-555-5555’,
‘5555425555’,
‘555 555 5555’,
‘1(519) 555-4444’,
‘1 (519) 555-4422’,
‘1-555-555-5555’,
‘1-(555)-555-25555’,
);
$regex = “/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i”;
foreach( $phone_numbers as $number ) {
echo $number . ‘: ‘ . ( preg_match( $regex, $number ) ? ‘valid’ : ‘invalid’ ) . ‘
‘;
}{/code}

Output

{code type=php}
555-555-5555: valid
5555425555: valid
555 555 5555: valid
1(519) 555-4444: valid
1 (519) 555-4422: valid
1-555-555-5555: valid
1-(555)-555-5555: valid
1-(555)-555-25555: invalid
{/code}

Reading $_GET values from a URL String in PHP

Reading $_GET values from a URL String in PHP was something I had been interested in for a while. It was quite simple to read the $_GET values from the current page’s URL (obviously), but what if you were using AJAX and passing a URL to figure out what data needed to be passed/used? There’s several other reasons why you may need this, but I digress.

While looking for a way to read $_GET variables from a string in PHP, I stumbled across this forum post on Stack Exchange.

{code type=php}
parse_str( parse_url( $url, PHP_URL_QUERY ), $array );
{/code}
Continue reading Reading $_GET values from a URL String in PHP

Securely Renaming a Massive Amount of Files with PHP – Part 3

Part One: Reading a folder structure recursively.
Part Two: Renaming the files (uniquely) and storing new and old file paths.
Part Three: Copy the files into a sequenced folder system.

This is the third and final part of my tutorial regarding renaming and copying files with new secure names. In the first two sections we covered:
Continue reading Securely Renaming a Massive Amount of Files with PHP – Part 3

Securely Renaming a Massive Amount of Files with PHP – Part 2

Part One: Reading a folder structure recursively.
Part Two: Renaming the files (uniquely) and storing new and old file paths.
Part Three: Copy the files into a sequenced folder system.

This tutorial is a continuation of Securely Renaming a Massive Amount of Files with PHP. In Part 1, we covered:
Continue reading Securely Renaming a Massive Amount of Files with PHP – Part 2

Securely Renaming a Massive Amount of Files with PHP – The Non-Destructive Route

A recent project scope included renaming (literally) tens of thousands of documents for security purposes – it’s not too secure having files name “November-2011-Finances.doc” hosted online.

This tutorial will explain how to use PHP locally (without a web server like XXAMP, MAMP, or WAMP). Using one of these 3 services is fine, but we decided to go the system route!

I will break this tutorial down into multiple parts:
Continue reading Securely Renaming a Massive Amount of Files with PHP – The Non-Destructive Route

Form Validation in PHP – Simplified

I was created a form earlier today, when I had to add Phone Number validation to the existing validation. This form already included email validation, but I decided to go over it as well.

Email Validation

This is a very similar validation, but there is nothing needed to customize:

{code type=php}
function valid_email( $str )
{
// This checks for “[email protected]
return ( ! preg_match(“/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix”, $str)) ? FALSE : TRUE;
}

if( ! valid_email( $_POST[’email_address’] ) {
echo “Please enter a valid email address.”
}

{/code}

Phone Number Validation

Edit: Please check out my new post on phone number validation, as it has a much more efficient regular expression pattern.

Phone Number validation is fairly straight forward, but has one major issue – formatting! There are many ways of putting in a phone number, and for usability’s sake, it’s best to supp
Continue reading Form Validation in PHP – Simplified