Joanna Irungu
Joanna Irungu

Reputation: 21

Working with require_once

I am working with the function require_once but keep getting errors when trying to run the page. Tried looking elsewhere but can't seem to get a precise answer for my problem. My code is:

    <?php
        require_once(ROOT_DIR . 'Pages/Page.php'); 
        require_once(ROOT_DIR . 'lib/Application/Authentication/namespace.php');

It gives me the following errors:

Notice: Use of undefined constant ROOT_DIR - assumed 'ROOT_DIR' in /opt/lampp/htdocs/myDiary/Pages/LoginPage.php on line 3

Warning: require_once(ROOT_DIRPages/Page.php) [function.require-once]: failed to open stream: No such file or directory in /opt/lampp/htdocs/myDiary/Pages/LoginPage.php on line 3

Fatal error: require_once() [function.require]: Failed opening required 'ROOT_DIRPages/Page.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/myDiary/Pages/LoginPage.php on line 3.

Please explain what my problem is, I haven't been programming for very long.

Upvotes: 1

Views: 1996

Answers (6)

Sirko
Sirko

Reputation: 74086

It is all clearly stated in the error messages:

  • Your constant ROOT_DIR is not defined at that position in the script,
  • so PHP can't find the respective files,
  • which results in the require_once() call failing.

As a solution make sure to set ROOT_DIR before using it. See define() for this.

Upvotes: 1

Ryhan
Ryhan

Reputation: 437

Think this should work

require_once(__ROOT__.'Pages/Page.php'); 

the useful link is here :

http://php.net/manual/en/function.require-once.php

Upvotes: 0

NullPoiиteя
NullPoiиteя

Reputation: 57322

you are getting this error since you haven't defined the directory

define this by

define('ROOT_DIR', '/path/to/your/scripts/');

Upvotes: 1

Sibiraj PR
Sibiraj PR

Reputation: 1481

Try DOCUMENT_ROOT

<?php 
   $path = $_SERVER['DOCUMENT_ROOT'];
   $path .= 'Pages/Page.php';
  include_once($path);
?>

Upvotes: 0

snaderss
snaderss

Reputation: 139

You should define ROOT_DIR first.. like so:

define('ROOT_DIR', 'path');

Upvotes: 0

Mihai Iorga
Mihai Iorga

Reputation: 39724

You have to define the constant ROOT_DIR on top:

define('ROOT_DIR', '/path/to/your/scripts/');

Upvotes: 1

Related Questions