2

I tried to define constants in my config.php file. Its primary use is just configuration, as I'm sure you guessed. But it isn't working. here's my config.php file:

<?php
    define("KPEREQUEST", "0", true);
    define("KPERESPONSE", "1", true);

    define("KPEGET", 0, true);
    define("KPEPOST", 1, true);
?>

and here's the result when I include config.php and print the define kPAPost:

KPEPOST: ""

but here's the result if i define the constants in index.php AND i print them from index.php:

KPEPOST: "1"

** EDIT **

heres my index.php file:

<?php
    $title = "API Documents";
    include 'header.php';
    include "config.inc.php";

    echo "KPEPOST: \"" . KPEPOST . "\""; 
?>

<?php include 'footer.php'; ?>

I've seen this done a million times in wordpress, magento, etc. I have no idea why this isn't working and I've done a ton of research to figure it out but I'm at a dead end now. Any help would be appreciated. Why can't I define a constant in a config file and use the define in files that include the config file?

5
  • printing the define: include "config.inc.php"; echo "KPEPOST: \"" . KPEPOST . "\""; Commented Sep 13, 2014 at 22:22
  • 1
    Please show the actual code in the question, not a comment. Commented Sep 13, 2014 at 22:23
  • kPAPost is not defined did you mean KPEPOST Commented Sep 13, 2014 at 22:25
  • right thats what i meant. i showed the files though. thats a direct copy and paste Commented Sep 13, 2014 at 22:27
  • 1
    Please enable full error_reporting, and see what that says; and also use require instead of include. Commented Sep 13, 2014 at 22:32

2 Answers 2

1

It sounds like you just don't have config.php (or that particular copy of config.php) in your include path.

Suggestion

  1. Define a root index.php for your entire web app;
  2. Do this:

    define('BASE_DIR', realpath(__FILE__));
    
  3. Then you can do this:

    require_once BASE_DIR.'config.php';
    

Further reading

Sign up to request clarification or add additional context in comments.

1 Comment

although i used dirname() instead of realpath()
0

another possible solution is to use ./ in front of the filename during the include. i discovered it inadvertently.

define("kPEPost", "1");

and the include:

include("./header");
include("./includes/config.inc.php");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.