0

I'm a php noob, so bear with me...

I have two files here. Both are in the SAME directory

I have a config file:

config.php

<?php
date_default_timezone_set('America/Chicago');

define('APP_NAME',"/control");
define('HTTP_SERVER', 'http://localhost/');
define('SITE_NAME', 'http://localhost/');
define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT'].APP_NAME);
?>

Then I have my index file:

index.php:

<!DOCTYPE html>
<html>
<head>
<?php
require ('config.php');
echo "<script type='text/javascript' src='".DOCUMENT_ROOT."/scripts/jquery.js"'></script>;
?>
.....THE REST OF MY HTML.....

But I get an error that "DOCUMENT_ROOT" is undefined. Why is it not pulling the value from the config.php file?

bagofmilk
  • 1,492
  • 8
  • 34
  • 69

3 Answers3

1

You need to dereference your variable using $:

$DOCUMENT_ROOT

That is assuming it's been properly defined in the server environment.

Nathan
  • 2,093
  • 3
  • 20
  • 33
1

There is a small syntax error at the end of your echo, otherwise it works fine for me.

echo "<script type='text/javascript' src='".DOCUMENT_ROOT."/scripts/jquery.js\"'></script>";
LifeQuery
  • 3,202
  • 1
  • 26
  • 35
1

Change this line: (missing ending quote " after </script> and $ sign in front of DOCUMENT_ROOT)

I tested the following on my server and the script did load, since I do have jquery.js on mine.

echo "<script type='text/javascript' src='".DOCUMENT_ROOT."/scripts/jquery.js"'></script>;

to:

echo "<script type='text/javascript' src='$DOCUMENT_ROOT/scripts/jquery.js'></script>";

or:

echo "<script type='text/javascript' src='".DOCUMENT_ROOT."/scripts/jquery.js'></script>";
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • This helps but now I get "Not allowed to load local resource" – bagofmilk Jan 27 '14 at 22:21
  • Give me a few minutes. @bagofmilk – Funk Forty Niner Jan 27 '14 at 22:24
  • I've been reading somewhere that it has to do with xampp permissions but nobody explains any further than that. – bagofmilk Jan 27 '14 at 22:26
  • Ok. I don't have the capabilities to load everything you're using, however my server did not generate any errors when I removed the `$` in `src='$DOCUMENT_ROOT` can you try replacing `src='$DOCUMENT_ROOT` in my answer with `src='DOCUMENT_ROOT` @bagofmilk - Noticed your other comment. I don't know anything about `xampp` – Funk Forty Niner Jan 27 '14 at 22:26
  • I get this error: ( GET http://localhost/control/DOCUMENT_ROOT/scripts/jquery.js 404 (Not Found) ). my code has this: echo ""; – bagofmilk Jan 27 '14 at 22:29
  • I kind of figured. When doing it the original way as I posted, I got this back in my server `` and I could see the jQuery file in the source when clicking on it in FF 26.0 - This is in using `src='$DOCUMENT_ROOT` @bagofmilk – Funk Forty Niner Jan 27 '14 at 22:30
  • So for some weird reason i changed require ('config.php') to --> require 'config'.php and it worked. I removed the parenthesis HOWEVER, it still gives me an error that it cannot load from local resource. Is this a permissions issue? If so, how do I go about editing this? – bagofmilk Jan 27 '14 at 22:33
  • Maybe you can't use the word `DOCUMENT_ROOT` to define, it could be a reserved method. @bagofmilk - but I'll see what I can do. – Funk Forty Niner Jan 27 '14 at 22:33
  • Also, you may not be able to use `DEFINED` words for JS, but I could be wrong. @bagofmilk – Funk Forty Niner Jan 27 '14 at 22:34
  • Is your desired output to be `/control/scripts/jquery.js` by any chance? @bagofmilk – Funk Forty Niner Jan 27 '14 at 22:42
  • Try this `echo "";` @bagofmilk - that echo'ed my whole server's path. – Funk Forty Niner Jan 27 '14 at 22:48
  • That works But it says it cannot load local resource. So its finding the file, but says it is not allowed to load it. – bagofmilk Jan 27 '14 at 22:49
  • The above echo'ed `` @bagofmilk – Funk Forty Niner Jan 27 '14 at 22:50
  • Ok, well that must surely have something to do with your system's settings. @bagofmilk – Funk Forty Niner Jan 27 '14 at 22:51
  • I changed my windows settings so that all users have full control and still get the error. Its a xampp thing and I cannot find anything anywhere to help – bagofmilk Jan 27 '14 at 22:53
  • I'm Googling as we speak. @bagofmilk If I find something relevant, I'll let you know. – Funk Forty Niner Jan 27 '14 at 22:54
  • And what if you added `public` (depending on the name of your public folder) and change both your `http://localhost/` in your config to `http://localhost/public/`? @bagofmilk – Funk Forty Niner Jan 27 '14 at 23:00
  • A few things that I found was (in your config file) Instead of `http://localhost/` try `http://127.0.0.1` or just `localhost/` @bagofmilk - and any luck with what I also wrote just above? – Funk Forty Niner Jan 27 '14 at 23:21
  • my public folder is "control" and its defining the path just fine, it just will not open the file due to a permission. The path it finds is: `http://localhost/control/scripts/jquery.js` which is correct, so the php code is correct - something to do with the xampp permissions I think. – bagofmilk Jan 27 '14 at 23:21
  • Are all your folders writeable? @bagofmilk For example `777` – Funk Forty Niner Jan 27 '14 at 23:28
  • All users have full control of all folders/files in the C:\ drive – bagofmilk Jan 27 '14 at 23:30
  • These answers on SO may help http://stackoverflow.com/a/9349612/ and http://stackoverflow.com/a/12656964/1415724 - And as for the config settings instead of `http://localhost/` can you try the actual drive path? I.e.: `C:\drive\control\` ? or something to that affect. @bagofmilk – Funk Forty Niner Jan 27 '14 at 23:39
  • I guess my main problem here is that my scripts and styles are not being referenced when the user goes to a php page inside a child folder. If anyone knows a way around this, that would be great. My assumption was that I need to define the root directory - but that turned into xampp not letting me load local files. – bagofmilk Jan 27 '14 at 23:43