Link to home
Start Free TrialLog in
Avatar of greenerpastures
greenerpastures

asked on

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.
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

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
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.
Avatar of greenerpastures
greenerpastures

ASKER

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
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.
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.
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/
ASKER CERTIFIED SOLUTION
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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];

?>
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.
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
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?
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.
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
include ('../random/tips.php');
include ('random/tips.php');    Definitely does not work, as well as other combinations like: include ('/random/tips.php');
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
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';
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.
Can you please print the current working directory and post it here?
http://php.net/manual/en/function.getcwd.php
echo getcwd() . "\n";
Results in:
/home/newsell/public_html/agents
And the file you want to include is in
/home/public_html/newsell/random/tips.php  ?
It is in:
/home/newsell/public_html/random/
The file is:
/home/newsell/public_html/random/tips.php
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
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?
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.
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';
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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