Problem with PHP code.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scaz182
Forum Newbie
Posts: 1
Joined: Thu Sep 11, 2008 2:30 pm

Problem with PHP code.

Post by scaz182 »

Hi people,

I am having an issue with the following code (this is a cut down demonstration of the issue I am having) You can see it working here http://www.richardscarrott.co.uk/project/test.php...or more accurately not working.

It displays a blank screen with no error messages or anything, can anybody see what I am doing wrong?

Code: Select all

<?php
// include custom function list
require_once ('includes/functions.php');
 
// Create URL (will eventually be taken from form input)
$URL = 'secure.streamlinenet.co.uk/cp/index.php'; // test
 
//Add http:// if URL does not already contain http:// or https://
if (!string_starts_with($URL, "http://") && !string_starts_with($URL, "https://")) {
    // add http:// as default (more likely)
    $URL = 'http://' . $URL;
    }
    
// Get HTML source from URL file
$URL_source = file_get_contents($URL);
 
 
 
// Check whether file_get_contents was succesfull, if not try using the "https://" protocol
if ($URL_source == false) {
    
    // echo statement here to indicate first file_get_contents was unsuccessful
    echo'<p>the first file_get_contents() was unsuccessful using: ' . $URL . '</p>';
    
    // check $URL starts with http:// otehrwise URL may be misspelt (it is not a protocol issue)
    if (string_starts_with($URL, "http://")) {
        // replace  http:// with https://
        $URL = str_replace ("http://", "https://", $URL);
        
        // get HTML source from modified URL file
        $URL_source = file_get_contents($URL);
    }
    else {
        echo '<p>Unable to read your webpage, please check the spelling is correct.</p>';
        echo $URL;
        // exit script;
        exit;
    }
    
}
 
// Check file_get_contents was succesfull, if not try using the "https://" protocol
if ($URL_source !== false) {
    echo '<p>Either the first or the second file_get_contents operation was successful using: ' . $URL . '</p>';
    }
 
 
?>

I believe the out put using the $URL = 'secure.streamlinenet.co.uk/cp/index.php' should be:
the first file_get_contents() was unsuccessful using: http://secure.streamlinenet.co.uk/cp/index.php
Either the first or the second file_get_contents operation was successful using: https://secure.streamlinenet.co.uk/cp/index.php
Any insight would be much appreciated.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Problem with PHP code.

Post by jayshields »

Put

Code: Select all

error_reporting(E_ALL);
at the top of your script and you'll probably get some useful error messages.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Problem with PHP code.

Post by thinsoldier »

Code: Select all

 
<?php
/**
* Checks if a given url/email needs http:// or mailto: added to it
* Requires checkEmailAddress function
*/
function urlHttp($url)
{
    if((checkEmailAddress($url)))
    {
        return 'mailto:'.$url;
    }
    if(!preg_match('|^http://|', $url))
    {
        return 'http://'.$url;
    }
    return $url;
}
 
 
 
/**
* Turns a given url or email address into html anchor (<a></a>)
* @uses urlHttp
*/
function linkUrl($url)
{
    $href = urlHttp($url);
    $format = '<a href="%s">%s</a>';    
    return sprintf($format, $href, $url);
}
 
Warning: I have no idea what I'm talking about.
Post Reply