When running Drupal from the command line REMOTE_ADDR isn't populated thus calls to ip_address can cause a PHP notice.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mikeytown2’s picture

Status: Needs review » Needs work

The last submitted patch, drupal-1882556-1-remote-addr-notice-D8.patch, failed testing.

mikeytown2’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, drupal-1882556-1-remote-addr-notice-D8.patch, failed testing.

mikeytown2’s picture

Status: Needs work » Needs review
FileSize
575 bytes

forgot to do git pull for 8.x

kscheirer’s picture

Status: Needs review » Reviewed & tested by the community

I like, this looks like an easy fix.

Dries’s picture

Status: Reviewed & tested by the community » Needs work

I'm not sure we can automatically asume 127.0.0.1 (localhost) if that server variable isn't set. It is probably better not to return an IP address, rather than returning an incorrect one.

mikeytown2’s picture

Status: Needs work » Needs review

We automatically assume 127.0.0.1 in drupal.sh. If this is still a bad assumption I'll re-work this patch :)

kris-o3’s picture

is this not the exact problem that drupal_override_server_variables was designed to solve?

sun’s picture

Issue summary: View changes
Status: Needs review » Closed (won't fix)

ip_address() no longer exists.

david_garcia’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Closed (won't fix) » Active

Sorry to reopen, isn't this still an issue in D7?

danreb’s picture

I've seen this in error_log but in line 2916 and 1616 as shown in my apache error log.

[03-Jun-2015 08:07:01 Asia/Manila] PHP Notice:  Undefined index: REMOTE_ADDR in /home/allnone/public_html/test/includes/bootstrap.inc on line 2916
[03-Jun-2015 00:07:02 UTC] PHP Notice:  Undefined index: SCRIPT_NAME in /home/allnone/public_html/test/includes/bootstrap.inc on line 1616
[03-Jun-2015 00:07:02 UTC] PHP Notice:  Undefined index: REMOTE_ADDR in /home/allnone/public_html/test/includes/bootstrap.inc on line 2916
susannecoates’s picture

I'm seeing this issue too when bootstrapping Drupal 7 in a php script run on the command line.

A simple code snippet to illustrate:

#!/usr/bin/php
<?php
// path to Drupal passed from CLI
define('DRUPAL_ROOT', $argv[1]);

// bootstrap drupal
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
chdir(DRUPAL_ROOT);
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

//Do something
print "\n" . date("Y-m-d H:i:s") . ": Hello World\n";
?>

Executed on the CLI with:
php test.php /srv/websites/susanne.test

Produces the following output:

PHP Notice:  Undefined index: REMOTE_ADDR in /srv/websites/susanne.test/includes/bootstrap.inc on line 2916
PHP Notice:  Undefined index: REMOTE_ADDR in /srv/websites/susanne.test/includes/bootstrap.inc on line 2916
PHP Notice:  Undefined index: REMOTE_ADDR in /srv/websites/susanne.test/includes/bootstrap.inc on line 2916
PHP Notice:  Undefined index: REMOTE_ADDR in /srv/websites/susanne.test/includes/bootstrap.inc on line 2916
PHP Notice:  Undefined index: REMOTE_ADDR in /srv/websites/susanne.test/includes/bootstrap.inc on line 2916

2015-07-16 13:39:36: Hello World
BD3’s picture

My issue was that I had a Drupal 6 site with a cron job running from cPanel. Well when a new Drupal 7 site was launched, I never removed the cron script from cPanel which was causing this error in the Apache logs every time it was ran. Removing the cron fixed this issue as I was running cron from Drupal 7 UI.

Just thought I would post here for anyone else having the same issue.

newswatch’s picture

I followed #14. It seems running cron from Cpanel was causing the problem.

Fabianx’s picture

Status: Active » Needs work

I do agree with Dries, I think returning '' would be better to avoid side effects and have the same behavior. Just without the warning. On the other hand you should really use drupal.sh to try to do command line processing, so it could also be considered won't fix.

Hmmm, okay so what about:

$ip_address = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : NULL;
poker10’s picture

As @kris-o3 mentioned, in scenarios like #13 the drupal_override_server_variables() should be used before bootstraping Drupal and it should fix the problem.

The second mentioned scenario (where the cron.php is run via command line php) can be fixed, but I don't think that the approach with NULL would work. We have multiple tables with hostname column defined as:

'hostname' => array(
  'type' => 'varchar',
  'length' => 128,
  'not null' => TRUE,
  'default' => '',
  'description' => 'The IP address this vote is from unless the voter was logged in.',
),

So it explicitly says NOT NULL. For example the Poll module is inserting the output of the ip_address() directly to the database, so this query:

db_insert('poll_vote')
  ->fields(array(
    'nid' => $node->nid,
    'chid' => $choice,
    'uid' => $user->uid,
    'hostname' => ip_address(),
    'timestamp' => REQUEST_TIME,
  ))
  ->execute();

would throw a PDOException (at least on some DB engines like PostgreSQL):

PDOException: SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "hostname" of relation "poll_vote" violates not-null constraint

The same problem can be with the accesslog and other tables/modules. So I think we have two options:

1. Replace the NULL with an empty string:

$ip_address = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

2. Won't fix this issue (see #16).