我有路径错误:
警告:require_once(test / assets / includes / memberfunc.php):无法打开流:第4行的C:\ xampp \ htdocs \ test \ assets \包含\ logininc.php中没有此类文件或目录
致命错误:require_once():在C:\ xampp \ htdocs \ test \中打开所需的'test / assets / includes / memberfunc.php'(include_path ='。; C:\ xampp \ php \ PEAR')失败第4行的assets \ includes \ logininc.php
然而我的道路是正确的。基目录是测试。测试是我所有页面所在的位置。上面是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 :(得分:1)
'memberfunc.php'位于同一目录中,因此您不需要路径。出于剪切懒惰,我会在该目录中放置一份'db_config.php',这样你的语句就像这样:
require_once("memberfunc.php"); //LINE 4
require_once("db_config.php");
答案 1 :(得分:0)
如何使用dirname(__FILE__)
:
require_once(dirname(__FILE__)."/test/assets/includes/memberfunc.php");
require_once(dirname(__FILE__)."/test/assets/config/db_config.php");
答案 2 :(得分:0)
您想要的是当前的工作目录。这是通过两种不同的方式实现的:
相对路径:
require_once('./assets/includes/memberfunc.php');
绝对路径:
require_once(getcwd() . '/assets/includes/memberfunc.php');
这里的主要区别是HTML模板根据传递给浏览器的URL构建其相对路径。 PHP根据执行文件的磁盘目录构建其相对路径。