php - 使用未定义的根路径
问题描述
我正在关注一个教程,我得到一个未定义的常量 ROOT_PATH。我有一个注册页面,其中包含一个名为 path.php 的文件,其中包含常量 ROOT_PATH,在该文件下方我有另一个包含链接一个名为 users 的文件的文件。
<?php
include(ROOT_PATH . "/app/database/db.php");
include(ROOT_PATH . "/app/helpers/validateUser.php");
$errors = array();
$username = '';
$email = '';
$password = '';
$passwordConf = '';
if(isset($_POST['register-btn'])){
$errors = validateUser($_POST);
if(count($errors) ===0){
unset($_POST['register-btn'], $_POST['passwordConf']);
$_POST['admin'] = 0;
$_POST['password'] = password_hash($_POST['password'], PASSWORD_DEFAULT);
$user_id = create('users', $_POST);
$user = selectOne('users', ['id' => $user_id]);
$_SESSION['id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['admin'] = $user['admin'];
$_SESSION['message'] = 'you are now logged in';
$_SESSION['type'] = 'success';
if($_SESSION['admin']){
header('location: ' . BASE_URL . '/admin/dashboard.php');
} else {
header('location: ' . BASE_URL . '/index.php');
}
header('location: ' . BASE_URL . '/index.php');
exit();
} else{
$username = $_POST ['username'];
$email = $_POST ['email'];
$password = $_POST ['password'];
$passwordConf = $_POST ['password'];
}
if(isset($_POST['login-btn'])){
$errors = validateLogin($_POST);
if(count($errors) === 0){
$user = selectOne('users', ['username' => $_POST['username']]);
if ($user && password_verify($_POST['password'], $user['password'])){
$_SESSION['id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['admin'] = $user['admin'];
$_SESSION['message'] = 'you are now logged in';
$_SESSION['type'] = 'success';
if($_SESSION['admin']){
header('location: ' . BASE_URL . '/admin/dashboard.php');
} else {
header('location: ' . BASE_URL . '/index.php');
}
header('location: ' . BASE_URL . '/index.php');
}
exit();
} else{
array_push($errors, 'wrong credentials');
}
}
}
现在根据他的说法,我不需要在用户中链接 path.php,因为在注册页面上我将 path.php 链接到 users.php 但我仍然收到此消息:警告:使用未定义的常量 ROOT_PATH - 假定为'ROOT_PATH '(这将在 PHP 的未来版本中引发错误)在第 5 行的 C:\xampp\htdocs\phpblogtut\app\controlers\users.php
这是 register.php :
<?php include("path.php"); ?>
<?php include(ROOT_PATH . "/app/controlers/users.php"); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<!-- Custom CSS -->
<link rel="stylesheet" href="assets/css/style.css">
<title>Register</title>
</head>
<body>
<!-- header -->
<?php include(ROOT_PATH . '/app/includes/header.php');?>
<!-- // header -->
<div class="auth-content">
<form action="register.php" method="post">
<h3 class="form-title">Register</h3>
<?php include(ROOT_PATH . "/app/helpers/formErrors.php");?>
<div>
<label>Username</label>
<input type="text" name="username" value="<?php echo $username; ?>" class="text-input">
</div>
<div>
<label>Email</label>
<input type="email" name="email" value="<?php echo $email; ?>" class="text-input">
</div>
<div>
<label>Password</label>
<input type="password" name="password" value="<?php echo $password; ?>" class="text-input">
</div>
<div>
<label>Confirm Password</label>
<input type="password" name="passwordConf" value="<?php echo $passwordConf; ?>" class="text-input">
</div>
<div>
<button type="submit" name="register-btn" class="btn">Register</button>
</div>
<p class="auth-nav">Or <a href="<?php echo BASE_URL . '/login.php' ?> ">Sign In</a></p>
</form>
</div>
<!-- JQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="assets/js/script.js"></script>
</body>
</html>
这是 path.php :
<?php
define('ROOT_PATH',realpath(dirname('_FILE_')));
define("BASE_URL", 'http://localhost/phpblogtut');
解决方案
我认为您缺少在 users.php 中包含 path.php
你的 users.php 文件应该是
<?php include_once("path.php"); ?> // **include path.php in users.php**
<?php include(ROOT_PATH . "/app/database/db.php");
include(ROOT_PATH . "/app/helpers/validateUser.php"); ?>
并且 register.php 文件应该是
<?php include_once("path.php"); ?> // remove this line if this files includes users.php
<?php include(ROOT_PATH . "/app/controlers/users.php"); ?>
推荐阅读
- powershell - PowerShell Get-PnPListItem 不返回所有字段
- jenkins - 我在哪里可以获得 Jenkins 参数化构建中所有内置参数的列表?
- c# - 检查远程firebird备份文件是否存在
- python - 如何从 JSON 列表创建 padas.DataFrame
- javascript - 从我的自定义应用程序调用 azure search api 时,在 Microsoft Edge 浏览器中出现如下所示的 CORS 错误
- reactjs - React Native Swipeout 性能问题
- python - 在 pandas 中转置数据,然后输出到 CSV
- python - 如何在python中运行while True循环?
- android - Firebase ListView 未实时更新
- wordpress - 无法使用 .htaccess 屏蔽 WordPress 页面 URL