Category Archives: PHP - Page 3

Get search query string from search engines using PHP

I have often come across situations when I would like to implement some functionality based on what the user searched for in a search engine like Google, Bing etc.

The search query string is normally passed as GET variables ‘q’ or ‘query’. The function below will return the search query string.

// Function returns the query string (q or query parameters) from the referrer
function get_search_query()
{
	$ref_keywords = '';
 
	// Get the referrer to the page
	$referrer = $_SERVER['HTTP_REFERER'];
	if (!empty($referrer))
	{
		//Parse the referrer URL
		$parts_url = parse_url($referrer);

		// Check if a query string exists
		$query = isset($parts_url['query']) ? $parts_url['query'] : '';
		if($query)
		{
			// Convert the query string into array
			parse_str($query, $parts_query);
			// Check if the parameters 'q' or 'query' exists, and if exists that is our search query terms.
			$ref_keywords = isset($parts_query['q']) ? $parts_query['q'] : (isset($parts_query['query']) ? $parts_query['query'] : '' );
		}
	}
	return $ref_keywords;
}

Note: Google has recently stopped sending the search query terms for all logged-in users. So, if a user is logged-in Google and searches for something and then follow a link to your site, you won’t be able to get the query terms that they searched for. The Making search more secure blog post by Google, explains this is more details.

PHP isset() vs empty() vs is_null()

PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.

isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.

Read more »

Getting real client IP address in PHP

Many times we need the visitor’s ipaddress for validation, security, spam prevention, etc. Getting the Visitor’s ipaddress is very easy in PHP.

The simplest way to get the visitor’s/client’s ipaddress is using the $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables. The variable in the $_SERVER array are created by the web server (like apache) and we can use them in PHP.

// Get the client ip address
$ipaddress = $_SERVER['REMOTE_ADDR']

Read more »

Voting Functionality in a website

In this post I will give the step by step explanation of how we can add Voting Functionality to a website. At the end of this article we will have a working sample voting application. The source code for the sample voting application can be downloaded from here.

We will be using PHP, MySQL, jQuery, HTML, CSS, AJAX to implement this.

To setup this functionality we will have to first setup database, then a front-end and also a page which would actually record and retrieve the votes.
Read more »