How to resolve “I am unable to logout from my Joomla website” problem

A new solicitor firm based in the UK signed up for website maintenance with our company. The website had been developed in Joomla and running for the past 1 year. The first issue they reported was that their members were not able to log out from the website even if they clicked on the logout button. They were concerned (and rightly so) that their member’s account could be used to perform any negative activity on the website.

The form code for the logout button was as follows

<form action="index.php" method="post" name="login" id="form-login">
<?php if ($params->get('greeting')) : ?>
<div>
<?php if ($params->get('name')) : {
echo JText::sprintf( 'HINAME', $user->get('name') );
} else : {
echo JText::sprintf( 'HINAME', $user->get('username') );
} endif; ?>
</div>
<?php endif; ?>
<div align="center">
<input type="submit" name="Submit" class="button" value="<?php echo JText::_( 'BUTTON_LOGOUT'); ?>" />
</div>

<input type="hidden" name="option" value="com_user" />
<input type="hidden" name="task" value="logout" />
<input type="hidden" name="return" value="<?php echo $return; ?>" />
</form>

After initial assessment I found that the logout code was doing a POST instead of sending the parameters in querystring. Due to this Joomla did not know whether the visitor was trying to logout or not.

I opened the modules/mod_login/tmpl/default.php and changed the form action from POST to GET.

I also added the following hidden field

<input type="hidden" name="view" value="login" />

So the final form code comes out to be as follows

<form action="index.php" method="get" name="login" id="form-login">
<?php if ($params->get('greeting')) : ?>
<div>
<?php if ($params->get('name')) : {
echo JText::sprintf( 'HINAME', $user->get('name') );
} else : {
echo JText::sprintf( 'HINAME', $user->get('username') );
} endif; ?>
</div>
<?php endif; ?>
<div align="center">
<input type="submit" name="Submit" class="button" value="<?php echo JText::_( 'BUTTON_LOGOUT'); ?>" />
</div>

<input type="hidden" name="option" value="com_user" />
<input type="hidden" name="view" value="login" />
<input type="hidden" name="task" value="logout" />
<input type="hidden" name="return" value="<?php echo $return; ?>" />
</form>

I saved the file and the logout started to work fine after that.

Hope the above helped. Comments?

An alternative way to check mySQL version in the Magento check script

Magento, like most other open source softwares, provides a list of requirements which need to be fulfilled before it can be installed on your machine. If you are installing Magento for the first time then I would recommend that you read the server requirments. on the other hand there may be some who would like to know which server requirements are fulfilled by their server so that they can see at a glance what they need to ask their hosting providers to install/setup for them.

Magento provides a script for this purpose which can be viewed on this page. You can download the actual script from here. Unzip the downloaded zip file and you will see a PHP script. Copy the script to the location where you would like to install Magento and execute it.

Once executed you will see the output on the page as follows:

The above snap shot shows that the mySQL version installed does not fulfill the version which is funny as we have mySQL community version 5.1 installed which is more than what is required. I looked through the code and found the following code on line 30 which checks the mySQL version

preg_match('/[0-9]\.[0-9]+\.[0-9]+/', shell_exec('mysql -V'), $version);

Note:The shell_exec() command is not allowed on shared hosting servers as it could allow a coder to run code which may compromise the security and stability of the server.

This could be the reason why the $version was coming up empty and due to this the script was flagging that the mySQL version was incompatible. The shell_exec() command may run correctly on some computers but it did not run as expected on ours so I thought of altering it to show the correct mySQL version.

mySQL provides a function mysql_get_server_info() which returns the mySQL version number. The only thing it needs is a connection to be opened to mySQL so it could then return the information. I commented out line 30 and added the following lines of code:

$conn = mysql_connect('server','username','password');
preg_match('/[0-9]\.[0-9]+\.[0-9]+/', mysql_get_server_info(), $version);

