英文:
PHP file directory not found
问题
I've translated the code as you requested. Here's the translated code:
我正在制作一个简单的简历类型的网站,可以在其中编辑我的个人资料。我遇到的问题是,我有一个 header.php 文件,其中包含所有导航按钮。我将该 header 文件包含到每个页面中。
当我在我的个人资料页面上单击“编辑”个人资料时,它会将我带到另一个页面,启用了输入字段以编辑我的个人资料。问题是,当我在编辑个人资料页面上(位于子文件夹中)并使用“../header.php”包含根时,导航选项不起作用,因为在 header.php 中,所有文件都在根文件夹中,但当我从子文件夹包含头文件并尝试导航时,它尝试在子文件夹中查找文件,但该文件不存在。
它应该让我通过正常方式浏览页面,即通过返回一个目录,但我无法制作硬编码的解决方案,因为我的页眉位于根文件夹中。
以下是我的代码:
header.php:
<?php
// 需要数据库文件以进行连接。
require 'resources/database/mysqli_connect.php';
// 启动会话。
session_start();
$rank = 0;
// 如果会话不为空,分配变量并运行查询。
if (!empty($_SESSION["id"])) {
$id = $_SESSION["id"];
$rank = $_SESSION["rank"];
$result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
$row = mysqli_fetch_assoc($result);
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<header>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<a href="./index.php" class="navbar-brand">IKAWORLD</a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarMenu">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="./index.php" class="nav-link">Home</a>
</li>
<?php
if ($rank == 5) {
echo
"<li class='nav-item'>
<a href='./admin.php' class='nav-link'>Admin</a>
</li>";
}
?>
<?php
if (empty($_SESSION["id"])) {
echo
"<li class='nav-item'>
<a href='login.php' class='nav-link'>Login</a>
</li>
<li class='nav-item'>
<a href='register.php' class='nav-link'>Register</a>
</li>";
} else {
echo
"<li class='nav-item'>
<a href='profile.php' class='nav-link'>Profile</a>
</li>
<li class='nav-item'>
<a href='logout.php' class='nav-link'>Logout</a>
</li>";
}
?>
</ul>
</div>
</nav>
</header>
</html>
根导航页面示例(profile.php):
<?php
// 需要数据库文件以进行连接 //
include 'header.php';
require './resources/database/mysqli_connect.php';
// 启动会话。
$rank = 0;
// 如果用户已登录,则分配变量和值 //
if (!empty($_SESSION["id"])) {
// 已登录用户的ID //
$id = $_SESSION["id"];
$result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
$row = mysqli_fetch_assoc($result);
// 这是与用户关联的父ID //
// 检查已登录用户的级别 //
$rank = $_SESSION["rank"];
if ($rank == 1) {
$level = "User";
} elseif ($rank == 2) {
$level = "Verified User";
} elseif ($rank == 3) {
$level = "Family";
} elseif ($rank == 4) {
$level = "Friend";
} elseif ($rank == 5) {
$level = "Admin";
}
}
// 如果用户未登录,将其重定向到登录页面 //
else {
header("Location: ./login.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="resources/stylesheet/stylesheet.css">
<link rel="stylesheet" href="resources/stylesheet/register.css">
</head>
<body>
<div class="content-wrapper">
<br>
<h1 class="subheading">Profile</h1>
<table>
<th>Category</th>
<th>Value</th>
<tr>
<td>User ID:</td>
<td><?php echo $row['id']; ?></td>
</tr>
<tr>
<td>Username:</td>
<td><?php echo $row['username']; ?></td>
</tr>
<tr>
<td>First name:</td>
<td><?php echo $row['first_name']; ?></td>
</tr>
<tr>
<td>Last name:</td>
<td><?php echo $row['last_name']; ?></td>
</tr>
<tr>
<td>Email:</td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Password:</td>
<td>--------------</td>
</tr>
<tr>
<tr>
<td>Rank</td>
<td><?php echo $level; ?></td>
</tr>
</table>
<a href="edit_pages/editProfile.php"><button>Edit Profile</button></a>
<br><br>
</div>
</body>
</html>
编辑个人资料页面(位于子文件夹中):
<?php
// 需要连接的数据库文档 //
include '../header.php';
require '../resources/database/mysqli_connect.php';
$rank = 0;
// 如果会话不为空,分配变量并运行查询。
if (!empty($_SESSION["id"])) {
$id = $_
<details>
<summary>英文:</summary>
I'm making a simple resume-type website in which I can edit my profile. The issue that I am having is, I have a header.php file which has all the navigation buttons. I'm including that header file into every page which I have.
When I go on my profile and click 'Edit' profile, it takes me to another page with input fields enabled to edit my profile. The problem is, when I am on the edit profile page (which is in a subfolder), and it has included root using '../header.php', the navigation options are not working because on header.php, all files are in the root folder, but when I'm including the header file from a subfolder and trying to navigate, it's not going to the root folder, it's trying to find the file in the subfolder which doesn't exist.
It should let me navigate through the pages as normal by going back a directory, but I can't make a hard-coded solution as my header is in the root folder.
Here's my code:
header.php:
<?php
// requires database file for connection.
require 'resources/database/mysqli_connect.php';
// Starts the session.
session_start();
$rank = 0;
// If session is not empty, assigns variables and runs the query.
if(!empty($_SESSION["id"])){
$id = $_SESSION["id"];
$rank = $_SESSION["rank"];
$result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
$row = mysqli_fetch_assoc($result);
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<header>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<a href="./index.php" class="navbar-brand">IKAWORLD</a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarMenu">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="./index.php" class="nav-link">Home</a>
</li>
<?php
if($rank == 5){
echo
"<li class='nav-item'>
<a href='./admin.php' class='nav-link'>Admin</a>
</li>";
}
?>
<?php
if(empty($_SESSION["id"])){
echo
"<li class='nav-item'>
<a href='login.php' class='nav-link'>Login</a>
</li>
<li class='nav-item'>
<a href='register.php' class='nav-link'>Register</a>
</li>";
} else{
echo
"<li class='nav-item'>
<a href='profile.php' class='nav-link'>Profile</a>
</li>
<li class='nav-item'>
<a href='logout.php' class='nav-link'>Logout</a>
</li>";
}
?>
</ul>
</div>
</nav>
</header>
</html>
Root navigation page example (profile.php):
<?php
// Requires database file for connection //
include 'header.php';
require './resources/database/mysqli_connect.php';
// Starts the session.
$rank = 0;
// If user is logged in, assigns variables and values //
if(!empty($_SESSION["id"])){
// Logged in user's ID //
$id = $_SESSION["id"];
$result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
$row = mysqli_fetch_assoc($result);
// This is the parent ID associated with the user //
// This checks the rank of the logged user //
$rank = $_SESSION["rank"];
if ($rank == 1){
$level = "User";
} else if ($rank == 2){
$level = "Verified User";
} else if ($rank == 3){
$level = "Family";
} else if ($rank == 4){
$level = "Friend";
} else if ($rank == 5){
$level = "Admin";
}
}
// If user is not logged in, it brings them to the login page //
else{
header("Location: ./login.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="resources/stylesheet/stylesheet.css">
<link rel="stylesheet" href="resources/stylesheet/register.css">
</head>
<body>
<div class="content-wrapper">
<br>
<h1 class="subheading">Profile</h1>
<table>
<th>Category</th>
<th>Value</th>
<tr>
<td>User ID:</td>
<td><?php echo $row['id']; ?></td>
</tr>
<tr>
<td>Username:</td>
<td><?php echo $row['username']; ?></td>
</tr>
<tr>
<td>First name:</td>
<td><?php echo $row['first_name']; ?></td>
</tr>
<tr>
<td>Last name:</td>
<td><?php echo $row['last_name']; ?></td>
</tr>
<tr>
<td>Email:</td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Password:</td>
<td>--------------</td>
</tr>
<tr>
<tr>
<td>Rank</td>
<td><?php echo $level; ?></td>
</tr>
</table>
<a href="edit_pages/editProfile.php"><button>Edit Profile</button></a>
<br><br>
</div>
</body>
</html>
edit profile page (located in a subfolder):
<?php
// Requires DB document for connection //
include '../header.php';
require '../resources/database/mysqli_connect.php';
$rank = 0;
// If session is not empty, assigns variables and runs the query.
if(!empty($_SESSION["id"])){
$id = $_SESSION["id"];
$rank = $_SESSION["rank"];
$result = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
$row = mysqli_fetch_assoc($result);
}
else{
// If user not logged in, it redirects them //
header("Location: ../login.php");
}
// This code runs when user clicks update profile //
if(isset($_POST["updateProfile"])){
// Stores input data to variables + cleans them to prevent SQLi Injection //
$username = mysqli_real_escape_string($conn, $_POST["userName"]);
$firstname = mysqli_real_escape_string($conn, $_POST["firstName"]);
$lastname = mysqli_real_escape_string($conn, $_POST["lastName"]);
$email = mysqli_real_escape_string($conn, $_POST["email"]);
$password = mysqli_real_escape_string($conn, $_POST["password"]);
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Checks if any fields have been left empty //
if (empty($_POST["userName"]) || empty($_POST["firstName"]) || empty($_POST["lastName"]) || empty($_POST["email"]) || empty($_POST["password"])) {
// If missing fields found, it throws this error //
echo "<script>alert('Missing fields detected!')</script>";
}
else{
// If everything is good, it creates the query to update user details in the DB //
$query = "update users set username = '$username', first_name = '$firstname', last_name = '$lastname', email = '$email', password = '$hashed_password' where id = '$id'";
if ($conn -> query($query) == TRUE){
// If query is successful, it alerts the user with successful message //
echo "<script> alert('Updated Successful'); </script>";
// Redirects user/
header("Location: ../profile.php");
} else{
// If unsuccessful, this error appears //
echo "<script> alert('An error has occurred.'); </script>";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="../resources/stylesheet/register.css">
<link rel="stylesheet" href="../resources/stylesheet/stylesheet.css">
</head>
<body>
<div class="content-wrapper">
<br>
<h1 class="subheading">Edit Profile</h1>
<form method="post" action="">
<table>
<th>Category</th>
<th>Value</th>
<tr>
<td>Username:</td>
<td><input type="text" value="<?php echo $row['username']; ?>" name="userName"></td>
</tr>
<tr>
<td>First name:</td>
<td><input type="text" value="<?php echo $row['first_name']; ?>" name="firstName"></td>
</tr>
<tr>
<td>Last name:</td>
<td><input type="text" value="<?php echo $row['last_name']; ?>" name="lastName"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" value="<?php echo $row['email']; ?>" name="email"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
</table>
<button type="submit" name="updateProfile">Update Profile</button>
</form>
<br><br>
</div>
</body>
</html>
</details>
# 答案1
**得分**: 1
You should not use a relative path to link to other files. If you use the relative path, you rely on your current environment. However, your environment can change. So you may have to change your code. As an example, if you move your file to a subdirectory in another subdirectory of the subdirectory, that means adjusting the code...
Instead, I would use the absolute path. This links the complete path to your file, including your environment.
This can be slower but is probably not critical for your project. It has several advantages, for example, you don't have to change the code, and it's much more efficient when you collaborate with other programmers who may be working in different environments.
Maybe see this answer here for the absolute PATH: https://stackoverflow.com/a/11604489/21940880
<details>
<summary>英文:</summary>
You should not use a relative path to link to other files. If you use the relative path, you rely on your current environment. However, your environment can change. So you may have to change your code. As example you move your file in a Sub directory in another Sub directory of the Sub directory. That means adjusting the code...
Instead, I would use the absolute path. This links the complete path to your file, including your environment.
This can be slower, but is probably not critical for your project. It has several advantages, for example, you don't have to change the code and it's much more efficient when you collaborate with other programmers who may be working in different environments.
Maybe see this answer here for the absolute PATH: https://stackoverflow.com/a/11604489/21940880
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论