简体   繁体   English

不确定如何在 HTML 文件中使用 PHP function 文件,该文件利用 Z4C4AD5FCAED007A3F7A4DBB1 中的 JS 变量

[英]Not sure how to use PHP function in HTML file that leverages JS variables from that HTML file

I have an index HTML page that grabs a user's username and password from a form.我有一个索引 HTML 页面,该页面从表单中获取用户的用户名和密码。

I want to base 64 encode this before passing it to a php file that makes a request to a server with the encoded credentials.我想在将其传递给 php 文件之前对其进行 base 64 编码,该文件使用编码凭据向服务器发出请求。

I tried doing something like:我尝试做类似的事情:

<script>
// The below function calls the PHP file responsible for retrieving campaign details. 
function getCampaignDetails() {
    var username = $('#username').val(); //This successfully returns the username.
    var password = $('#password').val(); //This successfully returns the password.
    
    var authentication_string = <?php $username = urldecode($_GET['username']); $password = urldecode($_GET['password']); echo base64_encode($username.':'.$password); ?>;
    
    $.ajax({
        type: "GET",
        url: "http://localhost/testing/get_campaign_details.php",
        headers: { 'Access-Control-Allow-Origin': '*' },
        data: {
            "authentication_string": authentication_string
        },
    });
}
</script>

But I get an error about an unexpected token < which I'm assuming is a syntax error in the authentication_string value.但是我收到一个关于意外令牌 < 的错误,我假设这是 authentication_string 值中的语法错误。 If I add quotes around this value, the php doesn't execute and I get the whole string as is passed to the php file, rather than the encoded credentials.如果我在此值周围添加引号,则 php 不会执行,并且我会得到传递给 php 文件的整个字符串,而不是编码的凭据。

Is there a way to use PHP in a basic HTML file that grabs a JavaScript variable value, uses a PHP function to get a new value, and then pass back this new value to the data in an Ajax request from the HTML file that is then subsequently utilized by another PHP file? Is there a way to use PHP in a basic HTML file that grabs a JavaScript variable value, uses a PHP function to get a new value, and then pass back this new value to the data in an Ajax request from the HTML file that is then随后被另一个 PHP 文件使用?

Or is there a way to base 64 encode something using an HTML/JavaScript function instead of PHP function?或者有没有办法使用 HTML/JavaScript function 而不是 PHP function 对 64 位进行编码?

Best,最好的,

You need to add quotation marks around the php that you're running to get the authentication_string .您需要在您正在运行的 php 周围添加引号以获取authentication_string

var authentication_string = "<?php $username = urldecode($_GET['username']); $password = urldecode($_GET['password']); echo base64_encode($username.':'.$password); ?>";

Javascript is expecting a value that it can assign to the variable authentication_string after the = like an int or a string. Javascript 期望它可以在 = 之后分配给变量authentication_string的值,例如 int 或字符串。 When it see's < it doesn't know what to do with it so it throws an unexpected token error.当它看到 < 时,它不知道如何处理它,所以它会抛出一个意外的令牌错误。

As a sidenote - passing a username and password in the querystring (the url) is not a good idea.作为旁注 - 在查询字符串(url)中传递用户名和密码不是一个好主意。 Even though they are encoded it's better to keep those kind of things away from prying eyes.即使它们是经过编码的,最好还是让这些东西远离窥探。 There's a post here that might be helpful on how to handle sensitive data like that.这里有一篇文章可能对如何处理此类敏感数据有所帮助。 Best way to pass a password via GET or POST 通过 GET 或 POST 传递密码的最佳方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM