tags:

views:

34

answers:

2

So, I have been lumped with trying to fix a php script we have on our remote access page. Originally it would use fopen to if a site was available. If it was the user would be directed to that site, if it was down the next url would be tested and the user redirected there if it was successful etc etc.

Our new shared hosting site does not allow fopen for security reasons so the script has to be ammended. I have tried my best to cobble something together but feel I am missing something, probably quite fundamental.

The script is three simple php files. The first is index.php (this is running on Joomla so I assume this is how it has to be setup) and it looks like this:

<?php
error_reporting(E_ALL);
ini_set('display_errors',TRUE);
$counter = 1;
include ("URLs.php");
include ("functionscript.php");

error_reporting(0);

while ( $counter <= 5 ) {
$webURL = "URL".$counter;
redirectIfValid( ${"URL".$counter} );
$counter = $counter + 1;
}
?>

URLs.php is just a list of the 4 sites that are to be tried in order. functionscript.php is the main meat and veg of the thing I suppose and where I have tried my dab hand at hacking in CURL instead of fopen:

<?
function redirectIfValid($webURL)
{
ini_set("default_socket_timeout","05");
set_time_limit(5);

$ch=curl_init();

curl_setopt ($ch, CURLOPT_URL, $webURL);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch,CURLOPT_VERBOSE,false);

curl_setopt($ch, CURLOPT_TIMEOUT, 5);

$page=curl_exec($ch);

//echo curl_error($ch);

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if($httpcode>=200 && $httpcode<300) return true;

else return false;

if($httpcode==true) {
    header('Location:'.$webURL);
    exit;
        }
       else {
    }
}
?>  

The end result of this scipt running is zip. If I right click and view source on the page that does load it just has a "1, 2, 3" going down the page. I am sure I have missed something dead simple but my programming ability is just shy of rubbish and would be grateful of someone casting an eye over this. I think the curl stuff should be fine but wonder if I am missing some gaping link between the index.php and functionscript.php.

EDIT: So after reading Belindas suggestion below I have changed the code to read this (I have only printed the lines that have changed which were all at the end of the script):

if($httpcode>=200 && $httpcode<300) {

    header('Location:'.$webURL);

    exit;
    }
        else {
        }
}
    ?>

The code still seems to fall over though oddly it is now to be adding a "4" to the previously mentions "1, 2, 3" going down the page when you view the source. I can only view this as a positive! Does anyone possibly have any more gems they can send my way as I must admit I am finding this a real trial by ordeal, though an enjoyable one.

A: 

The header statement won't be reached because you are returning either true or false before it is reached. Instead of returning true redirect on httpcode being between 200 and 300. Not sure if this will solve it but should help.

Belinda
took me awhile to read up on this have got a grasp of what you are driving at now. Will give this a go and post back results. thanks
Tim Alexander
The next thing that I would do would be check the value of $httpcode and see what it is.
Belinda
If it is what you are expecting and you have removed the line suggested by sathia try putting it back in.
Belinda
A: 

Add this, curl will follow the server indications:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

sathia
added this line but am afraid it made no difference to the end result. Thanks for the help all the same
Tim Alexander