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:\xampp\htdocs\phpblogtut\app\controlers\users.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');
I think you're missing to include path.php in users.php
your users.php file should be
<?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"); ?>
and the register.php file should be
<?php include_once("path.php"); ?> // remove this line if this files includes users.php
<?php include(ROOT_PATH . "/app/controlers/users.php"); ?>
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 includepath.php
.
<?php
include('path.php');
include(ROOT_PATH . "/app/database/db.php");
include(ROOT_PATH . "/app/helpers/validateUser.php");
Could you link the tutorial?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.