簡體   English   中英

如何調用另一個文件中包含的函數? 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頁上的表單后,它將信息發送到PHP腳本的第3頁頂部。 對於從第一個文件調用函數的錯誤,將對其進行處理。 數據庫工作正常,我從各個地方都完成了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>

多謝您的協助,不勝感激,

干杯

盧西奧

嘗試調試$path變量,我認為問題出在此行中包含的路徑中。

include("{$path}/inc/user.inc.php"); 或者嘗試像這樣在user.inc.php中的每個函數之上插入global $con

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM