I’m following a tutorial there I get an undefined constant ROOT_PATH. I have a register page that includes a file called path.php that contains the constant ROOT_PATH, below that I have another include that links a file called users like this.
<?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');
}
}
}
now according to him I do not need to link path.php in users because on the register page there I linked path.php goes to the users.php but I still get this message:
Warning: Use of undefined constant ROOT_PATH – assumed ‘ROOT_PATH’ (this will throw an Error in a future version of PHP) in C:xampphtdocsphpblogtutappcontrolersusers.php on line 5
this is 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>
this is path.php :
<?php
define('ROOT_PATH',realpath(dirname('_FILE_')));
define("BASE_URL", 'http://localhost/phpblogtut');
2
Answers
I think you’re missing to include path.php in users.php
your users.php file should be
and the register.php file should be
If you want to use a constant, you have to include the file containing the constant, if it was not declared in the same PHP file.
So, you need to include
path.php
.Could you link the tutorial?