Link to home
Start Free TrialLog in
Avatar of Chris Andrews
Chris AndrewsFlag for United States of America

asked on

downgrading script to work with pre-4.3 php


I deliver content to other sites by having them put this script on their php enabled site:

<?
//code for the Currency Exchange Calculator
$rqst=$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["PHP_SELF"];
$url="http://www.onyoursite.com/data/cxc.htp?rqst=".$rqst;
$content = @file_get_contents($url);
print $content;
?>

It gets the url the script is on ($rqst), sets up the url, sends the requesting url to me and grabs the content, and prints it.

Some of my users are using pre 4.3 versions of php.  I need this script redesigned for earlier versions of php.  For example, I know that file_get_contents wasn't part of php before 4.3.  I don't know about the server vars.

I don't want it to be an 'include', as, if I understand things correctly, that would allow me to run a script on their site, a security breach for the user.  It should be something that gets the text and prints it onto their page.

A short, efficient code is preferred over long winded scripts.

Thank you,    Chris
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru 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
About $HTTP_SERVER_VARS, them are compatible with versions previous to PHP 4.1
Avatar of waygood
waygood

if (!function_exists('file_get_contents'))
{
    function file_get_contents($filename, $use_include_path = 0)
    {
        $file = @fopen($filename, 'rb', $use_include_path);
        if ($file)
        {
            if ($fsize = @filesize($filename))
            {
                $data = fread($file, $fsize);
            }
            else
            {
                while (!feof($file))
                {
                    $data .= fread($file, 1024);
                }
            }
            fclose($file);
        }
        return $data;
    }
}
Avatar of Chris Andrews

ASKER


Replacing file_get_contents with readfile worked except I also get a number below the content, the number of bytes read I guess.  How can I suppress that?

Waygood, I will know ahead of time that the user doesn't have 4.3 or more, so I don't need the if statement.  Can you boil this down to just the pre - 4.3 part and I will test it as well :)

Chris
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
but using both bits you can give the script to everyone, no need to check if they are pre 4.3
>Replacing file_get_contents with readfile worked except I also get a number below the content, the number of bytes read I
> guess.  How can I suppress that?
I guess you are trying to do something like this:

print (readfile($url));

readfile returns bytes read and your are printing them. Just use:

readfile($url), no need to call print() or echo()

ah, ok Jaime, that works :)

Waygood, I understand, but I want to keep this code as short and simple looking as possible.  But I really do appreciate your help.  In fact, I am going to use part of your answer as well.  The part about using $_SERVER.  I'm glad you pointed that out.

Thank you both,    Chris

In fact, are readfile and file_get_contents compariable efficiency wise, maybe I should be using readfile for more current versions of php as well?
I think readfile could be more efficient because it don't store entire file content in a variable.
file_get_contents could be useful when you have to parse file contents.

Thank you Jaime,

Chris