Categories
Laravel

Laravel 4 Authentication

Laravel 4 Authentication – A comprehensive guide

(bookmark) I’m definitely going to be reading this guide to creating an Authenticated App in Laravel (by Christopher Pitt)

Categories
WordPress

Don’t track internal visits to a WordPress site

Here’s a quick way to reduce the number of internal site visits showing up in Google Analytics from logged in internal users.

This code was for a WooThemes based site using the woo_head hook. It won’t be available in other themes.
[php]<?php
add_action(‘woo_top’, ‘my_analytics’, 5);
function my_analytics() {

//Don’t track admins (editors, or publishers…)
if( current_user_can(‘edit_posts’)) {
echo "\n<!– Internal User. Not counted –>\n";
return;
}

echo <<<EOD
(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

ga(‘create’, ‘UA-YOUR-TRACKING-ID-HERE’, ‘example.com’);
ga(‘send’, ‘pageview’);

EOD;
}[/php]

Categories
Workflow

Activating Windows in Hyper-V

Don’t activate Windows 7 as part of the virtual image testing process. Not licensed to do so according to this:  MS Virtual Machine Instructions (PDF)

Via: Modern.ie

Categories
PHP

Pad and Trim Input Arrays

PHP function to get an input array to have only, and all of the keys specified. This is useful if you have an array of user input with fields as keys, and you want to pad to ensure all keys are present, while stripping any not part of the template array:
[php]<?php
/**
* Fill and strip array according to $keys
* @param array $input
* @param array $keys
* @param mixed $fill
* @return array
*/
function array_purge_fill($input, $keys, $fill = ”) {
$keys = array_flip($keys);
$input = array_intersect_key($input, $keys); //strip extra keys from input

//Add empty values for missing keys
$diff = array_diff_key($keys, $input);
$diff = array_fill_keys(array_flip($diff), $fill);

return array_merge($input, $diff);
}[/php]

Example:

[php]<?php $input = array( ‘name’=>’Mike’, ‘phone’=>’12345’, ‘extra_value’ => true );
$keys = array(‘name’, ‘phone’, ’email’);

$newInput = array_purge_fill($input, $keys);
//has name, phone, email keys. Email will be empty, ‘extra_value’ will be removed[/php]

Categories
WordPress

WordPress Menus with URLs That Don’t Break

WordPress has a handy Menus feature in the Appearance area of the admin. You can link to Pages and Post Categories and these will migrate fine as you move your site from a development to production environments and the site URL changes.

Sometimes though you may need to refer to internal URLs not supported by Menus natively  using the Custom URL feature. The problem is that specifying the absolute URL here will break when the site is deployed.

A simple solution is to add a filter and use a placeholder value in Custom URLs added to a menu.

wordpress-menu-custom-urls

Then add a filter in your themes functions.php file, or as a must-use plugin as a more robust solution:

<?php 
/**
* Replace (site_url) placeholder in navigation menus
* Helps keep menus portable between dev/staging/production environments
*/
add_filter(
	'wp_nav_menu', 
	function($menu) {
		$menu = str_replace('(site_url)', home_url(), $menu);
		return preg_replace('~https?\:\/\/(https?\:\/\/)~', '$1', $menu);
	},
20
);

Note: this code uses an Anonymous function which requires PHP5.3+.
For older versions define this as a regular function and pass in its name as a string to add_filter()

Categories
PHP

APC on PHP5.4 Windows

APC (Alternative PHP Cache for bytecode caching and object storage) isn’t bundled with Windows.

This seemed to work (basic test on PHP 5.4.11). This Stackoverflow answer was helpful.

Other pages with Windows DLLs linked from that Stackoverflow discussion did not work for me.

Categories
Workflow

Changing Geany Color Schemes

The Geany Themes collection by Codebrainz is the best source of alternate color schemes for Geany. The readme installation guide misses the last step though.

After copying the colorschemes directory to %appdata%/Roaming/Geany and restarting, the schemes will be available under View > Editor > Color Schemes

(Geany 1.22)

Categories
Workflow

CMD cd Shortcut

Sick of cmd starting in c:\Users\me and having to type a long path to cd?

Create a .bat file and save it in c:\Windows\System32 (as Admin)
Save as mypath.bat, or perhaps even something logical.

@ECHO OFF
cd c:\path_i\commonly_want_to\go\to\

Then in cmd just type mypath and you’ll get where you want to go.

Categories
PHP

PHP FTP Error – PORT Command Successful

If your PHP script that FTPs files starts failing with an error like this:

PHP Warning:  ftp_put() [function.ftp-put]: PORT command successful

Try using pasv mode.

<?php
$con = ftp_connect(ftp.example.com);
//...
ftp_pasv($con, true);
?>
Categories
Laravel

Laravel on Shared Hosting

AntJanus gave the basic approach here. It did work for my Hello World test, not sure if there are potential issues waiting, such as with maintenance or framework updates.

Basically install locally (on Composer so far), zip it all up and upload the framework directory to a private folder above public_html.

The public entry point can be directly in public_html if the site is for the domain root, or in a sub-folder. Edit both public/index.php and laravel/bootstrap/paths.php to be able to find each other.

Paths.php looked a bit like this

[29]  'public' => __DIR__.'/../public_html/laravel',
//laravel is sub-directory