Development

Changeset 10060

You must first sign up to be able to contribute.

Changeset 10060

Show
Ignore:
Timestamp:
07/02/08 09:43:58 (5 months ago)
Author:
Carl.Vondrick
Message:

1.2: added ->getRemoteAddress() to get the client's IP address and ->getForwardedRemoteAddress() to get the IP address from behind a proxy (closes #2798)

Note: I am not sure if ->getForwardedRemoteAddress() is the best method name. Welcome to suggestions in ticket #2798.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2/lib/request/sfWebRequest.class.php

    r9960 r10060  
    10671067 
    10681068  /** 
     1069   * Returns the remote IP address for the request. 
     1070   * 
     1071   * @return string The remote IP address 
     1072   */ 
     1073  public function getRemoteAddress() 
     1074  { 
     1075    $pathInfo = $this->getPathInfoArray(); 
     1076 
     1077    return $pathInfo['REMOTE_ADDR']; 
     1078  } 
     1079 
     1080  /** 
     1081   * Returns the remote IP addressed after resolved from the forward. 
     1082   * 
     1083   * This is useful if you are testing to see if the user is behind a proxy. However, 
     1084   * it should not be trusted. 
     1085   * 
     1086   * @return string|null The remote IP address resolved from a forward, null if none set 
     1087   */ 
     1088  public function getForwardedRemoteAddress() 
     1089  { 
     1090    $pathInfo = $this->getPathInfoArray(); 
     1091 
     1092    if (empty($pathInfo['HTTP_X_FORWARDED_FOR'])) 
     1093    { 
     1094      return null; 
     1095    } 
     1096 
     1097    return $pathInfo['HTTP_X_FORWARDED_FOR']; 
     1098  } 
     1099 
     1100  /** 
    10691101   * Parses the request parameters. 
    10701102   * 
  • branches/1.2/test/unit/request/sfWebRequestTest.php

    r9833 r10060  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(31, new lime_output_color()); 
     13$t = new lime_test(34, new lime_output_color()); 
    1414 
    1515class myRequest extends sfWebRequest 
     
    132132$_SERVER['HTTP_HOST'] = 'symfony-project.org:8043'; 
    133133$t->is($request->getUriPrefix(), 'https://symfony-project.org:8043', '->getUriPrefix() works for nonstandard https ports'); 
     134 
     135// ->getRemoteAddress() 
     136$t->diag('->getRemoteAddress()'); 
     137$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; 
     138$t->is($request->getRemoteAddress(), '127.0.0.1', '->getRemoteAddress() returns the remote address'); 
     139 
     140// ->getForwardedRemoteAddress() 
     141$t->diag('->getForwardedRemoteAddress()'); 
     142$t->is($request->getForwardedRemoteAddress(), null, '->getForwardedRemoteAddress() returns null if the request was not forwarded.'); 
     143$_SERVER['HTTP_X_FORWARDED_FOR'] = '10.0.0.1'; 
     144$t->is($request->getForwardedRemoteAddress(), '10.0.0.1', '->getForwardedRemoteAddress() returns the value from HTTP_X_FORWARDED_FOR');