The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   how to make PHP be parsed correctly locally once I got Apache and PHP working right? (http://hintsforums.macworld.com/showthread.php?t=23954)

marcnyc 05-21-2004 12:26 PM

I tried with this line (as suggested):
<? include($_SERVER['DOCUMENT_ROOT']."test.inc"); ?>
but I still get these errors:
Warning: main(/Library/WebServer/Documentstest.inc): failed to open stream: No such file or directory in /Users/memoryman/Sites/test/index.php on line 1
Warning: main(): Failed opening '/Library/WebServer/Documentstest.inc' for inclusion (include_path='.:/usr/lib/php') in /Users/memoryman/Sites/test/index.php on line 1

I am aware of the fact that this $DOCUMENT_ROOT thing was changed some time ago but since everything seemed to be working on my site I never took the time to change everything... I will do that sometimes, even though it will take me a while with all the pages... I just went by the old rule "if it ain't broken, don't fix it" ;-)

The fact is right now I just would like to work locally on my mac on some files that DO work remotely. A month ago I had a PC and I had no problem working offline with Apache and PHP... Mac's are supposed to be better, why can't I make it work? I just want a folder to be considered the document root and then I'll throw everything in that folder and that should work, right?

marcnyc 05-21-2004 12:54 PM

Quote:

Originally Posted by frisco
Just curious ... in index.php why not simply write

<?php include("../test.inc"); ?>

works for me.
cheers!

See what I am trying to do is make the files that work remotely work locally so that I can work locally and upload when I am done, simply and effortlessly... If I have to change every single path of all the includes of all the files I am working it becomes a major undertaking...

All I want is work on my HD as if it was my remote server. There's got to be a way! I was doing it on my PC, you guys are not gonna tell me I bought a Mac after 10 years of PC and I can't do this, are you? ;-)

frisco 05-21-2004 01:22 PM

Two Items >>

If in your Sites directory you have a php page with the line:
echo $_SERVER['Document_ROOT'];

it will show you : /Library/WebServer/Documents

Your test.inc would have to be located there, not in your Sites directory.

Like you, I develop and test php on OS X because of its time-saving advantages. You don't have to change the path of includes - just use relative paths as you would for any other file. For testing, be sure that the path in the browser address bar begins with http://127.0.0.1/~you/ ... I have not always succeeded with http://localhost/

On a somewhat different but related topic, .inc files are subject to security risks, and it's better to use a .php extension. See the section "Include Files" here: http://www.developer.com/lang/article.php/922871

I hope you find this helpful.

hayne 05-21-2004 01:24 PM

Quote:

Originally Posted by marcnyc
I tried with this line (as suggested):
<? include($_SERVER['DOCUMENT_ROOT']."test.inc"); ?>
but I still get these errors:
Warning: main(/Library/WebServer/Documentstest.inc): failed to open stream: No such file or directory in /Users/memoryman/Sites/test/index.php on line 1
Warning: main(): Failed opening '/Library/WebServer/Documentstest.inc' for inclusion (include_path='.:/usr/lib/php') in /Users/memoryman/Sites/test/index.php on line 1

You need a slash (/) in your include line. "/test.inc" instead of "test.inc".
This is evident from the error message where it complains about not finding a file called "Documentstest.inc"

georgeocrawford 05-21-2004 04:16 PM

As I wrote:

Quote:

Originally Posted by georgeocrawford
PHP Code:

include(dirname($_SERVER['SCRIPT_FILENAME'])."/test/test.inc"); 



marcnyc 05-22-2004 01:10 PM

Quote:

Originally Posted by frisco
Two Items >>

If in your Sites directory you have a php page with the line:
echo $_SERVER['Document_ROOT'];

it will show you : /Library/WebServer/Documents

Your test.inc would have to be located there, not in your Sites directory.

Like you, I develop and test php on OS X because of its time-saving advantages. You don't have to change the path of includes - just use relative paths as you would for any other file. For testing, be sure that the path in the browser address bar begins with http://127.0.0.1/~you/ ... I have not always succeeded with http://localhost/

On a somewhat different but related topic, .inc files are subject to security risks, and it's better to use a .php extension. See the section "Include Files" here: http://www.developer.com/lang/article.php/922871

I hope you find this helpful.

Thank you for your very explanatory post.
I understand exactly what you are saying but using relative paths would still mean for me to change all my files (which have already been created years ago with absolute $DOCUMENT_ROOT paths) and which, as you understood, is exactly what I am trying to avoid. So I guess the next logical step would be to change the root in fttpd.conf to my user's site folder and then work from there, do you agree or are there any negative implications in that?
By the way the reason I did not use relative paths in the past is that in some instances I remember using include("../this.inc"); would not work, I don't remember when and why...

Also thanks for pointing out that there is a difference between http://localhost/~myusername and http://127.0.0.1/~myusername - I was totally unaware of that, and my browser actually often self-redirects itself to http://myusername.local (without the ~) which seems to be even more convenient but I guess is still not as good as the IP format, right?

Thanks for that other link too, I will check that out when I have some time... Anyway, so far I have used .inc only for text includes and .php for code includes.

hayne 05-22-2004 04:08 PM

Don't neglect the configuration settings you can specify in php.ini
And if you haven't already done so, stop everything and go and read all of the documentation on configuration and includes at www.php.net

marcnyc 05-22-2004 11:58 PM

Ok I am getting crazy over this, can't seem to figure it out...
I have changed the two instances of the document root settings in httpd.conf to Library/WebServer/Documents/Sites I have created a folder called Sites inside the Documents and a simple include now works, except my scripts still won't work... Now I get a call to undefined function error as soon as I start any script with any function (which I repeat are all easy functions that work perfectly when launched remotely on my server)... THERE HAS GOT TO BE A WAY TO MAKE THIS WORK EASILY!!!!!!!!

hayne 05-23-2004 03:17 AM

Ann undefined function error usually means that your includes are incorrect. Try adding in a statement to print out the path that is being included, and then go and look in that file to see if the troublesome function is in that file.

marcnyc 05-23-2004 11:23 AM

Quote:

Originally Posted by georgeocrawford
All PHP distributions are now packaged with Register Globals OFF by default. This means that variables like
PHP Code:

$DOCUMENT_ROOT 

are no longer set by default before a script is run. You need to access them in a more secure way:
PHP Code:

$_SERVER['DOCUMENT_ROOT'


Quote:

Originally Posted by georgeocrawford
The alternatives, if you really don't want to change your scripts at all, are to turn Register Globals on in your php.ini config file, and either serve your site from /Library/WebServer/Documents or change the server root to ~/Sites/ in your Apache config file.

I would recommend altering your scripts, however, to ensure portability in the future.

It seems like the only problem I had left is that Register Globals were OFF on my local PHP and ON on my remote installation, therefore I am going to follow georgeocrawford's suggestion and take the extra step and change all instances of
PHP Code:

$DOCUMENT_ROOT 

to
PHP Code:

$_SERVER['DOCUMENT_ROOT'

in order to ensure portability, which is always a must nowadays.

I want to thank you ALL for taking the time to helping me out on this issue. I do REALLY appreciate your help, time and effort. You are great people with a great heart.

Now that this is solved, I will have to change all these instances of that variable in houndred of files, which brings me to my next question about what GREP softwares are available for Mac OSX to batch this process effectively. In order to keep these forums nice and clean I posted this question in a more appropriate section of the forums and I will post the address here if any of you would be so kind and provide me with any hint you might have about this. Thank you very much once again. Here is the link:
http://forums.macosxhints.com/showth...596#post125596


All times are GMT -5. The time now is 05:46 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.