Jump to content

Code doesn't crash but gives no result


Go to solution Solved by kicken,

Recommended Posts

I am an amateur php coder at best, yet I muddle through and make stuff work mostly, So this is not crashing, but all I am getting as an answer is the header and my this is not working, what am I doing wrong here? The fact that it is not throwing up an error is already a win, I have checked, $content exists.

 

<?php
function validateURL($url) {
    return filter_var($url, FILTER_VALIDATE_URL);
}

function fetchURLContent($url) {
    $content = @file_get_contents($url);
    return $content !== false ? $content : false;
}
function analyzePage($url) {
    $analysis = [];

    $url = validateURL($url);
    if ($url) {
        $content = fetchURLContent($url);

        if ($content !== false) {
            $analysis['page_size'] = getPageSize($content);
            //$analysis['page_speed'] = getPageSpeed($url);
        }
    }
}

function getPageSize($content) {
    // Example: Calculate and return the page size
    return number_format(strlen($content) / 1024, 2) . ' KB';
}

/*function getPageSpeed($url) {
    // Example: Calculate and return the page load time
    $start_time = microtime(true);
    $content = fetchURLContent($url);
    $end_time = microtime(true);

    $load_time = ($end_time - $start_time) * 1000;
    return number_format($load_time, 2) . ' ms';
}

function getPageRequests($content) {
    // Example: Count and return the number of page requests
    preg_match_all('/<img [^>]*src=["\']([^"\']+)["\']/i', $content, $images);
    return count($images[1]);
}
*/

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $url = $_POST['url'];
    $analysis = analyzePage($url);

    if (isset($analysis['error'])) {
        echo "<p>Error: {$analysis['error']}</p>";
    } else {
        // Output analysis results here...
        echo "<h1>Test</h1>";
        foreach ($analysis as $key => $value) {
            echo "<p>$key: $value</p>";
        }
    }



echo "This shit is working but not";
    }
    else
    {
?>

<h1>Test</h1>
    <form method="post">
        <label for="url">Enter URL: </label>
        <input type="text" name="url" id="url" required>
        <input type="submit" value="Analyze">
    </form>

    <?php
    }
    ?>

 

Link to comment
https://forums.phpfreaks.com/topic/317426-code-doesnt-crash-but-gives-no-result/
Share on other sites

  • Solution

Your analyzePage function is not returning a value, so your $analysis variable will be null.  If you have PHP's error reporting turned up all the way, and are on a modern version of PHP, you should be seeing a warning about not being able to use foreach on a null value.

On an unrelated note:

25 minutes ago, guymclarenza said:
return $content !== false ? $content : false;

This could simply be return $content; There is no need for the check against false there since if it is false, you will just return false.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.