简体   繁体   中英

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 .

I am doing this to include it 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. 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

<?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();
}


?>

Try this:

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

It's possible that public_html is not in the root folder of the server.

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

Try to make it an absolute path using magic constant __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:

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.

我认为您正在寻找的可能是:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM