2017-02-12 161 views
0

我目前正在尝试将文件包含到另一个文件中,它们位于两个单独的目录中,看起来像这样。php文件包含不同目录下的文件

public_html 
    \includes 
     \check_login_status.php 
     \db_connect.php 
     \users 
      \users.php 

我想包括check_login_status.phpuser.php

我这样做,包括它include_once("/public_html/includes/check_login_status.php");

我觉得这应该工作,但是在调试的时候都依赖于包括变量返回null。是否有另一种方法可以将目录中的文件包含在与目录相同的级别中?

check_login_status.php

<?php 
session_start(); 
include_once("/db_connnect.php"); 
// Files that inculde this file at the very top would NOT require 
// connection to database or session_start(), be careful. 
// Initialize some vars 
$user_ok = false; 
$log_id = ""; 
$log_username = ""; 
$log_password = ""; 
// User Verify function 
function evalLoggedUser($conx,$id,$u,$p){ 
    $sql = "SELECT ip FROM users WHERE id='$id' AND username='$u' AND password='$p' AND activated='1' LIMIT 1"; 
    $query = mysqli_query($conx, $sql); 
    $numrows = mysqli_num_rows($query); 
    if($numrows > 0){ 
     return true; 
    } 
} 
if(isset($_SESSION["userid"]) && isset($_SESSION["username"]) && isset($_SESSION["password"])) { 
    $log_id = preg_replace('#[^0-9]#', '', $_SESSION['userid']); 
    $log_username = preg_replace('#[^a-z0-9]#i', '', $_SESSION['username']); 
    $log_password = preg_replace('#[^a-z0-9]#i', '', $_SESSION['password']); 
    // Verify the user 
    $user_ok = evalLoggedUser($db_connect,$log_id,$log_username,$log_password); 
} else if(isset($_COOKIE["id"]) && isset($_COOKIE["user"]) && isset($_COOKIE["pass"])){ 
    $_SESSION['userid'] = preg_replace('#[^0-9]#', '', $_COOKIE['id']); 
    $_SESSION['username'] = preg_replace('#[^a-z0-9]#i', '', $_COOKIE['user']); 
    $_SESSION['password'] = preg_replace('#[^a-z0-9]#i', '', $_COOKIE['pass']); 
    $log_id = $_SESSION['userid']; 
    $log_username = $_SESSION['username']; 
    $log_password = $_SESSION['password']; 
    // Verify the user 
    $user_ok = evalLoggedUser($db_connect,$log_id,$log_username,$log_password); 
    if($user_ok == true){ 
     // Update their lastlogin datetime field 
     $sql = "UPDATE users SET lastlogin=now() WHERE id='$log_id' LIMIT 1"; 
     $query = mysqli_query($db_connnect, $sql); 
    } 
} 
?> 

db_connect.php

<?php 
$db_connect = mysqli_connect("localhost", "formfitdata", "**********", "formfit_users"); 
//check if connection error 
if (mysqli_connect_errno()){ 
    echo mysqli_connect_error(); 
    exit(); 
} 


?> 

回答

1

试试这个:

include_once("../includes/check_login_status.php"); 

这有可能是的public_html不在的根文件夹SE rver。

+0

我试过了,在调试过程中变量仍然返回null。 – sbowde4

+0

我认为那么错误不在包含而是在目标文件中。如果你尝试require_once,它会返回一个错误吗?如果不是的话,那么include就是可以工作的,它必须是代码中的东西。 –

+0

需要一次,不会返回错误,但该文件包含在同一目录中的其他文件中,并且它正常执行其功能。由于某些原因,变量返回null。 – sbowde4

-1

我认为你正在寻找的可能是什么:

include "/path/to/my/file.php"; 
+0

我意识到这一点,但路径无法正常工作。 – sbowde4

0
// users.php 
include_once __DIR__ ."/../includes/check_login_status.php" 

尝试用魔法不断__DIR__使它成为绝对路径。

相对路径将相对于调用脚本进行评估;如果你 - 例如 - 在index.php中包含users.php,它可能会导致意外的行为:

E.g.

public_html 
    index.php // include "https://stackoverflow.com/users/users.php" 
    \includes 
    check_login_status.php 
    \users 
    users.php // include "../includes/check_login_status.php" 

将使的index.php从有 “的public_html /../包括/ check_login ......”,这不存在。