You should know the details to connect to your mySQL server for the above to work. After making the above changes I ran the script again and this time it gave the following output:

Please note that the shell_exec() is not wrong but it does not work on some servers due to restrictions. The method I have provided is a workaround should the first method not work.

Comments?

How to parse large sized XML files using PHP XML Parser

I was recently provided a Google Base XML file by a client to load on one of his websites. The size of the XML file was over 20MB.  The format of the XML file was as follows:

<?xml version="1.0"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"
xmlns:c="http://base.google.com/cns/1.0">
<channel>
<title>Company Name</title>
<description>Company Name Description</description>
<link>www.companyname.com</link>
<item>
<g:id>TEST</g:id>
<title>Test Product Name</title>
<link>http://www.companyname.com/test-page.html</link>
<description>Test Product Descripton</description>
<g:price>99.99</g:price>
<g:expiration_date>2012-01-16</g:expiration_date>
<g:shipping>
<g:country>US</g:country>
<g:service></g:service>
<g:price>0.00</g:price>
</g:shipping>
<g:condition>new</g:condition>
<c:google_product_category type="string">Product</c:google_product_category>
<g:gtin>TESTGTIN</g:gtin>
<g:mpn>TESTMPN</g:mpn>
<g:product_type>Product</g:product_type>
<g:image_link>http://www.companyname.com/images/test_product.jpg</g:image_link>
<g:brand>TestBrand</g:brand>
<g:shipping_weight>1 kg</g:shipping_weight>
</item>
<item>
..
</item>
<item>
..
</item>
.
.
</channel>
</rss>

The task as can be seen is not complex however I had a feeling that reading such big file would prove to be a hitch. I first tried to use simplexml_load_file to load the file and then loop through the array and then save it into the database. The script failed after taking a few seconds and the error message I received was unable to load the file. That was expected.

I then coded a script which used the PHP XML Parser to parse the XML file. I just needed to specify what action to perform for each tag and the code handled the rest. Please view following code which parses the XML given above and saves it into the database:

<?php

// Open the database connection

$arr = array();
$flagStop = 0;
$flag = '';

$parser = xml_parser_create();

function start($parser,$element_name,$element_attrs) {
global $flag;
global $flagStop;

if ($element_name == 'ITEM') {
$flagStop = 1;
}

if ($flagStop == 1) {
switch($element_name) {
case "TITLE":
$flag = 'title';
break;
case "LINK":
$flag = 'link';
break;
case "DESCRIPTION":
$flag = 'description';
break;
case "G:IMAGE_LINK":
$flag = 'image';
break;
case "G:BRAND":
$flag = 'brand';
break;
case "G:PRICE":
$flag = 'price';
break;
case "G:SHIPPING":
$flag = 'not';
break;
default:
$flag = '';
break;
}
}
}

function stop($parser,$element_name) {
global $arr;
global $flagStop;

switch($element_name) {
case "ITEM":

// Save the information into the database

// Clear the array fields before moving onto the next ITEM tag
$arr[0]['title'] = '';
$arr[0]['link'] = '';
$arr[0]['description'] = '';
$arr[0]['image'] = '';
$arr[0]['brand'] = '';
$arr[0]['price'] = '';

// Resetting the flag
$flagStop = 0;

break;
}
}

function char($parser,$data) {
global $arr;
global $flag;
global $flagStop;

if ( ($flagStop == 1) && ($arr[0][$flag] == '') ) {
$arr[0][$flag] = $data;
}
}

xml_set_element_handler($parser, "start", "stop");
xml_set_character_data_handler($parser, "char");

$fp = fopen("<path to xml file>","r");
$totdata = '';
while ( ($data = fread($fp,8192)) != '') {
xml_parse($parser, $data, feof($fp)) or die(sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser)));
}

xml_parser_free($parser);

echo "done";
?>

XML Parser is a swell function to parse large XML files and the script above saved the data into the database within a couple of seconds.

Comments?

