Ask the community if you have any question about PHP,
MySQL, Apache and Linux for quick answers!!!

Archive for January, 2011

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

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

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

Count number of times a word exists in a column or string

MySQL does not provide any native function to count the number of times a specific word exists in a column or string. But you can do it yourself by using MySQL string functions LENGTH() and REPLACE(). e.g. you have a column description and want to count number of instances of a word 'iPhone'. This query will return the number of occurrences of word iPhone.

SELECT (LENGTH(description) - LENGTH(REPLACE(description,'iPhone','')))  / LENGTH('iPhone')
FROM table

Comments

What’s new in MySQL 5.5 webinar

I just attended MySQL webinar "What's New: MySQL 5.5 and MySQL Enterprise Edition". You may find the recording of this webinar at mysql.com. It was nice to see improved InnoDB performance (10x) as compared to MySQL 5.1 InnoDB. A couple of people in the webinar reported that they have experienced the enhanced performance with MySQL 5.5.

Here are some of the questions that looked interesting to me

Q: you have comparion charts between myisam and innodb?
A: We've just completed our MyISAM and InnoDB benchmarking and will be posting a whitepaper this week.

Q: Can you compare MySQL 5.5 partitioning schemas and performance with MS SQL Server 2008?
A: I'd recommend the following whitepaper for details: http://www.mysql.com/why-mysql/white-papers/mysql_wp_partitioning.php

Q: Are there any linux graphs/stats with using 2 or 4 core CPU's? The previous linux graphs were with 6 cores.
A: Here is a link to additional details regarding some of the benchmarks. Many of them start w/ 4 cores enabled and then expand to additional cores. As 2 cores scaled well with previous version, I believe most benchmarks started with 4 cores: http://assets.en.oreilly.com/1/event/36/What_s New in MySQL 5_5__ Performance and Scalability Benchmarks Presentation.pdf

Q: The white paper referenced for partitions is for 5.1. I am interested in the features and extensions that have been implemented in 5.5 A. Is there a newer white paper? Thanks
Q: Yes -- Here are improvements for 5.5 regarding partitioning http://dev.mysql.com/tech-resources/articles/mysql_55_partitioning.html

Q: Does MySQL support sub-partitioning ?
A: Yes http://dev.mysql.com/doc/refman/5.5/en/partitioning-subpartitions.html

Q: Is there any testing data available that focuses on the improved innodb performance in a virtual/cloud environment rather then the larger multi-core physical testing platforms noted in the presentation
A: I'd recommend reviewing the detailed presentation posted from our users conf: http://www.mysql.com/news-and-events/on-demand-webinars/display-od-532.html

Comments

How to remove PHPSESSID from URL?

You can remove PHPSESSID from URL by changing session.use_trans_sid value in php.ini, .htaccess (Apache) or in PHP code.

In php.ini set the value of session.use_trans_sid to 0
session.use_trans_sid = 0

In .htaccess
php_value session.use_trans_sid 0

In PHP code set the use_trans_sid value to false like below

ini_set('session.use_trans_sid', false);

Comments