Archive for category Programming

Sharepoint Designer – “The server could not complete your request” error message

I started getting this error message yesterday while working in Sharepoint Designer. I would get the message whenever I would open a Sharepoint website. The solution to this problem is quite simple:

1) Open Internet Information Services (IIS) Manager.

2) Expand your computer list on the left then click on Application Pools.

3) Locate the application pool used by your Sharepoint website.

4) Right-click on the application pool and select Recycle.

You should now be able to connect to your Sharepoint website using Sharepoint Designer as usual.

Tags: , ,

CSS: How to remove the dotted outline on links

When you click on a link element, it becomes focused and displays a dotted border around it. In some case, you might want to remove that outline for aesthetics, especially on menu links that are displayed as block elements. To remove that outline, simply place this CSS line of code:

a:focus {
	outline:none;
}

In the above example, all links that become focused won’t display the outline.

Keep in mind that hiding that outline will prevent people use keyboard navigation to see which element is selected.

Tags: , ,

PHP: Converting titles to permalinks

Having the item title in the URL is a good way to improve SEO for your website. For example, /shop/adobe_photoshop_cs5/ is much more meaningful to users and search engines than /shop.php?i=83s9f8haj8dj. Unfortunately, not all characters are valid in URLs, which is where a permalink function comes handy to convert titles into a nice URL format. Permalinks usually remove all non-alphanumerical characters and replace spaces with underscores. “Adobe Photoshop CS5” is then converted to “adobe_photoshop_cs5”.

function permalize($input) {
	return str_replace(' ','_',strtolower(trim(preg_replace("/[^a-zA-Z0-9 ]/", '', $input))));
}

Note that you can use some special characters in URLs (like + and -), I usually like to remove them completely, but it is totally fine to leave them should you choose to.

If you don’t want to rely on a permalink to retrieve database data about your item, you can still insert the ID in the URL (e.g. /shop/34655/adobe_photoshop_cs5/ ). The permalink could then be discarded in the code while still providing better readability and SEO.

Tags: , ,

WordPress and Password-Protected Directories

WordPress adds a little code to your root .htaccess in order to catch all URLs on your website and process them. It works fine if you only have WordPress on your websites and normal files/folders. But should you have a password-protected directory, WordPress might catch the 401 page as well which results in never showing the login authentication box at all.

I tried to add additional RewriteCond and RewriteRule to catch the password-protected directories before WordPress does but without success. The trick is in fact much more simple than that. You simply need to tell Apache to go to a custom 401 page of your choice (it can be blank or the default page, doesn’t matter). You will then be prompted with a login box and let you proceed to the password-protected directory. Should the login be wrong, the custom page will be displayed. No more annoying WordPress url rewriting!

Here’s the code: (notice the usual WordPress code, you simply need to add the ErrorDocument line above it)

ErrorDocument 401 default

# BEGIN WordPress
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>
# END WordPress

The best way to write PHP code

PHP is quite flexible in the way you write code. You can write same code in about 10 different styles and it’s still going to work. The problem is when you try to read and understand someone else script. It’s either well documented or completely unreadable.

In my opinion, writing large blocks of documentation is, most of the time, useless and time consuming. It is possible write code in a way that it can be read by anyone with very short amount of documentation. It all starts with naming your functions correctly, dividing long blocks of code in short functions that are self-explained.

So I decided to share some best practices I’ve read or learned over time.

Naming

// INCORRECT
function Getlastmessages()
function GetLastMessages()
function getLastMessages()

// CORRECT
function get_last_messages()

Sure the CamelCase version isn’t bad, but the best way to name methods is by separating words with an underscore.

You should name your classes the same way but by using an uppercase letter as the first letter. (ex: Get_last_message)

Constants also follow the same rule but all letters are in uppercase (ex: GET_LAST_MESSAGE)

HTML Output

Unless the HTML to output is relatively short, you should always close PHP, write your HTML and then open PHP back.

Use the short <?= tag (if enabled on your server) to open PHP to output a variable.

TRUE, FALSE and NULL

// INCORRECT
$var = true;
$var = True;
if($var == null) {}

// CORRECT
$var = TRUE;
$var = FALSE;
if($var == NULL) {}

Always write conditional values in uppercase.

PHP Files ending

As strange as it sounds, you shouldn’t close PHP at the end of PHP file. You should instead include a standard comment at the end and that’s it.

<?php
// PHP CODE HERE

/* End of file index.php
/* Location: ./application/index.php */

Why? Because PHP will close automatically as it reaches the end of the file but more importantly, it will prevent any whitespace or line breaks issues at the end of the file.

UTF-8 File Encoding and Unix line-breaks

Your code editor should always be configured to use UTF-8 encoding and Unix style line breaks. This will ensure compatibility with most languages and servers configuration.