POST之前的Javascript加密传递 [英] Javascript Encrypt pass before POST

查看:98
本文介绍了POST之前的Javascript加密传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下提交按钮时,我正在尝试加密密码.

I am trying to encrypt a password when submit button is pressed.

我不明白为什么密码输入的值没有更改,加密正确完成了.

I don't understand why the value of password input is not changed, the ecryption is done correctly.

<script>
    function cript(){
        var pass = CryptoJS.SHA1("<?php echo $_POST["password"]; ?>");

        var passString = pass.toString();

        console.log(passString);
        document.getElementById('inputPassword').value = passString;
    }
</script>

以及在HTML表单上:

and on HTML Form:

<input type="submit" class="btn btn-primary" value="Proceed" onclick="cript()">

有人可以告诉我解决方案吗?

Can anyone tell me the solution?

推荐答案

您的PHP代码无法在单击时执行.只能在服务器加载文件时执行该操作,因为您正在等待用户输入密码,然后调用该功能将无法使用.

Your PHP code can't execute on click. It can only be executed by server when you load the file, since you are waiting for user to input the password and then call the function it is not going to work.

您的javascript var传递为空,因为此时您没有POST.

Your javascript var pass is empty since you don't have POST at that point in time.

可以工作的流程是: 执行PHP->提供html + javascript->用户输入->加密->提交

Flow that can work is: execute PHP -> deliver html + javascript -> user input -> do crypt -> submit

因此,在这种情况下,您不能依靠PHP为您提供数据.您需要在这里使用其他方法.

So you can not rely on PHP to give you data in this case. You need to go with different approach here.

尝试

<script>
    function cript(){
        var pass = CryptoJS.SHA1(document.getElementById('inputPassword').value);

        var passString = pass.toString();

        console.log(passString);
        document.getElementById('inputPassword').value = passString;
    }
</script>

这仅解决与尝试传递PHP有关的问题 变量到javascript变量.有关SHA1的安全问题是 另一个话题,如果这是学校项目或摆弄 学习JavaScript然后就可以了.本示例出于学习目的如何 使用javascript访问元素中的值是可以的.但是如果这 代码将要被放置在服务器上并被使用,那么SHA1应该 绝对要避免

This only takes care of problem related with trying to pass PHP variable to javascript variable. Security issues about SHA1 are another topic and if this is school project or fiddling around to learn javascript then it is ok. This example for learning purposes how to use javascript to access values in elements is ok. However if this code is ever going to be put on server and be used, then SHA1 should definitely be avoided

这篇关于POST之前的Javascript加密传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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