Solved

downgrading script to work with pre-4.3 php

Posted on 2004-08-08
11
270 Views
Last Modified: 2012-06-27

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
0
Comment
Question by:St_Aug_Beach_Bum
  • 4
  • 4
  • 3
11 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 400 total points
ID: 11749621
About file_get_contents() have you tried with readfile() ? It read entire file and send directly to standard output.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11749635
About $HTTP_SERVER_VARS, them are compatible with versions previous to PHP 4.1
0
 
LVL 9

Expert Comment

by:waygood
ID: 11749685
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;
    }
}
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11749711

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
0
 
LVL 9

Assisted Solution

by:waygood
waygood earned 100 total points
ID: 11749736
Also because the $HTTP vars are going to be dropped at some point in favour of the $_SERVER stuff then use:-

if(isset($_SERVER))
{
      $rqst=$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
}
else
{
      $rqst=$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["PHP_SELF"];
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:waygood
ID: 11749742
but using both bits you can give the script to everyone, no need to check if they are pre 4.3
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11749746
>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()
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11749834

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
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11749853

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?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11751873
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.
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 11752266

Thank you Jaime,

Chris
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calculate values in an array 18 33
php call to a non-object 3 33
PHP get array item with custom id 4 22
Testing a string condition in PHP 6 5
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now