Does "include" not carry over defined variables?

The “FX_SCRIPT_NAME” variable is being used to prevent direct calls to scripts that should not be accessed directly. I’ve got the following code:

index.php... define('FX_SCRIPT_NAME', 'index'); require_once("./includes/core.php"); ...

includes/core.phpif(!defined(FX_SCRIPT_NAME)) { fx_die(1005, 'Script identifier missing or invalid.', 'script-name-not-found'); }
…and accessing index.php gives me the custom fx_die error message stating script-name-not-found. Does a define’d variable not get passed to required/included scripts? If not, is there another way to ensure secondary script files like includes/core.php can’t be called directly?

I am surprised that your script doesn’t work… My understanding of define() should have resulted in FX_SCRIPT_NAME ===“index”. Have you echoed the constand before the indlude and then again in the include file? Are you using a PHP version before define() outside of classes was permitted?

You are missing the quotes in core.php

if(!defined(FX_SCRIPT_NAME)) {

Should be:
if(!defined([size=14pt][/size]FX_SCRIPT_NAME[size=14pt][/size])) {

I would still like to know where fx_die comes from

Quotes were the issue, thanks! I guess the server I originally wrote this on had magic quotes or something similar that was hiding this error from me, at least until I moved servers.

fx_die() is just my custom error handler function, writing important errors to a logfile and providing a more user-friendly error message.

Sponsor our Newsletter | Privacy Policy | Terms of Service