我正在尝试从外部脚本更新用户密码。 我使用以下脚本添加用户:
$login = $_POST['login'];
$password = $_POST['password'];
//Generate a random string
$salt = genRandomPassword();
//$pass is the encripted password
$pass= md5(stripslashes($password).$salt) .'.'.$salt;
// Insert columns.
$columns = array('name', 'username', 'email', 'password');
// Insert values.
$values = array($db->quote($name) , $db->quote($login),
$db->quote($email), $db->quote($pass));
$query = $db->getQuery(true);
// Prepare the insert query.
$query
->insert($db->quoteName(T_USERS)) //T_USERS = users table
->columns($db->quoteName($columns))
->values(implode(',', $values));
它工作正常,用户可以很好地登录。 但是,当我使用以下脚本更新密码时:
//random string
$salt = genRandomPassword();
//$pass is the encripted password
$pass= md5(stripslashes($password).$salt) .'.'.$salt;
$fields = array(
$db->quoteName('password') . ' = ' . $db->quote($pass)
);
$conditions = array($db->quoteName('id') . ' = ' . $session->get('user_id'));
$query = $db->getQuery(true);
$query->update(T_USERS)->set($fields)->where($conditions);
$db->setQuery($query);
$db->execute();
密码已更新到数据库中,但身份验证失败。
请帮忙
答案 0 :(得分:0)
我之前尝试过,但最后我以joomla的方式做了,首先你需要在你的脚本中使用这段代码:
define( '_JEXEC', 1 );
define('JPATH_BASE', '.');
define( 'DS', DIRECTORY_SEPARATOR );
require_once ('includes/defines.php' );
require_once ( 'includes/framework.php' );
我有两个功能:
function create()
{
$openId = $this->getOpenId();
$udata = array(
'name' => $_POST['name'],
'username' => $_POST['name'],
'password' => $_POST['password'],
'email' => $_POST['email'],
'groups' => array(2), // set the usergroup(s) here (id)
);
$user = new JUser;
try {
$user->bind($udata);
$user->save();
if ($user->id == 0) {
$lang = &JFactory::getLanguage();
$lang->load('lib_joomla');
echo json_encode(array('success' => false, 'message' => JText::_($user->getError())));
} else {
$sql = "INSERT INTO `bak_fields_values` (`field_id`, `item_id`, `value`) VALUES ('1', '" . $user->id . "', '" . $openId . "')";
$this->conn->query($sql);
echo json_encode(array('success' => true, 'message' => 'Ok'));
}
} catch (Exception $ex) {
echo 'Exception: ', $e->getMessage(), '\n';
}
}
和
function save()
{
$rest_json = file_get_contents("php://input");
$_POST = json_decode($rest_json, true);
$user = new JUser($_POST['id']);
$udata = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
);
if ($_POST['password'] != '') {
$udata['password'] = $_POST['password'];
$udata['password2'] = $_POST['password'];
}
try {
$user->bind($udata);
$user->save();
if (count($user->getErrors()) > 0) {
$lang = &JFactory::getLanguage();
$lang->load('lib_joomla');
echo json_encode(array('success' => false, 'message' => JText::_($user->getError())));
} else {
echo json_encode(array('success' => true, 'message' => 'Ok'));
}
} catch (Exception $ex) {
echo 'Exception: ', $e->getMessage(), '\n';
}
}