Connect to Joomla database in standalone script

APPLIES TO Joomla 2.5.x

Why?
I'm building a Joomla component which is to be compatible with Joomla versions 1.6.x to 2.5.x. Because I use dynamic scripts running in the background (mootools), some of these need to connect to the database but as they sit outside of the MVC structure, we need them to use the existing configuration file in order to retrieve the credentials (ie. username, password, database, etc.). For obvious reasons, these cannot be hardcoded.

What?
Any developer installing my component will do so on a website which uses different login details for their Joomla database. The example below demonstrates how to get these details and extract data from the database.

How?
Based on a script I found on StackOverflow. Not sure what version they were using so I modified it to make this work with my environment (Joomla 2.5.6). This is a cut down version of my download.php script:
copyraw
bool correctPass = CryptSharp.PhpassCrypter.CheckPassword(passwordPlain, joomlaPassword);
  1.  bool correctPass = CryptSharp.PhpassCrypter.CheckPassword(passwordPlain, joomlaPassword)
Clear as mud? Feel free to use the comment fields at the bottom of this page to ask a question.

Additional
I'm pretty sure you don't need all the require files and my solution may be crude, but this script works perfectly for what I had in mind.

Important!
There are a few concealed features of this script that I haven't made a song and dance about but are noteworthy:
  1. The only anti-code injection facility in this example is the "mysqli_real_escape_string" function applied to a number. You will need to be more vigilent.
  2. Variables are all cleared and redefined. To pass a value to this script you have to use the $_GET, $_POST or $_SESSION methods. Or redeclare it after the above.
  3. MYSQLI_USE_RESULT allows these results to be used once, remove it if you need counts or to use the data later on.

Further Post-Joomla 2.5 aides
  • Validating a login was MD5(password+salt) but is expected to be (not tested)
    bool correctPass = CryptSharp.PhpassCrypter.CheckPassword(passwordPlain, joomlaPassword);
Category: Joomla :: Article: 428

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Joes Word Cloud

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.