Website issues with PHP include after moving to PHP 5.3

Hi,
After changing server and moving to PHP 5.3x from earlier versions any code similar to this one stopped working:
<?php
 include ('http://mysite.com/random/tips.php');
?>

I have several instances with "include" and all stopped working. Thanks for any help.
greenerpasturesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
Set allow_url_include to 1.

And upgrade to at least 5.4. Preferably 5.5 or 5.6, to have long time support.

HTH,
Dan
1
flashwebhostCommented:
Why you need to include remote URL like that ? This is used by most hackers to execute their tools on your server from remote server, that is why it is disabled in latest PHP versions by default. You should consider updating your script to include/require only local files.
0
greenerpasturesAuthor Commented:
I see. I did not know this was disabled. Would you please tell me the proper way to reference the local files? I tried various ways, like:
 include '/public_html/lrandom/tips.php';

None works. Thanks
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

flashwebhostCommented:
Try using

include '/path/to/tips.php';

You can find path by looking into phpinfo() function. Or create a simple PHP file with following content

<?php

echo __DIR__;

Open in new window


Save it as 1.php, upload it to the folder where the tips.php is, then try calling it, it will show full path to the folder.
0
Dave BaldwinFixer of ProblemsCommented:
This...
<?php
 include ('http://mysite.com/random/tips.php');
?>
should never have given you anything but the results of 'tips.php'.  Any time you include the 'http' at the beginning, that's what you should get because that makes it run thru the PHP interpreter.  Another way to get the same thing is 'file_get_contents()'.  http://php.net/manual/en/function.file-get-contents.php

If you want to include the PHP code instead of the results, it should look like this:
<?php
 include ('/random/tips.php');
?>
That will get the PHP code included in your page.
1
Ray PaseurCommented:
Here are the relevant man page references.
http://php.net/manual/en/function.include.php (See the User-Contributed Notes)
http://php.net/manual/en/ini.core.php#ini.include-path
http://php.net/manual/en/language.variables.scope.php
http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-include
http://php.net/manual/en/function.getcwd.php

Going forward, you probably want to move to a currently supported version of PHP.  PHP 5.3 is obsolete and unsupported today, not even for security fixes.  The current versions of PHP are listed on the upper right hand side of the PHP home page: http://php.net/
0
Mark BradyPrincipal Data EngineerCommented:
I think you have the right idea from the experts comments above however just my two cents worth.

If you need that file to be included I would make sure the tips.php file is in the same folder as the file you are including it into. Then I would use require_once in place of include.
If the file can not be found your script will die with a fatal error so it is very easy to debug.

Also, include, include_once, require and require_once are not functions so there is no point in using parenthesis. Write it like this.

require_once 'tips.php';

Your script will look in the same folder as it is in for this file and load it's contents.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
greenerpasturesAuthor Commented:
Thanks for for the help, but so far none of this works

<?php
//echo __DIR__;
 //include ('/random/tips.php');
$tips = file_get_contents('/random/tips.php');
echo $tips;
?>

Here's the contents of the tips.php
<?php

$textfile ="tips.txt";

$items = file("$textfile");

$item = rand(0, sizeof($items)-1);

echo $items[$item];

?>
0
Ray PaseurCommented:
OK, let me see if I can paraphrase.

You have a file named "tips.txt" that contains some lines of information.  You want to read this file into an array with file() and you want to randomly select one of the lines from the file.  You want to echo this line to the browser.

Does that sum it up correctly?  If so, I will show you a tested and working code example.
0
Ray PaseurCommented:
Please see: http://iconoun.com/demo/temp_greenerpastures.php

The file of tips:
GreenerPastures Tip Zero
GreenerPastures Tip One
GreenerPastures Tip Two
GreenerPastures Tip Three

Open in new window


The main script that is linked above:
<?php // demo/temp_greenerpastures.php

/**
 * http://www.experts-exchange.com/questions/28710495/Website-issues-with-PHP-include-after-moving-to-PHP-5-3.html#a40958597
 */
error_reporting(E_ALL);

// BRING IN AND EXECUTE THE SCRIPT
require_once('temp_greenerpastures_random.php');

Open in new window


The script that is included by require_once():
<?php // demo/temp_greenerpastures_random.php

/**
 * http://www.experts-exchange.com/questions/28710495/Website-issues-with-PHP-include-after-moving-to-PHP-5-3.html#a40958597
 */
error_reporting(E_ALL);

// BRING IN THE TIPS FILE AND CHOOSE ONE AT RANDOM
$tips = file('temp_greenerpastures_tips.txt');
$rand = rand(0, count($tips)-1);
echo $tips[$rand];

Open in new window

HTH, ~Ray
0
greenerpasturesAuthor Commented:
This script started working only after I moved the tips.php and tips.txt into the same directory based on Mark's suggestion above, so this original line now works:
include ('tips.php');

I still do not understand why a different directory would not work:
include ('random/tips.php');

This is very inconvenient. Could anyone explain to me how to include a file in a different directory?
0
Ray PaseurCommented:
If this works...

include ('tips.php');

