Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Stop A PHP Loop

  1. #1
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Stop A PHP Loop

    Hey,

    I have made a dj panel to put on peoples website and it uses the fopen to check the panels version against the lastest one in a text file on my website. The problem is when my site is down the fopen just keeps looping and pretty much destroys the homepage where the "you need to upgrade" message would appear because it can't reach my servers.... how would I stop the PHP loop and display an error if the server/file can't be found?

    Heres my syntax:
    PHP Code:
    $filename "http://www.quickscriptz.ca/radiodjpanel_update.txt";
    $handle fopen("$filename""r");
    $contents '';
    while (!
    feof($handle)) {
    $contents .= fread($handle8192);
    }
    fclose($handle);
    $_divide explode("&"$contents);
    $_array 0;
    echo 
    "<center>";
    while(
    $_divide[$_array] != '')
    {
    $result mysql_query("SELECT panel_version FROM rp_data") or die(mysql_error());
    while(
    $row mysql_fetch_assoc($result)) {
    $currentversion $row['panel_version'];
    list(
    $_version$_html$_message) = explode("~"$_divide[$_array]);
    if(
    $_version != $currentversion|$_SESSION['rank'] == "Administrator"){
    echo 
    "<blockquote class='exclamation'>A newer version of the Radio DJ Panel is available for download. Click <a href='admin/update.php'>here</a> to update your panel.</blockquote>";
    }
    $_array++;
    echo 
    "</center>"
    Thanks in advance! +Rep for solutions!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 315 Times in 307 Posts
    fopen() returns a FALSE if it fails. You need to test for this and take appropriate action -
    PHP Code:
    if($handle fopen("$filename""r"))
    {
    // code if fopen worked
    } else {
    // code if fopen failed

    Actually, to write bullet-proof, user friendly code (tell them if something failed instead of just displaying a blank or broken page) every function call needs to use logic like this to do something if it worked and something if it did not.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •