首页 > 解决方案 > 如何将函数()中的变量访问到另一个php文件

问题描述

我将 login_auth.php 文件包含到 form_login.php 中,现在,我想将函数 adminAutentication 中的两个变量访问到 form_login.php 我尝试过但没有用。

login_auth.php

$uname_err= '';
$psswd_err = '';

function adminAuthentication() {
   $psswd_err = "The password you entered was not valid!";
   $uname_err = "No account found with that username!";
}

form_login.php

<?php include 'login_auth.php'; ?>

<form method="post">
   <div class="form-group">
       <span class="text-danger"><?php echo $uname_err ?></span> // problem here cannot access the variable
   </div>
   <div class="form-group">
       <span class="text-danger"><?php echo $psswd_err; ?></span> // problem here cannot access the variable
   </div>
    <button type="submit" name="submit">Login</button>
</form>

标签: php

解决方案


在 adminAuthentication() 函数中,您必须声明全局变量:

function adminAuthentication() {
   global $psswd_err, $uname_err ;
   $psswd_err = "The password you entered was not valid!";
   $uname_err = "No account found with that username!";
}

推荐阅读