2013-07-22 50 views
0

我尋路錯誤:憋屈一個奇怪的尋路錯誤

Warning: require_once(test/assets/includes/memberfunc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\test\assets\includes\logininc.php on line 4

Fatal error: require_once(): Failed opening required 'test/assets/includes/memberfunc.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\test\assets\includes\logininc.php on line 4

然而,我的路徑是正確的。基目錄是測試。測試是我所有網頁的位置。在上面是assets/includes/logininc.php和assets/configs/db_config.php。一切都應該是正確的。請指教

這是我最簡單的形式,在測試稱爲login.php中坐鎮。

<form id="login-form" method="post" action="assets/includes/logininc.php"> <fieldset> 
    <legend>Login </legend> 
    <p>Please enter your username and password to access the administrator's panel</p> 

    <label for="username"> <input type="text" name="username" id="username" />Username: </label> <label for="password"> <input type="password" name="password" id="password" />Password: </label> <label for="submit"> <input type="submit" name="submit" id="submit" value="Login" /> </label> </fieldset> </form> 

這反過來又引導我到下面的文件。 Logininc.php。它位於assets/includes/logininc.php

<?php 


require_once("/test/assets/includes/memberfunc.php"); //LINE 4 
require_once("/test/assets/config/db_config.php"); 



session_start(); 

if($_SESSION['logged_in'] ==true) { 

    redirect('/test/index.php'); 
     echo "success"; 
} 
else{ 
    if ((!isset($_POST['username'])) || (!isset($_POST['password'])) or           


    (!ctype_alnum($_POST['username']))) 

    redirect("/test/login.php"); 
} 

    $mysqli = @new mysqli(HOST, NAME, PW, DB); 

//check connection 

if(mysqli_connect_errorno()) 
{ 
    printf("Unable to connect to DB! : %s", 
      mysqli_connect_error()); 
    exit(); 
} 

//Escape unsafe chars 

$username = $mysqli->real_escape_string($_POST['username']); 
$username = $mysqli->real_escape_string($_POST['password']); 

//Time to make a SQL Q for execution 

$sql = "SELECT * FROM users WHERE username= '".$username . "'AND password = '" . md5($password) . "'"; 
$result = $mysqli->query($sql); 

// If one row is returned, USER AND PW is valid! 

if (is_object($result) && $result->num_rows == 1) { 

    //set session variable for login true 

    $_SESSION['logged_in'] = true; 

       redirect('../../index.php'); 
} 
else{ //if number of rows is not one redirect back to login 
redirect('../../login.php'); 
} 


    ?> 

請指教。

+0

嘗試'require_once(「memberfunc.php」);' –

+0

在您的assets \ includes/logininc.php文件中的php開始標記後面執行'print getcwd();'以確保路徑,因爲這幾乎肯定是這樣。 –

+0

@koala_dev試過了,但後來我得到完全相同的錯誤DB_CONFIG。也嘗試過你的建議,但沒有明確。丹尼爾菲格羅亞嘗試了你的建議,但似乎我對自己的道路是正確的?回聲= C:\ XAMPP \ htdocs中\測試\資產\包括 –

回答

0

如何使用dirname(__FILE__)

require_once(dirname(__FILE__)."/test/assets/includes/memberfunc.php"); 
require_once(dirname(__FILE__)."/test/assets/config/db_config.php"); 
+0

好的,我試過了。但它是正確的?我不明白。請參閱警告:require_once(C:\ xampp \ htdocs \ test \ assets \ includes/test/assets/includes/memberfunc.php):無法打開流:C:\ xampp \ htdocs \ test \ assets中沒有此類文件或目錄\ include \ logininc.php在第5行 –

1

「memberfunc.php」是在同一個目錄,所以你並不需要的路徑。進出剪切懶惰,我會把「db_config.php」的副本,該目錄還讓你的語句應該是這樣的:

require_once("memberfunc.php"); //LINE 4 
require_once("db_config.php"); 
0

你想要的是當前的工作目錄。這是通過兩種不同的方式來實現:

相對路徑:

require_once('./assets/includes/memberfunc.php'); 

絕對路徑:

require_once(getcwd() . '/assets/includes/memberfunc.php'); 

這裏的主要區別是,基於傳遞的URL的HTML模板建立的相對路徑到瀏覽器。 PHP根據磁盤上的哪個目錄執行文件來構建其相對路徑。