guymclarenza Posted November 9, 2023 Share Posted November 9, 2023 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/317426-code-doesnt-crash-but-gives-no-result/ Share on other sites More sharing options...
Solution kicken Posted November 9, 2023 Solution Share Posted November 9, 2023 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317426-code-doesnt-crash-but-gives-no-result/#findComment-1612813 Share on other sites More sharing options...
guymclarenza Posted November 9, 2023 Author Share Posted November 9, 2023 Thank You, now lets see if I can make the next thing work. Quote Link to comment https://forums.phpfreaks.com/topic/317426-code-doesnt-crash-but-gives-no-result/#findComment-1612814 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.