title: UniFi External Captive Portal - Mobile WiFi Disconnects on Auth
body: <p>I have created an external captive portal on a UniFi controller (v5.4.11) and successfully authorized devices using this library:</p>
  
  <p><a href="https://github.com/malle-pietje/UniFi-API-browser" rel="nofollow noreferrer">https://github.com/malle-pietje/UniFi-API-browser</a></p>
  
  <p>However, when my PHP code sends the authorize_guest($mac, $duration) command, the WiFi for the mobile device in question is actually disconnected.</p>
  
  <p>This happens on both Android and iOS as far as I can tell.</p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code> $controlleruser = $CFG-&gt;unifi_user;
  $controllerpassword = $CFG-&gt;unifi_pass;
  $controllerurl = 'https://'.$server.':'.$CFG-&gt;unifi_port;
  $controllerversion = '5.4.11';
  $cookietimeout = '3600';
  $theme = 'bootstrap';
  $debug = false;
  $duration = $CFG-&gt;unifi_minutes;
  $note = '';
  $site_id = 'default';
  
  require_once('class.unifi.php');
  
  $unifidata      = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
  $set_debug_mode = $unifidata-&gt;set_debug($debug);
  $loginresults   = $unifidata-&gt;login();
  /**
   * To add note to a new device we need to do the following before authorizing the device:
   * - first block the device to get an entry in the user collection
   * - get the device id from the user collection
   * - add note to the device
   * - then unblock the device again
   */
  $block_result   = $unifidata-&gt;block_sta($mac);
  $getid_result   = $unifidata-&gt;stat_client($mac);
  $user_id        = $getid_result[0]-&gt;_id;
  $note_result    = $unifidata-&gt;set_sta_note($user_id, $note);
  $unblock_result = $unifidata-&gt;unblock_sta($mac);
  /**
   * then we authorize the device for the requested duration
   */
  $auth_result = $unifidata-&gt;authorize_guest($mac, $duration);
  /**
   * provide feedback in json format
   */
  return $auth_result;
  </code></pre>