我目前正在尝试将文件包含到另一个文件中,它们位于两个单独的目录中,结构看起来像这样。
public_html
\includes
\check_login_status.php
\db_connect.php
\users
\users.php
我想将check_login_status.php
加入user.php
。
我这样做是为了包含它include_once("/public_html/includes/check_login_status.php");
我觉得这应该可行,但依赖于include的所有变量在调试时都返回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();
}
?>
答案 0 :(得分:1)
试试这个:
include_once("../includes/check_login_status.php");
public_html可能不在服务器的根文件夹中。
答案 1 :(得分:0)
// users.php
include_once __DIR__ ."/../includes/check_login_status.php"
尝试使用魔法常量__DIR__使其成为绝对路径。
将相对于调用脚本评估相对路径;如果您 - 例如 - 在index.php中包含users.php,则可能会导致意外行为:
E.g。
public_html
index.php // include "/users/users.php"
\includes
check_login_status.php
\users
users.php // include "../includes/check_login_status.php"
将使index.php包含在“public_html /../ includes / check_login ...”中,但不存在。
答案 2 :(得分:-1)
我认为你在寻找的可能是:
include "/path/to/my/file.php";