Take the 2-minute tour ×

I'm not trying to create a plugin for moodle, I'm simply creating an external script that checks the credentials a user enters into a form against the user table in a moodle database.

Right now in the script, it the username matches, but the password doesn't. I've read some information about moodle using salting, but I'm still not getting any luck.

I'm using something like this to check the password.

$salt = 'random string of chars found in config file';
$password = md5($_POST['password'].$salt);
$query = "SELECT * FROM `user` WHERE password = '$password';";

Any suggestions would be greatly appreciated.

share|improve this question
Could you try to log in your database and run this command? select * from user where password=MD5('your-passwordsalt'); and see what happens? Are you sure the column name is user and not mdl_user? –  franzlorenzon Jan 29 '13 at 14:16
add comment

1 Answer

up vote 1 down vote accepted

I have tested the code its working fine for me there are some possibilities maybe which you are doing wrong -

  1. Make sure $salt string is same as in config.php($CFG->passwordsaltmain) file.
  2. Make sure password is not directly reset from mysql database without moodle code.
  3. Missing table prefix in your query.

My code -

    if ($_POST['submit']) {

        //your host detail
        $link = mysql_connect('localhost', 'root', 'root') or die(mysql_error());

        //your dbname
        mysql_select_db('moodle_23') or die(mysql_error()); 

        $salt = 'MG.b.;w>0B)3c.^:OqDd#?^h'; //change with your salt

        $query = "SELECT * FROM mdl_user WHERE username = '{$_POST['username']}' AND password = '$password'";


        echo '<pre>';

        while ($row = mysql_fetch_assoc($result)) {
    <form action='' method="post">
        <p>Username = <input type="text" name ="username"/></p>
        <p>Password = <input type="password" name ="password"/></p>
        <input type="submit" value="submit" name="submit"/>


share|improve this answer
add comment

Your Answer


