title: Set session in Invision Power Board
body: <p>After lots of efforts we found something for IPB remote login, but it's not working correctly. We are able to fetch member information but not able to set this member in session.
  Please help us to the set session for IPB.</p>
  
  <p>Here is the code:</p>
  
  <p><code>remote_login.php</code></p>
  
  <pre><code>&lt;?php
  $_SERVER['SCRIPT_FILENAME'] = __FILE__;
  $path   = '';
  
  require_once $path . 'init.php';
  \IPS\Session\Front::i();
  
  $key = md5( md5( \IPS\Settings::i()-&gt;sql_user . \IPS\Settings::i()-&gt;sql_pass ) . \IPS\Settings::i()-&gt;board_start );
  
  $login_type = 'email';
  
  /* uncomment for more security  */
  // $ip_address = array('127.0.0.1', 'x.x.x.x'); // EDIT THIS LINE!!
  // if(in_array($_SERVER['REMOTE_ADDR'], $ip_address) !== TRUE) {
      // echo_json(array('status' =&gt; 'FAILD', 'msg' =&gt; 'BAD_IP_ADDR'));
  // }
  
  /* -~-~-~-~-~-~ Stop Editing -~-~-~-~-~-~ */
  
  if( !\IPS\Request::i()-&gt;do || !\IPS\Request::i()-&gt;id || !\IPS\Request::i()-&gt;key || !\IPS\Login::compareHashes( \IPS\Request::i()-&gt;key, md5($key . \IPS\Request::i()-&gt;id))) {
      echo_json(array('status' =&gt; 'FAILD', 'msg' =&gt; 'BAD_KEY'));
  }
  
  $member = \IPS\Member::load( \IPS\Request::i()-&gt;id, $login_type );
  
  if( !$member-&gt;member_id ) {
      echo_json(array('status' =&gt; 'FAILD', 'msg' =&gt; 'ACCOUNT_NOT_FOUND'));
  }
  
  switch(\IPS\Request::i()-&gt;do) {
      case 'get_salt':
          echo_json(array('status' =&gt; 'SUCCESS', 'pass_salt' =&gt; $member-&gt;members_pass_salt));
      break;
      case 'login':
          if( \IPS\Login::compareHashes($member-&gt;members_pass_hash, \IPS\Request::i()-&gt;password) === TRUE ) {
  
  /* Remove old failed login attempts */
                          if ( \IPS\Settings::i()-&gt;ipb_bruteforce_period and ( \IPS\Settings::i()-&gt;ipb_bruteforce_unlock or !isset( $member-&gt;failed_logins[ \IPS\Request::i()-&gt;ipAddress() ] ) or $member-&gt;failed_logins[ \IPS\Request::i()-&gt;ipAddress() ] &lt; \IPS\Settings::i()-&gt;ipb_bruteforce_attempts ) )
                          {
                              $removeLoginsOlderThan = \IPS\DateTime::create()-&gt;sub( new \DateInterval( 'PT' . \IPS\Settings::i()-&gt;ipb_bruteforce_period . 'M' ) );
                              $failedLogins = $member-&gt;failed_logins;
                              if ( is_array( $failedLogins ) )
                              {
                                  foreach ( $failedLogins as $ipAddress =&gt; $times )
                                  {
                                      foreach ( $times as $k =&gt; $v )
                                      {
                                          if ( $v &lt; $removeLoginsOlderThan-&gt;getTimestamp() )
                                          {
                                              unset( $failedLogins[ $ipAddress ][ $k ] );
                                          }
                                      }
                                  }
                                  $member-&gt;failed_logins = $failedLogins;
                              }
                              else
                              {
                                  $member-&gt;failed_logins = array();
                              }
                              $member-&gt;save();
                          }
  
  
  /* If we're still here, the login was fine, so we can reset the count and process login */
                          if ( isset( $member-&gt;failed_logins[ \IPS\Request::i()-&gt;ipAddress() ] ) )
                          {
                              $failedLogins = $member-&gt;failed_logins;
                              unset( $failedLogins[ \IPS\Request::i()-&gt;ipAddress() ] );
                              $member-&gt;failed_logins = $failedLogins;
                          }
                          $member-&gt;last_visit = time();
                          $member-&gt;save();
  
  
  /*==========================try to set session code start================*/
              /* Create a unique session key and redirect */
  
              \IPS\Session::i()-&gt;setMember( $member );
              $expire = new \IPS\DateTime;
              $expire-&gt;add( new \DateInterval( 'P7D' ) );
              \IPS\Request::i()-&gt;setCookie( 'member_id', $member-&gt;member_id, $expire );
              \IPS\Request::i()-&gt;setCookie( 'pass_hash', $member-&gt;member_login_key, $expire );
  
              if ( $anonymous and !\IPS\Settings::i()-&gt;disable_anonymous )
              {
                  \IPS\Request::i()-&gt;setCookie( 'anon_login', 1, $expire );
              }
              \IPS\Session::i()-&gt;setMember( $member );
              \IPS\Session::i()-&gt;init();
              \IPS\Request::i()-&gt;setCookie( 'ips4_member_id', $member-&gt;member_id, $expire );
              \IPS\Request::i()-&gt;setCookie( 'ips4_pass_hash', $member-&gt;member_login_key, $expire );
  
              /*$member-&gt;checkLoginKey();
  
              $expire = new \IPS\DateTime;
              $expire-&gt;add( new \DateInterval( 'P1Y' ) );
              \IPS\Request::i()-&gt;setCookie( 'ips4_member_id', $member-&gt;member_id, $expire );
              \IPS\Request::i()-&gt;setCookie( 'ips4_pass_hash', $member-&gt;member_login_key, $expire );*/
  
  /*==========================try to set session code end================*/
              echo_json(
                      array(
                          'status' =&gt; 'SUCCESS',
                          'connect_status'            =&gt; ( $member-&gt;members_bitoptions['validating'] ) ? 'VALIDATING' : 'SUCCESS',
                          'email'                     =&gt; $member-&gt;email,
                          'name'                      =&gt; $member-&gt;name,
                          'connect_id'                =&gt; $member-&gt;member_id,
                          'member' =&gt;$member
                      )
                  );
          }
      break;
  }
  
  
  function echo_json(array $arr) {
      echo json_encode($arr);
      exit;
  }
  </code></pre>
  
  <p><code>login.php</code></p>
  
  <pre><code>  &lt;?php
  
  $ips_connect_key = '3325a51154becfc88fXXXXXXXXX';
  $remote_login = 'IPB/remote_login.php';
  
  $email          = $_GET['email'];
  $password       = $_GET['password'];
  
  $key            = md5($ips_connect_key . $email);
  // fetch salt first
  $res = json_decode(file_get_contents($remote_login . "?do=get_salt&amp;id={$email}&amp;key={$key}"), true);
  
  $hash = crypt( $password, '$2a$13$' . $res['pass_salt'] );
  
  $res = json_decode(file_get_contents($remote_login . "?do=login&amp;id={$email}&amp;key={$key}&amp;password={$hash}"), true);
  $_COOKIE["ips4_member_id"]=41;
  $_COOKIE['ips4_pass_hash']="e195d3939b62342481dfc32fcf360538";
  $_COOKIE['ips4_IPSSessionFront']="sn359rogbto4j7jqhcqh10stl5";
  print_r($res);
  echo "&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;";
  print_r($_COOKIE);
  </code></pre>
  
  <p><code>calling login.php</code></p>
  
  <pre><code> login.php?email=XXXXX@gmail.com&amp;password=XXXXXX!
  </code></pre>
  
  <p>Here we are able to get member information but not able to set that member as logged in.</p>