Symfony World blog is not maintained anymore. Check new sys.exit() programming blog.

symfony forward through LAN

Scene from "Disney's Adventures of the Gummi Bears" by Jymn Magon & Art Vitello (introduced in 1985)

Ever thought of creating an action that would run different code depending on whether the client machine is inside or outside the LAN? It's really easy yet useful. I used it few times in one of my backend apps so far. For people outside the LAN, both links are hidden (use comparison below) and the redirected links are inaccessible. Thanks to it, company employees working from outside the office may not see inaccessible links. Below is the auxiliary method:

  /**
   * Checks if the client computer is inside the LAN with a
   * specific address. If so, redirects to a local address.
   *
   * @param String $ip - IP of the webpage to be forwarded to
   * @param String $port - port of the webpage to be forwarded to
   */
  protected function forwardThroughLan($ip, $port)
  {
    if ($_SERVER['REMOTE_ADDR'] == sfConfig::get('path_server_ip'))
      $this->redirect('http://'.$ip.':'.$port);
    else
      $this->setTemplate ('forwardThroughLanError');
  }
and here is direct usage:
  /**
   * Executes forward TRAC through LAN action. Forwards to TRAC webpage
   * inside LAN in the office building.
   *
   * @param sfWebRequest $request
   */
  public function executeForwardTracThroughLan(sfWebRequest $request)
  {
    $this->forwardThroughLan('192.168.1.99', '8080');
  }

No comments:

Post a Comment