Ask us if you have any question about PHP, MySQL, Apache,
Linux or optimizing your website for quick answers!!!

Forcing a file download

The following .htaccess code will force the downloading a file instead of opening download request box.

AddType application/octet-stream .pdf
AddType application/octet-stream .csv
AddType application/octet-stream .zip

Comments off

How to restrict PHP code to override php.ini settings

One can restrict overriding PHP settings in .htaccess or in PHP using ini_set() also known as runtime when running PHP as an Apache module. There are two handy php.ini configuration directives which we use to achieve this i.e. php_admin_value and php_admin_flag.

Comments

Deleting WordPress Spam and Pending comments

Recently I have seen hundreds of spam comments on my blog, to get rid of these comments I simply executed the following 2 queries in MySQL.

To delete Spam comments (declared spam by WordPress)

  1. DELETE FROM `wp_comments` WHERE comment_approved='spam'

To delete pending comments (not detected as spam by wordpress but spam in nature)

  1. DELETE FROM `wp_comments` WHERE comment_approved='0'

Now I am happy and no need to go through hundreds of comments.

If you have deleted thousands of comments then don't forget to run OPTIMIZE TABLE command to reduce the comments table size. This is how you can OPTIMIZE the table.

  1. OPTIMIZE TABLE `wp_comments`

Comments off

InnoDB OPTIMIZE TABLE bug workaround

Once I wrote about an InnoDB bug in which OPTIMIZE TABLE resets the AUTO_INCREMENT counter of an InnoDB table. This bug was fixed in MySQL 5.1.55. Someone asked me the workaround for older versions. You can avoid this by using OPTIMIZE TABLE with LOCK TABLE in the below sequence.

  1. LOCK TABLE a WRITE;
  2. SHOW TABLE STATUS LIKE 'a';
  3. OPTIMIZE TABLE a;
  4. ALTER TABLE a AUTO_INCREMENT = 765438;
  5. UNLOCK TABLES;

765438 is a dummy value for AUTO_INCREMENT, you will get value for your table from column Auto_Increment in output of SHOW TABLE STATUS.

We need to use LOCK TABLES so that no other use can insert data to the table.

Comments

Google does not honor robots.txt and noindex

For quite some time Google is considering web site performance a critical factor in search rankings. Recently a list of most-asked questions is published by Strangeloop in which they have discussed some frequently asked questions how Google factors web site performance into its search rankings.

One of the point they discussed

Q: Do they measure pages marked as non-crawlable?
Yes. They measure pages your users use, not what you have told Google is crawlable

So it means, the pages you mentioned not to crawl by Google in robots.txt or using meta tag noindex is not honored by Google if these are being visited by users (given the users have Google toolbar installed).

Many sites have some reporting tools which run on the same domain name and most of the reports take time to generate. It'll definitely impact your search ranking if you have Google toolbar installed in the browser where you are viewing reports. To stop impacting this on your search ranking you can either uninstall Google toolbar or disable PageRank feature. This tip has improved search ranking of my friend's site.

Comments

Fed-up of updating status to multiple social platforms?

Are you fed-up of updating status to multiple social platforms? Don't worry, Hellotxt is a cool service to update your status to multiple services. This will save a lot of your time.

Comments

Google is now in content delivery network (CDN) market

Google has launched another performance centric service i.e. Page Speed Service. It seems to be the content delivery service which other CDNs are providing with a difference by doing some performance tuning i.e. concatenating CSS and some other tasks which are also performed by Google's mod_pagespeed. By providing this service, Google will always have updated content for its services like Google search and AdSense. Let's see how Google plan this service future.

Comments

Setting a php.ini directive in .htaccess to apply only to one .php file

Yesterday I needed to increase the value of upload_max_filesize for a site's admin script/page while other pages of the size didn't require that. It is really easy to do it by using Apache's Files directive in .htaccess for a specific file with the below code.

