[英]php file includes with files in different directories
I am currently trying to include a file into another file, they are in two separate directories in a structure that looks like this. 我目前正在尝试将一个文件包含到另一个文件中,它们位于结构类似的两个单独目录中。
public_html
\includes
\check_login_status.php
\db_connect.php
\users
\users.php
I want to include check_login_status.php
into user.php
. 我想将check_login_status.php
包含到user.php
。
I am doing this to include it include_once("/public_html/includes/check_login_status.php");
我这样做是为了包括include_once("/public_html/includes/check_login_status.php");
I feel like this should work, but all the variables that depend on the include are returning null when debugging. 我觉得这应该工作,但是所有依赖于include的变量在调试时都返回null。 Is there another way to include a files from a directory on the same level as the directory you are in? 还有另一种方法可以包含与您所在目录处于同一级别的目录中的文件吗?
check_login_status.php 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 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();
}
?>
Try this: 尝试这个:
include_once("../includes/check_login_status.php");
It's possible that public_html is not in the root folder of the server. public_html可能不在服务器的根文件夹中。
// users.php
include_once __DIR__ ."/../includes/check_login_status.php"
Try to make it an absolute path using magic constant __DIR__ . 尝试使用魔术常数__DIR__使其成为绝对路径。
Relative paths will be evaluated relative to the calling script; 相对路径将相对于调用脚本进行评估; if you - for example - include users.php within an index.php it could lead to unexpected behaviour: 例如,如果您在index.php中包含users.php,则可能会导致意外的行为:
Eg 例如
public_html
index.php // include "/users/users.php"
\includes
check_login_status.php
\users
users.php // include "../includes/check_login_status.php"
Will make index.php include from "public_html/../includes/check_login..." which does not exist. 将使index.php包含不存在的“ public_html /../ includes / check_login ...”。
我认为您正在寻找的可能是:
include "/path/to/my/file.php";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.