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

时间:2016-01-10 23:05:27

标签: 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命令,唯一不起作用的是从第3个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>

感谢您的关注,感谢任何帮助,

干杯

卢西奥

1 个答案:

答案 0 :(得分:2)

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

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

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