2

Is it possible to use require_once $_SERVER['DOCUMENT_ROOT'] . '/defines.php'; I keep getting an error on it.

I tried require_once ($_SERVER['DOCUMENT_ROOT'] . 'htdocs/comments3/defines.php'); but it still won't work please help me.

im trying to recreate the project of https://www.youtube.com/watch?v=X_fJoLdbVyM&list=PLyKBLKYqadGlmL6QLKCicUp9tQsybUII5 its a comment box using ajax[Javascript jQuery JSON PHP mysql] but on episode 6 i cant follow it coz it starts erroring out for the darn defines.php im using C:\xampp\htdocs\comments for my files path

Katsuragi
  • 21
  • 1
  • 4
  • you can use whatever you want, as long as the path/file you're building actually exists. Since it doesn't work for you, that path doesn't exist. – Marc B Mar 23 '14 at 05:08
  • Please provide error detail also. – Durgesh Chaudhary Mar 23 '14 at 05:11
  • http://stackoverflow.com/questions/14974171/serverdocument-root-path-not-work-hence-create-error?rq=1 – doniyor Mar 23 '14 at 05:19
  • why don't you echo $_SERVER['DOCUMENT_ROOT'] and check the path. – Yalamber Mar 23 '14 at 05:22
  • when i view my doc this is what i get Warning: require_once(C:/xampp/htdocs/ so /htdocs/comments3/defines.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\comment3\index.php on line 1 Fatal error: require_once() [function.require]: Failed opening required 'C:/xampp/htdocs/ so /htdocs/comments3/defines.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\comment3\index.php on line 1 – Katsuragi Mar 23 '14 at 05:28

2 Answers2

1

Following worked for me

require_once ($_SERVER['DOCUMENT_ROOT'] . '/myapp/defines.php');

where myapp is my applications name. So in case it will be

require_once ($_SERVER['DOCUMENT_ROOT'] . '/comments3/defines.php');

$_SERVER['DOCUMENT_ROOT'] points to C:\Xampp\htdocs on windows and similarly on other OS. so you need to give path from there starting with / so /comments3/defines.php and so on.

In case above does not work please share complete path of defines.php

In your case it is a spelling mistake at comments3 instead of comment3

Durgesh Chaudhary
  • 1,075
  • 2
  • 12
  • 31
  • In that case above will work for you ie `require_once ($_SERVER['DOCUMENT_ROOT'] . '/comments3/defines.php');`. – Durgesh Chaudhary Mar 23 '14 at 05:39
  • tried it still giving me Warning: require_once(C:/xampp/htdocs/comments3/defines.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\comment3\index.php on line 1 Fatal error: require_once() [function.require]: Failed opening required 'C:/xampp/htdocs/comments3/defines.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\comment3\index.php on line 1 i dont get this error im not a pro at this please help many thanx for helping i really dont understand this – Katsuragi Mar 23 '14 at 05:45
  • your folder name is comment `s `3 or comment3. because I see comments3 in required and comment3 as index.php location – Durgesh Chaudhary Mar 23 '14 at 05:49
  • ow man it worked dude you the best do you have contact email or facebook or skype when i get any more problems please id like your help – Katsuragi Mar 23 '14 at 05:52
  • I am rarely available on my mail/fb/skype still you can mail me at `me[at]techphernalia[dot]com` and please mark it as answer. – Durgesh Chaudhary Mar 23 '14 at 05:57
  • im getting another error similar to the one i had earlier but this tym its with include this is my code ?php require_once INCLUDES . '/comment3/includes/comment_box.php'; ? – Katsuragi Mar 23 '14 at 05:57
  • i tried using same changes i did earlier as you shown me but im getting the error for Warning: require_once(C:/xampp/htdocs/comments3/defines.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\comment3\index.php on line 1 Fatal error: require_once() [function.require]: Failed opening required 'C:/xampp/htdocs/comments3/defines.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\comment3\index.php on line 1 did u say http://home.techphernalia.com/??? or is this tweeter? – Katsuragi Mar 23 '14 at 06:00
  • sir if i was to upload this to a web host do i jus change the codes to /defines.php?? – Katsuragi Mar 23 '14 at 06:03
  • No it is fine and it will work as you are not hard coding any url. You are just playing safe. :) `me@techphernalia.com` is my email id. Please mark the answer if it solved your problem. – Durgesh Chaudhary Mar 23 '14 at 06:08
  • sir i sent u an email did u recieve it yet? – Katsuragi Mar 23 '14 at 06:15
  • replied. You can get help accordingly. Please mark the answer. – Durgesh Chaudhary Mar 23 '14 at 06:22
  • @Durgesh Chaudhary : I am facing same problem . My working directory is 'httpdocs/index.php' and I want to enter 'httpdocs/login/include/config.php' , I am trying with `require_once ($_SERVER['DOCUMENT_ROOT'] . '/login/include/config.php'); ` but it is not working .What is the exact code? – sanzuu Jun 27 '16 at 07:50
0

use like this

<?php require_once ('defines.php');  ?>
Karthick Kumar
  • 2,349
  • 1
  • 17
  • 30