Wednesday, November 4, 2009

Top 5 most common PHP blunders

PHP is a very flexible language. Flexible enough that someone with no programming experience at all can create useful programs, meanwhile appeasing the needs of the formally trained OOP programmers.

Coders from either of these two groups have certainly written code that generated an error. Sometimes I just wish that either the errors were more helpful, or simply spat out a number, so you could look it up in the "this number means this error" table.

I can see it now.. "Parse error #63" and of course referencing my list of error codes that doubles as a black light poster I see error #63 reads "you left the stupid semi-colon off the end of the line again !" Some things just never change.

Anyway, here are in my opinion the most common errors PHP coders encounter, and hopefully some hints on what might be the cause.


  • #5

    Warning: Cannot modify header information - headers already sent by (output started at ...

    If you have a PHP script that tries to use the header function, or set cookies, you might have encountered this error. It's one of the few where the error message is actually helpful. The most common cause is an extra space or line either at the top or bottom of the PHP script, outside the <?php & ?> tags. Reading the error, it actually tells what file, and where the problem was caused.



  • #4

    Parse error: syntax error, unexpected T_STRING in ...

    Probably one of the most common errors, and also the least helpful. The most likely cause is the line above the one indicated in the error message is missing the semi-colon at the end.



  • #3

    Parse error: syntax error, unexpected $end in ...

    This error can be a nightmare to correct and almost always has to do with a missing closing curly bracket } You can avoid these types of problems by formatting your code as you go, lining up the opening and closing curly brackets like this...


    if ($loggedin == 1) {
    echo 'Hello '. $username;
    }
    else
    {
    echo 'Hello guest';
    }

    You will ALWAYS have the same number of opening and closing curly brackets. Another possible cause is mismatched double and single quotes in an echo or print statment, eg; echo "Oh no another error!';



  • #2

    Parse error: syntax error, unexpected 'X', expecting ',' or ';'

    Another not entirely helpful error, but usually easily fixed. Check for attempts to print/echo strings that are not surrounded by single or double quotes, like ...
    echo Oh no another error!; This error might also be the result of a missing semi-colon at the end of the preceding line.



  • #1

    Invalid argument supplied for XYZ ....

    Certainly a candidate for least helpful, this error arises when your try to do something to an array or object that doesn't exist. The actual cause can vary from simple typoes to a failure to create and populate the array/object.


Friday, October 9, 2009

Some helpful mod rewrite rules to improve SEO.

If you are new to creating websites, or just want add some new tricks to your existing skill set, getting a grasp on mod rewrite is a pretty good way to improve your website, both in terms of usiblity (for people) and improving search engine rankings (for bots).

Getting started, the .htaccess file


htacess is feature of Apache web server. If set up correctly, it gives the website owner a way to pass configuration changes and tweaks to the Apache web server, affecting how their website behaves.

Apache is typically installed with modules, one of those modules is called mod rewrite, and with it, you can essentially tell Apache what to do, and how to respond to nearly any type of website request.

Check your website for any .htacess files you might have. Keep in mind, Apache directives inside a .htaccess file affect all directories beneath it, so if you have existing .htaccess files, you can usually just add to them.

So either edit or create a .htaccess file with any text editor such as notepad.


Handy .htaccess entries


Customized error pages
Let's face it sometimes things just don't go as planned, but your website can recover from it gracefully with custom error pages.

So, writing a single error page in HTML, including links to the site sitemap.xml, and other sections of the site, a search form if applicable, as well as a javascript redirect to the site's main page. So if a person encounters an error, they will
be steered in the right direction. You can use a single .html file to respond to numerous error types.

Using an error page named 404.html to respond to 404 (not found) and 500 (internal) errors, add this to your .htaccess file

ErrorDocument 500 http://www.superdupersite.com/404.html
ErrorDocument 404 http://www.superdupersite.com/404.html


Blocking website access by IP address.

Deny from 127.0.0.0


Firing up mod rewrite . .


In order to make sense of mod rewrite directives, you need to tell Apache to access the mod_rewrite module.

Adding a line that reads . . .

RewriteEngine on
does exactly that.

Next, need to tell mod rewrite which directory to act upon, if this .htaccess file resides in your main web directory, you probably want . .

RewriteBase /


If the .htaccess file is in some sub directory of your website, you probably want ..

RewriteBase directoryname/


Handy mod rewrite rules


After enabling and setting the base directory, you can start adding mod rewrite rules. One I always include is what I call the the non WWW to WWW rule, basically force the browser or search engine spider to use www in the URLs of the site. If they browse to any pages without WWW, they are 301 redirected.


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


Prettier URLS


If you are using PHP, or really any other web development language, you can take parameters from ugly URLs and make them pretty.

The example below will serve index.php?author=Smith when /author-Smith.html is requested.

RewriteRule ^author-(.*)\.html$ index.php?author=$1 [R=301,L]


Likewise this rule serves index.php?book=Diary when /book-Diary.html is requested.

RewriteRule ^book-(.*)$ index.php?book=$1 [R=301,L]


Passing multiple paremters



Finally you could get book and author in one request like this..

index.php?author=Smith&book=Diary when /author-Smith-book-Diary.html


RewriteRule ^author-(.*)-book-(.*)$ index.php?author=$1&book=$2 [R=301,L]

The $1 represents characters in the first parentheiss (.*) the $2 represents the 2nd one and so on.

Regular expressions


If you have created something like the above mod rewrite rules, congratuations, you have used regular expressions since they were used. ^book=(.*)$

Regular expressions can get tricky but the vasics are ..

^ [caret] Means the beginning of a line, so ^G would match any line with a capital g as their first character.

$ [dollar] Means the end of a line , so G$ would match any line ending with a capitol g.

. [period] Means any single character, including space,

* [asterisk] Mean continually match on previous rule.. more on this later

Ranges . You can specify a range of characters to match on with square bracket []
Match any single digit number ..
[0-9]

Match any single lowercase letter
[a-z]

Match any single uppercase letter
[A-Z]

Match any letter, regardless of case
[A-Za-z]

Match any alphanumeric character
[A-Za-z0-9]

Remember the period and asterisk earlier? Combing examples above, you could do..

Any 2 digit number
[0-9].

Any number, regardless of lenghth
[0-9]*

And same for letters

Any 2 letters
[A-Za-z].

Any letter combination, regardless of lenghth.
[A-Za-z]*

Using just the period and asterisk means ALL characters, so
.* would match an entire line.

A.*Z would match all characters between capital a and z

Remember when a regular expression rule is wrapped with parenthesis, like this .. ^book=(.*)$ it becomes available to the destination URL as $1, $2 etc.

There are far more complex ways to use mod rewrite, as well as .htaccess but hopefully this has gotten you off to a good start.
 
Get Chitika | Premium