0

I am trying to make a redirect php script, I want that script to check if the link exist and then redirect the user to the link, if it doesn't exist then it will get the next link and so on, but for some reason is not working, maybe you could give me some help on this.

<?php
$URL = 'http://www.site1.com';
$URL = 'http://www.site2.com';
$URL = 'http://www.site3.com';

$handlerr = curl_init($URL);
curl_setopt($handlerr,  CURLOPT_RETURNTRANSFER, TRUE);
$resp = curl_exec($handlerr);
$ht = curl_getinfo($handlerr, CURLINFO_HTTP_CODE);

if ($ht == '404')
{ echo "Sorry the website is down atm, please come back later!";}
else { header('Location: '. $URL);}
?>

3 Answers 3

2

You are overwriting your $URL variable..

$URL = 'http://www.site1.com';
$URL = 'http://www.site2.com';
$URL = 'http://www.site3.com';

Put these urls in an array and go through it with a for each loop.

Sign up to request clarification or add additional context in comments.

Comments

1

You have a few issues in your code. For 1, your $URL will overwrite itself, resulting in only 1 url in there. It needs to be an array:

array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );

You can get many responses, not just a 404, so you should tell cURL to follow redirects. If the URL was a redirect itself, could get a 301 that redirects to a 200. So we want to follow that.

Try This:

<?php

function curlGet($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ( $httpcode == 200 ) {
        return true;
    }
    return false;
}

$urlArray = array( 'http://www.site1.com', 'http://www.site2.com', 'http://www.site3.com' );

foreach ( $urlArray as $url ) {

    if ( $result = curlGet($url) ) {
        header('Location: ' . $url);
        exit;
    }

}

// if we made it here, we looped through every url
// and none of them worked
echo "No valid URLs found...";

2 Comments

Thank you Wade is it working like a charm, you just made a typo on line 21, it is "{" and not "}", perfect!
typo fixed.. dont have my glasses on :D
0

http://php.net/manual/en/function.file-exists.php#74469

<?php
function url_exists($url) {
    if (!$fp = curl_init($url)) return false;
    return true;
}
?>

This will give you the url exists check.

to check multiple urls though, you need an array:

<?
$url_array = [];
$url_array[] = 'http://www.site1.com';
$url_array[] = 'http://www.site2.com';
$url_array[] = 'http://www.site3.com';

foreach ($url_array as $url) {
    if url_exists($url){
        // do what you need;
        break;
    }
}

?>

PS - this is completely untested, but should theoretically do what you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.