... It would be able to find tips.php in the same current working directory as the script that uses include().

For this to work...

include ('random/tips.php');

... you would want to verify that random/ is a sub-directory in the current working directory of the script that uses include().  You would also want to verify that it has appropriate permissions.  You can often do this by simply browsing to the script from your web browser address bar.
0
greenerpasturesAuthor Commented:
Ray,
Permissions work. So originally my file was in:
public_html/newsell/random/tips.php

The file that is using the include is at:
public_html/newsell/agents/3.html

PHHP include only works if I place tips.php ALSO in public_html/newsell/agents/
BUT, how do I include the original file: public_html/newsell/random/tips.php
INTO public_html/newsell/agents/3.html
0
Dan CraciunIT ConsultantCommented:
include ('../random/tips.php');
1
greenerpasturesAuthor Commented:
include ('random/tips.php');    Definitely does not work, as well as other combinations like: include ('/random/tips.php');
0
Dan CraciunIT ConsultantCommented:
You need to go back one folder before entering "random".
That's what .. does.

random/tips.php translates to public_html/newsell/agents/random/tips.php
/random/tips.php translates to /random/tips.php
../random/tips.php translates to public_html/newsell/random/tips.php
1
Mark BradyPrincipal Data EngineerCommented:
If the file using the include is an html file it won't work. It needs to be a .pho file. In any case you need to go back one folder in your path like this.

Include '../random/tips.php';
0
greenerpasturesAuthor Commented:
Include '../random/tips.php'; still does not work, as well as Include ('../random/tips.php');
Could there be some sort of security setting in php.ini or anywhere else that prevents files from other directories to be included on my server?
The same issue is apparently happening on another of my sites on the same server.
0
Dan CraciunIT ConsultantCommented:
Can you please print the current working directory and post it here?
http://php.net/manual/en/function.getcwd.php
0
greenerpasturesAuthor Commented:
echo getcwd() . "\n";
Results in:
/home/newsell/public_html/agents
0
Dan CraciunIT ConsultantCommented:
And the file you want to include is in
/home/public_html/newsell/random/tips.php  ?
1
greenerpasturesAuthor Commented:
It is in:
/home/newsell/public_html/random/
The file is:
/home/newsell/public_html/random/tips.php
1
Mark BradyPrincipal Data EngineerCommented:
That path is different than what you posted originally. You posted:

public_html/newsell/random/tips.php

Now you are saying it is in:
public_html/random/tips.php

newswell is missing off the last post
1
Ray PaseurCommented:
Is this on a professionally hosted server?  If so, you might want to ask the hosting company to look into it.  Here is the way I understand the directory structure.
public_html / index.php
|
|__ agents / your script is in here?
|
|__ random / tips.php

Open in new window

If that is correct, and the /random/ directory has the correct permissions, then...

include '../random/tips.php';

...should work.  When you say it "doesn't work" what do you mean?  What are the symptoms?  How are you testing?
0
Ray PaseurCommented:
Also, this appears to be confused:

public_html/newsell/random/tips.php

... is not the same as:

/home/newsell/public_html/random/tips.php

I am guessing that the latter is correct because you're hosting the site on a shared server and your account is "newsell" thus public_html under that account is the web root directory.  Can you confirm that for us please?  Or if my guess is wrong, please clarify, thanks.
0
greenerpasturesAuthor Commented:
Ray, our diagram is correct.Is there a way to construct the entire path using PHP?

like:
include 'SOME_PHP_TO_FULL_PATH/random/tips.php';
0
Ray PaseurCommented:
Hope this helps.  It uses relative path addressing and tests out OK on my server.  To see a random tip, go to this URL:
http://iconoun.com/demo/greenerpastures/agents/

greenerpastures/agents/index.php
<?php // demo/greenerpastures/agents/index.php

/**
 * http://www.experts-exchange.com/questions/28710495/Website-issues-with-PHP-include-after-moving-to-PHP-5-3.html#a40958597
 */
error_reporting(E_ALL);

// BRING IN THE TIPS FILE AND CHOOSE ONE AT RANDOM
$tips = file('../random/tips.txt');
$rand = rand(0, count($tips)-1);
echo $tips[$rand];

Open in new window

greenerpastures/random/tips.txt
GreenerPastures Tip Zero
GreenerPastures Tip One
GreenerPastures Tip Two
GreenerPastures Tip Three

Open in new window


URL paths:
greenerpastures (==> YOUR public_html)
|
|__ agents / index.php
|
|__ random / tips.txt

Open in new window

I could not find any advantage to having a separate tips.php file - it feels like a fifth wheel.  Really all you need is the random selection from the text file.
0
RobOwner (Aidellio)Commented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for elvin66's comment #a40958393

for the following reason:

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Mark Brady (https:#a40958393)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

tagit
Experts-Exchange Cleanup Volunteer
0
Ray PaseurCommented:
A tested and working code example is posted here; should probably be worth at least a few points?
https://www.experts-exchange.com/questions/28710495/Website-issues-with-PHP-include-after-moving-to-PHP-5-3.html#a40959391
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.