如何调用包含在另一个文件中的函数?PHP


How do I call a function included in another file? PHP

我在其他地方找不到这个问题的答案,我希望这里的人可能知道。我已经连续花了9个小时试图让它发挥作用,我一直在不停地寻找。

我有三个PHP文件。第三个文件包含我的PHP函数,用于检查现有用户并将用户添加到数据库

user.inc.php

<?php
//checks if given username exists in database
function user_exists($user){
    $user = mysqli_real_escape_string($user);
    $sql= "SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'";
    $result = mysqli_query($con,$sql);
    if ($result == 1){
    return true;
    }
    else {
    return false;   
    }
}
function email_exists($email){
    $user = mysqli_real_escape_string($email);
    $sql="SELECT COUNT(`user_id`) FROM `users` WHERE `user_email` = '{$email}'";
    $result = mysqli_query($con,$sql);
//  $row = mysqli_fetch_assoc($total);
    if ($result == 1){
    return true;
    }
    else {
    return false;   
    }
}

//checks if given username/password is valid
function valid_credentials($user,$pass){
    $user = mysqli_real_escape_string(htmlentities($user));
    $pass = sha1($pass);
    $sql= "SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'AND `user_password` ='{$pass}'";
    $result = mysqli_query($con,$sql);
if ($result == 1){
    return true;
    }
    else {
    return false;   
    }
}

//adds a user to the database
function add_user($user,$pass,$email){
    mysqli_query($con,"INSERT INTO`users` (`user_name`,`user_password`,`user_email`) VALUES ('a','b','c')");
    $user = mysqli_real_escape_string(htmlentities($user));
    $pass = sha1($_REQUEST[$pass]); 
    mysqli_query($con,"INSERT INTO`users` (`user_name`,`user_password`,`user_email`) VALUES ('{$user}','{$pass}','{email}')");
}

?>

第二个PHP文件启动与数据库的连接,并在底部包含上一个文件。

init.inc.php

<?php 
session_start();
$exceptions = array('registerPage','login');
$page = substr(end(explode('/',$_SERVER['SCRIPT_NAME'])), 0, -4);
if (in_array($page, $exceptions)=== false){
    if (isset($_SESSION['username'])=== false ){
        header('Location: login.php');
        die();
    }
}
$con = mysqli_connect('127.0.0.1','root','','yingyujiaocheng');
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
mysqli_query($con,"INSERT INTO `users` (`user_name`,`user_password`) VALUES ('user','pass')");
$path = dirname(__FILE__);
include("{$path}/inc/user.inc.php");
?>

第三个PHP文件包括顶部的第二个文件,还包含HTML和表单。当第3页上的表单填写并提交时,它会将信息发送到第3页顶部的PHP脚本。这是针对从第一个文件调用函数的错误进行处理的。数据库工作正常,我在每个地方都执行了MYSQLI命令,唯一不工作的是从第三个PHP文件调用第一个PHP文件。函数user_exists、email_exists和add_user不能正确调用。

这是第三个文件:

registerPage.php

 <?php error_reporting(E_ALL);  
  include('core/init.inc.php');
  $errors = array();
  if(isset($_POST['username'],$_POST['password'],$_POST['email'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
     if (empty($username)){ 
         $errors[] = 'The username cannot be empty.';
     }
     if (empty($password)){
         $errors[] = 'The password cannot be empty.';    
     }
     if (empty($email)){
         $errors[] = 'The email field cannot be empty.';     
     }  
     if (user_exists($username)){       
         $errors[] = 'The username is already taken';       
     }
     if (email_exists($email)){
         $errors[] = 'The email already taken';         
     }

    if (empty($errors)){    
         add_user($username,$password,$email);
        //$_SESSION['username'] = htmlentities($username);
        //header('Location: protected.php');
        // die();
     }
  }
  ?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
  <link href="ext/Styles/styleSheet.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
  <div class="header" align="centre">
  <img src="ext/Images/Logo.png" width="150" height="80" style="float:left;" />
  <h2 class="headerFontClickedSmall" style="float: right; margin-top:36px; margin-right: 60px">  </h2>
  <h2 class="headerFontUnclickedSmall" style="float: right; margin-top:36px; margin-right: 10px"> / </h2>
  <h2 class="headerFontUnclickedSmall" style="float: right; margin-top:36px; margin-right: 10px">  </h2>
  <h2 class="cornerBox1" style="float: right; margin-top:20px; margin-right: 50px">  </h2>
  </div>
  <div class="content">
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <br />
  <?php echo "Username is :" . $username . "<br>";
echo "Password is :" . $password;
?>
  <br />
  <h1 class="contentHeader" style="">  </h1> 
  <div>
      <?php 
          if (empty($errors) === false){
  ?>
  <ul>
      <?php
          foreach ($errors as $error){
            echo "<li>{$error}</li>";
         }
          ?>
      </ul>
         <?php
         }
      ?>
  </div>
  <form action="registerPage.php" method="POST">
  <h1 class="contentRegisterText" style=""> : <input class="inputbox" style="margin-left:30px" type="text"  
                                                         name = "username" id="username"/> </h1>
  <br />
  <h1 class="contentRegisterText" style=""> : <input class="inputbox" style="margin-left:30px" type="text" 
                                                         name = "email" id="email"/> </h1>
  <br />
  <h1 class="contentRegisterText" style="">: <input class="inputbox" style="margin-left:30px" type="password" 
                                                         name = "password" id="password"/> </h1>
  <br /><br /><br /><br /><br /><br />
  <input style="margin-left:30px" type="submit"  value = "" id="Register"/> </form>
  </div>
  <div class="footer" align="center" >
      <div class="floating-box" style="margin-top:40px" >
          <dl>
              <dt><h1 class="footerTitle">社交媒体</h1></dt>
              <br />
              <dd><a href="http://www.huya.com/lucio">
  <img src="ext/Images/HuyaLogo.png" alt=" " width="42" height="42" outline="none">
  </a> 
                  <img src="ext/Images/weixinLogo.png" width="40" height="40" style="margin-left:3;"/><img src="ext/Images/logo-qq.png" width="40" height="40" /></dd>          
          </dl>
      </div>
      <div class="floatingboxFooter1" style="margin-top:40px">
          <dl>      
              <dt><h1 class="footerTitle"></h1> </dt>
              <br />
              <dd><h1 class="footerSmall">:</h1></dd>
              <dd><h1 class="footerSmall">Weixin:  </h1></dd>
              <dd><h1 class="footerSmall">QQ:      </h1></dd>
          </dl>
      </div>
      <div class="floating-box" style="margin-top:64px">
          <dl>      
              <br />
              <dd><h1 class="footerSmall">bangzhu@yingyujiaocheng.com</h1></dd>
              <dd><h1 class="footerSmall">yingyujiaocheng</h1></dd>
              <dd><h1 class="footerSmall">yingyujiaocheng</h1></dd>
          </dl>
      </div>
  <div class="floatingboxFooter1" style="margin-top:40px">
          <dl>      
              <dt><h1 class="footerTitle"></h1> </dt>
              <br />
              <dd><h1 class="footerSmall"></h1></dd>
              <dd><h1 class="footerSmall"></h1></dd>
              <dd><h1 class="footerSmall"></h1></dd>
          </dl>

  </div> 
  </body>
  </html>

感谢您的关注,如有帮助,我们将不胜感激,

干杯

Lucio

尝试调试$path变量,我认为问题出在您此行中包含的路径上。

include("{$path}/inc/user.inc.php");或者尝试在user.inc.php中的每个函数上插入global $con,如下所示:

function user_exists($user) { global $con; // rest of the code... }