title: include Jfactory class in an external php file, Joomla
body: <p>I am writing a module for Joomla, at this point I really need to be able to connect to the database using Jfactory. Normally one could simply use <code>$db = JFactory::getDBO();</code> , but the PHP error tells me the JFactory class is not included. <strong>So now I need to know how to include this JFactory class</strong>. I've tried a couple of suggestions found on the internet, absent success, yet. This is the code (it works perfect on standalone)</p>
  
  <pre><code>    &lt;?php
  // server info
  $server = 'localhost';
  $user = 'ss';
  $pass = 'oo';
  $db = 'ss';
  
  $connection = mysql_connect($server, $user, $pass)  or die ("Could not connect to server ... \n" . mysql_error ());
   mysql_select_db($db) or die ("Could not connect to database ... \n" . mysql_error ());
  
  
  if(isSet($_POST['username']))
  {
  $username = $_POST['username'];
  $username = mysql_real_escape_string($username);
  $sql_check = mysql_query("SELECT Username FROM users WHERE Username='$username'");
  
  if(mysql_num_rows($sql_check))
  {
  echo '&lt;font color="#cc0000"&gt;&lt;STRONG&gt;'.$username.'&lt;/STRONG&gt; is already in use.&lt;/font&gt;';
  }
  else
  {
  echo 'OK';
  }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>I hope my problem is clear to you. Your help will be much appreciated. </p>
  
  <p><strong>Attempt 1</strong></p>
  
  <pre><code>&lt;?php
  
  include('../../../../configuration.php');
  
  include('../../../../libraries/joomla/factory.php');
  
  $config =&amp; JFactory::getConfig();
  
  // server info
  $server2 = $host;
  $user2 = $user;
  $pass2 = $password;
  $db2 = $db;
  
  $connection = mysqli_connect($server2, $user2, $pass2)  or die ("Could not connect to server ... \n" . mysqli_error ());
   mysqli_select_db($db2) or die ("Could not connect to database ... \n" . mysqli_error ());
  
  
  if(isSet($_POST['username']))
  {
  $username = $_POST['username'];
  $username = mysql_real_escape_string($username);
  $sql_check = mysql_query("SELECT username FROM #__users WHERE username='$username'");
  
  if(mysql_num_rows($sql_check))
  {
  echo '&lt;font color="#cc0000"&gt;&lt;STRONG&gt;'.$username.'&lt;/STRONG&gt; is already in use.&lt;/font&gt;';
  }
  else
  {
  echo 'OK';
  }
  
  }
  ?&gt;
  </code></pre>
  
  <p>but this is not working either.</p>
  
  <p><strong>attempt 2 (successful)</strong></p>
  
  <pre><code>include('../../../../configuration.php');
  $jc = new JConfig();
  $table = 'users';
  $users = $jc-&gt;dbprefix . $table;
  // connect to the database
  $mysqli = new mysqli($jc-&gt;host, $jc-&gt;user, $jc-&gt;password, $jc-&gt;db);
  </code></pre>
  
  <p>Now it is all working as I want. The only thing is now: safety. I'm not too sure about this being hacker proof. Can someone review this? thanks :)</p>