Never ignore page authority of your web pages

Web pages on your website have a page authority which are assigned by search engines and so even though they may seem like not generating a lot of traffic you should not ignore them and try to use them directly or indirectly on your website.

We recently updated a client website based on WordPress and implemented a new theme on it as well as added some new features on it. The client had been running this website for over 2 years and he was looking for a major website overhaul.
The client was maintaining the website for the past 2 years on the same sitemap and wanted everything new on a new theme.

After we completed the theme implementation on the new website the client then added the older pages of his current website to the new website. This was not an issue with respect to the website look and feel as he was in his right to add whatever content to his website he wants but the problem was that the old pages would not be able to take advantage of the importance (Page Authority) search engines had placed in those pages. The client had also not been using permalinks on his website where as we had implemented it with the new theme as that would help the website on search engines.

We compared the current website sitemap to the new one and found that there were over 20 pages which did not exist on the new website. The only logical thing we could do was write 301 redirect rules for those pages so they could redirect to the new urls.

Moral of the story: Every page on your website is important so never disregard your old pages. Search engines regard the age of the domain and its pages very highly and you should always use them whether they may seem useless to you.

Bulk generate search engine friendly URLs in Zen Cart using Fast Updates

Our SEO department had recently made a request to implement CEON URI mapping module on one of our leading furniture retailer store in the UK. The module was implemented by the developer assigned to the project in a couple of hours and the website was handed over to the concerned department so they could continue adding the SEO friendly URLs to the e-commerce website.

Now the website had over 5,000 products and if they had to go by the default way of going into  each product edit page and updating that product page to generate the URL then it would have taken quite some time to complete this task and then continue on with their main job of actually optimizing the website.

I decided on using Fast Updates for this purpose. I added an input box on the page where the user could add his own version of the URL or he could leave it empty and let the system generate it for him. To generate the SEO friendly URL I had to use the concerned functions from the CEON URI mapping module. After some search I found the function needed for this purpose.

The actual function which generates the SEO friendly URL can be found in the following class file

includes/classes/class.CeonURIMappingAdminCategoriesProducts.php

The name of the function which does the job is

autogenCategoryOrProductURIMapping

The function expects the following parameters in the order given

id – product id of the product
type – category or product. As we are generating the URLs for the products I will just enter ‘product’ here
category – the category id against which this product is associated
name – the product name of the product
language code – just enter ‘en‘ here as it is for an English language website
language id – the Zen Cart language id for the website. In my case it will be 1 as it is for an English language website

The function returns the URL which I saved into the ceon_uri_mappings table and the problem was resolved. I handed it over to SEO department and they generated all the URLs within a couple of hours.

Comments?

How to display custom error message in Zend Captcha

The client recently requested to change the text of the default Zend CAPTCHA error message to just Incorrect Code when a visitor tries to submit the form without entering the CAPTCHA code in the input box beneath the image.
That was pretty easy enough as Zend provides you the option to override the default error message.

You just need to add the following line to the parameters being sent when instantiating the Zend Captcha object.

'messages' => array('badCaptcha' => 'Incorrect Code')

After applying the line and testing it we get the desired message as can be seen below

Comments?

How to verify email address using the Zend Email Validator

Zend framework provides some nice validators to validate the data input by your visitor/customer. If you want you can write your own custom validator and use it in your code. The one I would like to discuss today is the Zend Email Validator which is really wonderful in that you don’t need to write any JavaScript code to validate the email address. You just need to add the Zend Email Validator to the form element and it will take care of the rest.

Take a look at the following form element against which I have added the Zend Email Validator

$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email Address')
 	->setRequired(true)
  	->addFilter('StripTags')
  	->addFilter('StringTrim')
  	->addValidator('NotEmpty')
	->addValidator(new Zend_Validate_EmailAddress(
			array('domain' => true)
		      ));

The above code will check the domain part of the email address provided by the customer.