<Files my.php>
php_value upload_max_filesize 10M
</Files>

Comments

Quora.com invites giveaway

Hello,

I just created an account on www.quora.com. Quora is a collection of questions and answers. It is open for some countries and for some it is invite only. If you need an invitation, just send me a message on twitter or post a comment here.

Comments (47)

OPTIMIZE TABLE on InnoDB table resets AUTO_INCREMENT counter

MySQL recently fixed a known bug (I was not aware of this before) of OPTIMIZE TABLE (in MySQL Server 5.1.55, 5.5.9) in which OPTIMIZE TABLE statement was not preserving the AUTO_INCREMENT counter on OPTIMIZE TABLE operation.

Want to see yourself? Lets do it (code taken from the MySQL bug report)

  1. CREATE TABLE a (a INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT) ENGINE=INNODB;
  2.  
  3. INSERT INTO a VALUES ();
  4. INSERT INTO a VALUES ();
  5. INSERT INTO a VALUES ();
  6.  
  7. DELETE FROM a ;
  8.  
  9. SHOW TABLE STATUS LIKE 'a';

In the below table status, you can see the AUTO_INCREMENT value is 4 (even after deleting all the rows which is normal behavior).

  1. mysql> SHOW TABLE STATUS LIKE 'a'\G
  2. *************************** 1. row ***************************
  3. Name: a
  4. Engine: InnoDB
  5. Version: 10
  6. Row_format: Compact
  7. Rows: 3
  8. Avg_row_length: 5461
  9. Data_length: 16384
  10. Max_data_length: 0
  11. Index_length: 0
  12. Data_free: 4194304
  13. Auto_increment: 4
  14. Create_time: 2011-02-20 07:52:18
  15. Update_time: NULL
  16. Check_time: NULL
  17. Collation: latin1_swedish_ci
  18. Checksum: NULL
  19. Create_options:
  20. Comment:
  21. 1 row in set (0.00 sec)

Now execute the OPTIMIZE TABLE command and check the table status

  1. OPTIMIZE TABLE a;
  2.  
  3. SHOW TABLE STATUS LIKE 'a';
  1. mysql> SHOW TABLE STATUS LIKE 'a'\G
  2. *************************** 1. row ***************************
  3. Name: a
  4. Engine: InnoDB
  5. Version: 10
  6. Row_format: Compact
  7. Rows: 0
  8. Avg_row_length: 0
  9. Data_length: 16384
  10. Max_data_length: 0
  11. Index_length: 0
  12. Data_free: 4194304
  13. Auto_increment: 1
  14. Create_time: 2011-02-20 07:52:18
  15. Update_time: NULL
  16. Check_time: NULL
  17. Collation: latin1_swedish_ci
  18. Checksum: NULL
  19. Create_options:
  20. Comment:
  21. 1 row in set (0.00 sec)

MySQL fixed this bug 5 years after the bug report.

So be careful with OPTIMIZE TABLE if you have older MySQL versions.

TRUNCATE TABLE reset the AUTO_INCREMENT to 1 so don't confuse this example with TRUNCATE TABLE.

Comments (2)

How to get a user’s IP address in PHP?

There are many sites out there which tell you your own IP address. But all of them show you some ads, take some time to load and consume bandwidth (not a problem now a days). I decided to create one for my own use due to this. I thought to share the code and URL with you.

PHP's $_SERVER ($_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server) came to my help. I used REMOTE_ADDR, because I don't need the IP behind the proxy at the moment. Here is the code to get the IP address

  1.  
  2. echo "IP: ". $_SERVER['REMOTE_ADDR'];
  3.  

Here is the URL (http://ip.mwasif.com) to know your IP address.

REMOTE_ADDR: The IP address from which the user is viewing the current page.
HTTP_X_FORWARDED_FOR: The IP address of the user behind proxy. Becareful while using this, sometimes it may return user's LAN address.

Comments

« Previous entries Next Page » Next Page »