-3

i have a problem including MySQL connection file into my 'functions.inc.php'. If i require inside my functions, it works, but functions doesn't work if i include into another page. Like this:

function AddToVoteLogs($server_ip){
    include_once 'dbh.inc.php'; // WORK

    CODE ...
}
include_once 'dbh.inc.php'; // dont work
function AddToVoteLogs($server_ip){
    CODE ...
}

I try to do with _ DIR _ still dont work. Another problem. My functions work in functions.inc.php page but dont work in another pages.

function AddToVoteLogs($server_ip){
    include_once 'dbh.inc.php'; // WORK
    $Zip = getUserIP();
    
    $sql = "SELECT last_vote FROM votes WHERE IP = '$Zip';";
    $result = mysqli_query($conn, $sql);
    while ($row = mysqli_fetch_assoc($result)) {
        if (time() - 86400 >= $row['last_vote']) {
            $hoursIn = 1;
        }
        else {
            $hoursIn = 0;
        }
    }
    date_default_timezone_set('Europe/Vilnius');
    $dates = date("Y-m-d H:i:s");
    $ipas = getUserIP();
    $sqla = "INSERT INTO vote_log VALUES ('$dates', '$ipas', '$server_ip', $hoursIn);";
    $result = mysqli_query($conn, $sqla);
}
Shadow
  • 33,525
  • 10
  • 51
  • 64
Marijus12
  • 38
  • 6
  • Are you asking about JavaScript or PHP? I've added PHP and MYSQL tags, and removed JS/HTML – evolutionxbox Dec 29 '21 at 00:55
  • 1
    For the question to be clear, show exactly what is in each file. How u call it and what is inside dbh.inc.php. Remove the second part if it is not relevant to the first question. – Itay Moav -Malimovka Dec 29 '21 at 00:58
  • It usually doesn't matter if its called within a function or not. Frequently its caused by misunderstanding of [include path resolution](https://stackoverflow.com/questions/1973813/how-does-include-path-resolution-work-in-require-once). Remember that paths are resolved relative to the entry script. You may try "require_once" instead of "include_once". It complains if the file is not found. The error message would show what path it is expecting. Then you can add `__DIR__` to construct the correct path. – Koala Yeung Dec 29 '21 at 02:53

3 Answers3

0

In your case (because I do not know the structure of your project), it is better to use only absolute paths for include_path(). To do this, you should declare a constant with a root path in the root of the project and use it in include_path() in the future.

For example:

// index.php
define('DEFINED_ROOT_PATH', __DIR__.'/your-project-root-dir/'); // or $_SERVER[DOCUMENT_ROOT] or any other way
...

// Your custom .php file
include_once(DEFINED_ROOT_PATH. '/lib/dbh.inc.php');
...
Oleg Barabanov
  • 2,468
  • 2
  • 8
  • 17
0

It usually doesn't matter if its called within a function or not. Frequently its caused by misunderstanding of include path resolution.

Remember that paths are resolved relative to the entry script (e.g. usually index.php). Not the functions.inc.php here.

To debug this kind of issue, try:

  1. Use "require_once" instead of "include_once". It complains if the file is not found. The error message would show what path it is expecting.

  2. It's usually more reliable doing paths with the constant __DIR__. The constant __DIR__ is defined against the current script file (functions.inc.php in your case). So unless you move that file around frequently, this is much more reliable.

  3. If your dbh.inc.php is in the same directory as functions.inc.php, the path to it should be:

    __DIR__ . '/dbh.inc.php'
    
Koala Yeung
  • 7,475
  • 3
  • 30
  • 50
0

Use global variable

Try This

function AddToVoteLogs($server_ip){
//include_once 'dbh.inc.php'; // NOT WORK
global $conn; // WORK
$Zip = getUserIP();

$sql = "SELECT last_vote FROM votes WHERE IP = '$Zip';";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    if (time() - 86400 >= $row['last_vote']) {
        $hoursIn = 1;
    }
    else {
        $hoursIn = 0;
    }
}
date_default_timezone_set('Europe/Vilnius');
$dates = date("Y-m-d H:i:s");
$ipas = getUserIP();
$sqla = "INSERT INTO vote_log VALUES ('$dates', '$ipas', '$server_ip', $hoursIn);";
$result = mysqli_query($conn, $sqla);

}