Once the visitor tries to enter invalid email address then the system will flag the error message. Please see below

Comments?

How to fetch images from remote server using PHP

We were updating the products on a client website from a new supplier. The supplier provided a CSV file containing the list of products and related information however they did not provide any product images. After browsing their website I found that they had saved all of their product images by the SKU name of the product so this was not going to be difficult at all.

I first uploaded the products into the database and then set about writing the script which would allow me to fetch the product image from the remote server and save it onto our server. The following is the PHP code I wrote to perform the extraction of the images from the remote server.

//code to fetch the SKU code from the database
//loop starts here

$strContents = file_get_contents(<Image URL on the supplier website>);
$fobj = file(“images/”.<SKU code of the product>.”jpg”,”w+”);
fwrite($fobj,$strContents);
fclose($fobj);
$fobj = null;

//loop ends here

Of course the above code would require some changes in your server configuration to allow PHP to extract the image file from the remote server.

Comments? Let me know please.

Steps to integrate Google Base with Zencart

Google Base is a place where you can easily submit all types of online and offline content, which will be searchable on Google Shopping product search. You can describe any item you post with attributes, which will help people find it when they do related searches. In fact, based on your items’ relevance, users may find them in their results for searches on Google Shopping, Google Maps and even our main Google web search.

Zencart Google Base Data Feed module has an ability which would help you export easy your products into Google Base engine.

Here are some significant steps which would help you to make this system successful.

Step 1:
Set up a new Google Account which we will use for Google Base. For e.g
Username: googlebase
Password: test1234

Step 2:
Sign in to Merchant center using link http://www.google.com/base/

Step 3:
Once you have logged in go to the Data feed option and specify the Data feed filename (domain_products.xml) and country etc. You don’t need to change the file name because Zencart by default uses this filename.

Before uploading your feed, please read the http://www.google.com/support/merchants/bin/answer.py?hl=en_US&answer=188494

Step 4:
Now you need to go to Settings and click on General option and fill the form accordingly and save the changes.

Now click on FTP option and set username and password of FTP server which would be needed for Zencart integration. For e.g.

FTP server name: uploads.google.com
Username: googlebaseftp
Password: googlebase

Step 5:
Here you would need to login into the admin of Zencart and make sure you have already installed Zencart Google Base Data Feed module.

Step 6:
Once you have logged in to the admin go to the Configuration and click on Google Base Feeder Configuration and put the value of Ftp user name and password which we have done in step 4 and ignore other values.

Step 7:
Now you would need to go to the tools option of admin and click on Google Base Feeder and click upload button and see the message it should be “Uploaded Successfully” and you will get email confirmation on your Google base account email address.

Step 8:
Now you can sign in to the Google Base/Merchant center and see the entire feed status summary like active/ Inactive/ errors in product attributes and statistics reporting etc.

Hope the above helped!!

How to display featured pages in WordPress

I was working on a WordPress website for a client and one of the client requirements was to display featured pages on the home page. If you do a Google for this term you are most likely to find results for featured posts, related posts etc. However I found one plugin which fit the requirements completely and that is the Featured Page Widget plugin.

I downloaded and installed it via the Install Plugin option within the WordPress panel. It has quite an easy to use interface and the best part was that it gave options to select featured pages as well as posts.

As you can see from above the plugin provides many options to setup how you require it to work on your WordPress website. Once these changes are done you can then use it as a widget or you can use it via code. Here we will discuss it’s usage via the widget section in WordPress.

The widget provides you the options to display the number of pages on a page, which pages to display on the page, the template to use for displaying the pages (customizable), the amount of content to display for each page as well as the featured image dimensions to configure. Please note that widget only allows featured images to show on the page.

You just have to save the settings now and see it action on your website. As I said it’s very easy to use and the benefits far outweigh any limitations it may have.

Let me know how it went for you. If you need my help in setting this up on your WordPress website then please let me know.