How to get an IP address of a visitor to your website using PHP?
Sometimes you will want to know the IP address of any visitor to your website for marketing analysis, or for sales chart purposes, or for geo location analysis of your website visitors. By using PHP, finding an IP address nowadays is much easier. It is very important requirement for many scripts where we store the members or visitors details. For security reason we can store IP address of our visitors who are doing any purchases or recording the geographical location of the visitor can be done this way. Sometimes based on the IP address we can redirect the browser to specific location of the site. For example, your website has advertisement banners that only targets users in San Francisco. When a visitor visit your website, you’ll record their IP address and then from there your codes will know that this visitor is in San Francisco. Therefore, an ad banner about San Francisco restaurant or hotel will be displayed on the page. There are many applications using this and below is the PHP code to recognize the IP address of any visitor of your web site.
If register_global in your php.ini is on, your codes on php page will be:
$ip=@$REMOTE_ADDR;
echo “IP Address is $ip”;
You will see this:
IP Address is 209.31.236.130
If you have a geo location by IP Lookup DB, you will find out that this sample IP address is in San Jose.
If register_global is off, your codes on php page will be:
$ip=$_SERVER[‘REMOTE_ADDR’];
echo “IP Address is $ip”;
You will see the same output as the above. With this piece of data, you can save it in your database table and redirect to an ads that targets users in this location. Or you can display a local community news for this user.
I hope that my explanation about IP address helps you in making decision for your website advertisement or your blogs.
What hook functions are generally included in .install file?
In Drupal, when you want to install a custom module, you must have a mycustommodule.install file. Implementations of the hooks below should be placed in a mycustommodule.install file in the same directory as mycustommodule.module. Some hooks are optional depends on how large your module is. For example, if your module doesn’t require any database table or variable, you wouldn’t have hook_schema in your .install file.
hook_disable | Perform necessary actions before module is disabled. |
hook_enable | Perform necessary actions after module is enabled. |
hook_install | Install the current version of the database schema, and any other setup tasks. |
hook_requirements | Check installation requirements and do status reporting |
hook_schema | Define the current version of the database schema. |
hook_uninstall | Remove any tables or variables that the module sets. |
hook_update_last_removed | Return a number which is no longer available as hook_update_N(). |
hook_update_N | Perform a single update. |
hook_requirements($phase)
Check installation requirements and do status reporting. This hook must be located in .install file. Use parameter $phase to check installation requirements ($phase == ‘install’ ) and status reporting ($phase == ‘runtime’).
Parameters
$phase: There’re 2 phases to check the requirements during the installation and runtime.
‘install’: the module is being installed.
‘runtime’: the runtime requirements are being checked and shown on the status report page.
Return value
A keyed array of requirements.
- ‘title’: the name of the requirement.
- ‘value’: the current value (e.g. version, time, level, …). During install phase, this should only be used for version numbers, do not set it if not applicable.
- ‘description’: description of the requirement/status.
- ‘severity’: the requirement’s result/severity level, one of:
- REQUIREMENT_INFO: For info only.
- REQUIREMENT_OK: The requirement is satisfied.
- REQUIREMENT_WARNING: The requirement failed with a warning.
- REQUIREMENT_ERROR: The requirement failed with an error.
Usage sample:
<?php function hook_requirements($phase) { // Ensure translations don't break at install time $requirements = array(); $t = get_t(); // Report Drupal version if ($phase == 'runtime') { $requirements['drupal'] = array( 'title'--> $t('Drupal'), 'value' => VERSION, 'severity' => REQUIREMENT_INFO ); } // Test PHP version $requirements['php'] = array( 'title' => $t('PHP'), 'value' => ($phase == 'runtime') ? l(phpversion(), 'admin/logs/status/php') : phpversion(), ); if (version_compare(phpversion(), DRUPAL_MINIMUM_PHP) < 0) { $requirements['php']['description'] = $t('Your PHP installation is too old. Drupal requires at least PHP %version.', array('%version' => DRUPAL_MINIMUM_PHP)); $requirements['php']['severity'] = REQUIREMENT_ERROR; } // Report cron status if ($phase == 'runtime') { $cron_last = variable_get('cron_last', NULL); if (is_numeric($cron_last)) { $requirements['cron']['value'] = $t('Last run !time ago', array('!time' => format_interval(time() - $cron_last))); } else { $requirements['cron'] = array( 'description' => $t('Cron has not run. It appears cron jobs have not been setup on your system. Please check the help pages for configuring cron jobs.', array('@url' => 'http://drupal.org/cron')), 'severity' => REQUIREMENT_ERROR, 'value' => $t('Never run'), ); } $requirements['cron']['description'] .= ' '. t('You can run cron manually.', array('@cron' => url('admin/logs/status/run-cron'))); $requirements['cron']['title'] = $t('Cron maintenance tasks'); } return $requirements; } ?>
Note: this hook must go into your .install file, in order to block the installation of the module unless requirements exist. If it’s in your .module file, only the runtime phase will execute. A lot of contributed modules get this wrong.
hook_install()
Install the current version of the database schema, and any other setup tasks. The hook will be called the first time a module is installed, and the module’s schema version will be set to the module’s greatest numbered update hook.
The implementation of hook_install() will need to explicitly load the module before any declared functions may be invoked.
Anything added or modified in this function that can be removed during uninstall should be removed with hook_uninstall().
Parameters
No parameters
Return value
none
Usage sample
/*
* Implementation of hook_install().
*/
function videoregistration_install() {
drupal_install_schema(‘videoregistration_type’);
}
hook_uninstall()
This hook will remove any tables or variables that the module has set. The uninstall hook will fire when the module gets uninstalled.
Parameters
None
Return value
None
Usage sample
function videopage_uninstall() {
drupal_uninstall_schema(‘videoshows’);
variable_del(‘video_types’);
}