让用户更改自己的密码? [英] let user change their own password?

查看:77
本文介绍了让用户更改自己的密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习php和sql,所以请对我轻松一点,我知道我在某些地方会出错.我试图允许用户登录并能够更改其密码.我已经尝试过一个脚本,我相信它应该可以工作,但是我想我做错了,因为它只会链接到php函数页面,而根本不会更改密码.这是我的脚本:

I am just starting to learn php and sql so please go easy on me, i know i'm going to be wrong in certain places. I am trying to allow a user to login and be able to change their password. I have made an attempt of a script which i believe should work, but i guess i'm doing something wrong as it will just link to the php function page and not change the password at all. Here's my script:

HTML表单:

<form method="POST" action="includes/changepassword.php">
<p><input type="password" name="oldpasswd" id="oldpasswd" maxlength="30" placeholder="Old Password"></p>
<p><input type="password" name="newpsswd1" id="newpsswd1" maxlength="30" placeholder="New Password"></p>
<p><input type="password" name="newpsswd2" id="newpsswd2"maxlength="30" placeholder="Confirm Password"></p>
<input type="submit" name="submit" id="submit" value="change password">

changepassword.php文件:

changepassword.php file:

 <?php
require_once("session.php"); 
require_once("functions.php");
require('_config/connection.php');
function changepassword ($oldpasswd, $newpasswd1, $newpasswd2) {
    /*
     * RETURNS
     * 0 - if password changed
     * 1 - if new passwords are not equal
     * 2 - if user authentification problems
     */


        $oldpasswd = ($_POST['oldpasswd']);
        $newpasswd1 = ($_POST['newpasswd1']);
        $newpasswd1 = ($_POST['newpasswd2']);




    if ($newpasswd1 != $newpasswd2) {
        return 1;
    }

    //check user logged in changes OWN passwd
    $sql = "SELECT password FROM ptb_users WHERE id = ".$_SESSION['user_id'];
    $result = mysql_query($sql)or die('User not found: ' . mysql_error());

    if (md5($oldpasswd)==$result) { 

        //Encrypt $emailpassword in MD5 format for the database
        $md5_np=md5($newpasswd1);

        // Make a safe query
        $query = sprintf("UPDATE `ptb_users` SET `password` = '%s' 
                    WHERE `id` = ".$_SESSION['user_id'],
                    mysql_real_escape_string($md5_np));

        mysql_query($query)or die('Could not update password: ' . mysql_error());
        return 0;
    } else {
        return 2;
    }


}   
?>

我做错了什么?

推荐答案

它只会链接到php函数页面,而根本不会更改密码

it will just link to the php function page and not change the password at all

您告诉HTML表单完全这样做:<form method="POST" action="includes/changepassword.php">.但是另一方面,您永远不会调用函数.

You told the HTML form to do exactly that: <form method="POST" action="includes/changepassword.php">. But on the other hand, you never call your function.

这篇关于让用户更改自己的密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