简体   繁体   English

如何从另一个文件引用PHP文件中的变量?

[英]How can I reference a variable in a PHP file from another one?

I have another problem, now I have variables in a PHP file that have to be referenced in another one, but it is doing it without success. 我还有另一个问题,现在我在PHP文件中有必须在另一个文件中引用的变量,但是这样做没有成功。

For further explanation, I have this segment in a PHP file: 为了进一步说明,我在PHP文件中包含此段:

if (isset($_REQUEST['login']) && isset($_REQUEST['password']))
    establishingConnection($_REQUEST['login'], $_REQUEST['password'], $xml);

Here, I must submit a username and a password to establish a connection with a web service that I'm developing. 在这里,我必须提交用户名和密码才能与正在开发的Web服务建立连接。

After that, the user can make operations like add/remove/change links/tags, and so on.. He can change his personal data (username, password, email or remove his own account). 之后,用户可以进行添加/删除/更改链接/标签等操作。.他可以更改其个人数据(用户名,密码,电子邮件或删除自己的帐户)。 For any of those operations, I need the current login (ie, his username). 对于任何这些操作,我需要当前登录名(即他的用户名)。 Like in this example: 像这个例子:

if (isset($_SESSION['login']))
{
    if (isset($_REQUEST['password']) && isset($_REQUEST['newUsername']))
        changeUsername($_SESSION['login'], $_REQUEST['password'], $_REQUEST['newUsername'], $xml);
    else {
        $msg = 'Some fields are missing';
        message($xml, $msg, 1);
    }
}
else
{
    $msg = 'Please login first!';
    message($xml, $msg, 1);
}

Unfortunately, I can't import the login data properly. 不幸的是,我无法正确导入登录数据。 I can see this problem, by seeing this message: 通过看到以下消息,我可以看到此问题:

$msg = 'Some fields are missing';

My main objective is to reference a PHP variable in a file, on another one. 我的主要目标是在另一个文件中引用一个PHP变量。 My JavaScript file is not responsible to provide th login's username. 我的JavaScript文件不负责提供登录名。 I tried to include php files, but without success. 我试图包括php文件,但没有成功。

Any suggestions to resolve this? 有什么建议可以解决这个问题吗?

EDIT: Well, I tried on my php, but I figured out some problems in my JavaScript script. 编辑:好吧,我尝试了我的php,但是我发现我的JavaScript脚本中存在一些问题。

I will post the functions that are responsible for this operation, since I don't have a clue where the problem is. 我将负责执行此操作的函数,因为我不知道问题出在哪里。

HTML: HTML:

        <div id="changeUserid">
            <form id="changeUseridForm">
                New username: <input type="text" id="newUserid" />
                <br>
                Type your password to validate your action: <input type="password" id="retypePass" />
                <br>
                <input type="submit" value="Change" />
            </form>
        </div>

My JavaScript (jQuery) function: 我的JavaScript(jQuery)函数:

$("#changeUseridForm").submit(function(){
        $.ajax({
            type: "GET",
            url: "API/root/users/changeUsername.php",
            data: {
            newUsername: $("#newUserid", this).val(),
            password: $("#retypePass", this).val(),
            xml: 1,
            },
            dataType: 'xml',
            success: function(xml){
                if($(xml).find("success").length > 0){
                    alert("Username changed successfully!");
                    window.location.reload();
                }
                else if($(xml).find("error").length > 0){
                    alert("You have to fill all the fields!");
                }
            }
        });

        return false;
    });

EDIT2: My problem in changing the username is solved. EDIT2:我在更改用户名方面的问题已解决。 Now, I have the problem that I can make a login with a old username with success. 现在,我遇到的问题是我可以成功使用旧用户名进行登录。 Any suggestion? 有什么建议吗?

I am not sure I am completely understand your problem, are both these code segments from same file or separate ones? 我不确定我是否完全理解您的问题,这两个代码段是来自同一文件还是来自单独的文件? And are you submitting two different html forms or is it just one form? 并且您要提交两种不同的html表单还是只是一种表单?

I am assuming, you have submitted a HTML form to one page, and then when you move to another page, you want to access those same values, is that right? 我假设您已经向一个页面提交了HTML表单,然后当您移至另一页面时,想要访问这些相同的值,对吗? You can't pass the $_REQUEST values from one page to another like this. 您不能像这样将$ _REQUEST值从一页传递到另一页。 You can either assign these values to some variable and then include that file, or use session variables to store these files: 您可以将这些值分配给某个变量,然后包括该文件,或者使用会话变量来存储这些文件:

Do this in first file: 在第一个文件中执行此操作:

$password = $_REQUEST['password'];
$newUsername = $_REQUEST['newUsername'];

Then, include the first file in second one and use the variables (assuming first file is a.php): 然后,在第二个文件中包含第一个文件并使用变量(假设第一个文件是a.php):

include "a.php";
if (isset($_SESSION['login'])){
    if (isset($password) && isset($newUsername)){
        changeUsername($_SESSION['login'], $password, $newUsername, $xml);
    } else {
        $msg = 'Some fields are missing';
        message($xml, $msg, 1);
    }
}

You can also use session variables to pass on data from one page to another, just make sure there is session_start() on start of every page you use session variables on. 您还可以使用会话变量将数据从一个页面传递到另一页面,只需确保在使用会话变量的每个页面的开始处都有session_start()

Note: You should also validate user submitted data before using it, but that is another topic. 注意:您还应该在使用之前验证用户提交的数据,但这是另一个主题。 Also, whenever in doubt, print/echo your variables to see what data is passing on and which variable is the problem. 此外,每当有疑问时,请打印/回显您的变量以查看正在传递的数据以及问题所在的变量。

EDIT (Question 2): Can you login from both new and old username? 编辑(问题2):可以同时使用新旧用户名登录吗? If yes, it means instead of replacing the currenct username for new one, you are adding a new user(name). 如果是,则意味着您要添加新的用户(名称),而不是用新的用户名代替当前的用户名。 Share how you are updaing the account info (that is, if you are using database, share the update query you are using) and someone might be able to help. 共享您更新帐户信息的方式(也就是说,如果您正在使用数据库,请共享您正在使用的更新查询),也许有人可以提供帮助。

I think the problem is related to Session_start(). 我认为问题与Session_start()有关。 Please make sure that you have included this before checking the session login and password variables. 在检查会话登录名和密码变量之前,请确保已包括此内容。

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

相关问题 如何从 PHP 中的另一个包含文件中引用变量? - How can I reference variables from another included file in PHP? 如何在php中从一个文件到另一个文件获取变量 - how do I get a variable from one file to another in php php 和 cron 作业问题,我可以从另一个 php 文件更改 php 文件中的一个变量吗? - php and cron job questions, can I change one variable in php file from another php file? 我在一个php文件中声明了一个变量并给了一个值,我该如何将该变量与另一个php文件中存储的值一起使用? - I have a variable declared and given a value in one php file, how can I use that variable with the value stored in it in another php file? 如何将PHP变量从一个文件多次传递到另一个文件? - How do I pass a PHP variable from one file to another multiple times? 如何将变量从一个PHP文件传输到另一个? - How to transfer variable from one php file to another? 如何在PHP中将变量从一个文件传递到另一个文件 - How to pass a variable from one file to another in php 如何在php中将一个表中的变量用作另一表的SELECT参数 - How can I use a variable from one table as a SELECT parameter for another table in php 将变量从一个PHP文件传递到另一个 - Passing an Variable from one PHP File to another 将php变量从一个文件传递到另一个文件? - Passing php variable from one file to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM