How to add Captcha verification in the comments of the Yii blog demo

This post is about adding captcha verification in the comments section of the Yii demo blog. This post was triggered from two comments posted on the page of the tutorial (Comment 1 and Comment 2). In order for you to follow through the current post you need to have the Yii framework in place and you need to have followed all the steps of the Blog Tutorial up untill the “Creating and Displaying Comments” section. So, let’s start:

If you have followed the tutorial you should by now have a working Leave a comment form in place that is displayed underneath each individual post of your blog web application. The form should contain the Name, Email, Website and Comment fields that will populate the corresponding properties of your Comment model. The Post ID should be set on submit, the Status of the Comment should also be set on submit and the Time Created should be set just before saving the Comment (if this is a newly created comment).

Continue reading “How to add Captcha verification in the comments of the Yii blog demo”

How to get your Error reporting constant values

PHP lets you specify the Error Reporting level, so that when a piece of your code is run based on this setting of the php.ini file the generated errors and/or warnings will be shown or not. Generally speaking in a development environment it is recommended to set this as high as possible so that you are informed about all the little details of your code whereas in  a production environment this setting is usually set to something less “explanatory”.

In order to set the value of the error_reporting directive php constant values and bitwise operators are used, as they are described in the PHP manual . The setting can be set during runtime using the php error_reporting() function (manual) passing as a parameter the int value calculated based on the constant values and bitwise operators used. The function returns the integer value of the previous error_reporting level.

Now if you wish to find out what is your current error_reporting level you could go and have a look in your php.ini file or you could use the error_reporting() function with no parameters, in which case the function will return an integer value representing the current error reporting level. If you go via the php.ini file you will see the names of the constants used and the operations applied to produce your current reporting level but if you try to get a similar result via a php file that you have written you might notice that it is more difficult to understand  what is going on since now you are only dealing with an integer value and not with the named constants. So which is the description of your error reporting level and how can you retrieve it from within a php file?

Continue reading “How to get your Error reporting constant values”

PHP class constant values

Noticed today that there are more than one ways to access the constants of a class when using the class name, or when using an object of the class to access the constant or even when using an object of the class as the value of a property of another class. The different ways are shown in the code snippets that follow along with a parse error that is generated when the constant is accessed as part of an object’s property.

Continue reading “PHP class constant values”

PHP self vs static

In PHP there are two special keywords self and static that play an important role in classes that inherit static methods or member variables. They resemble the use of $this for instanciated objects. $this is used to access member variables and methods of an object of a particular type. self and static can be used in a similar manner for accessing static methods and variables from within a class that defines or inherits them. So, let’s see an example of the use of self and static and of the major difference between them. Let us assume that you have two classes one called base and one called derived. Base defines two static variables called name with a value of ‘base’ and number with a value of 0. Derived extends base and defines the same two static variables but now name has a value of ‘derived’ and number has a value of 1.

Continue reading “PHP self vs static”

PHP __CLASS__ and get_class

Recently I was trying to test a small piece of PHP code and run into the magic constant __CLASS__ . I quote from the PHP manual (1)

The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. FooBar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.

So I thought that when I use __CLASS__ it will return me the class of the object that executes the code that includes the echo of __CLASS__. But as it turns out this is not true if __CLASS__ was echoed from within a method defined in a base class and inherited from a child class. As it turns out in that case __CLASS__ returns the name of the class that contains the defined method and not the name of the class that inherits the method and executes it. Consider the following example:

Continue reading “PHP __CLASS__ and get_class”

Array_unique funny story

Here is a funny story about an “error” I thought I was dealing with recently with PHP and arrays. Assume you have a simple array (elements are keyed by incremental numbers – default assignment when you use $a[] = 1) with many elements (let’s say 10.000) whose unique values you wish to save in your database for later use. So how did I choose to implement this? I run my array through PHP’s array_unique and then serialize it and then store it in the database for later!

Continue reading “Array_unique funny story”

Relatively positioned elements: How to remove white space

Positioning elements in HTML with CSS creates some problems when rendering the elements to be presented on a website. For example I came across this common problem with relatively positioned elements:

if you relatively position an element an empty space is created in the place where the element was supposed to normally appear

So how can we remove this white gap?

In order to examine this issue let us create an html page with 3 divs. One outer div which includes 2 other divs (div A and div B) and some text that follows the outer div. You can use the following html and css code to reproduce the result presented in the following image. I have chosen to set the background-color of the divs so we can easily identify them.

\Read more