Slow when trying to save.

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
helpless & lost kid
Forum Newbie
Posts: 5
Joined: Wed Jun 03, 2009 12:17 am

Slow when trying to save.

Post by helpless & lost kid »

Hi, Im currently working in a project which help users to configure their router through a web instead of the normal command prompt. However, while trying to save/overwrite some configuration, the loading bar usually hang after some time. How can I improve on it? below is the whole entire code in the program. pls help. Thanks
<?php
 
ob_start('ob_gzhandler');
 
    session_start();
    $info = $_SESSION['manual'];
    $lala = explode("|", $info);
?>
 
<?php
    $host = "172.16.164.".$lala[2]; //terminal server's ip address
    $port = "23"; //terminal server port number
    $con1 = TRUE;
    set_time_limit(0);
 
    $protocolType = $_POST['protocoltype'];
    $control = $_POST['server'];
    $networkAdd = $_POST['address'];
    $autoSystem = $_POST['as_no'];
    $processID = $_POST['proc_id'];
    $wlidcardMask = $_POST['wc_mask'];
    $areaNo = $_POST['area_num'];
 
    //Display info upon entering the page
    //$fp = fsockopen($host,$port,$errno,$errstr);
    $fp = fsockopen($host,$port,$errno,$errstr,180); //3:36 PM 29/7/2008
 
    if(!$fp)
    {
        $results = "Error: could not open socket connection";
    }
    else
    {
        fputs ($fp, "disconnect");
        fputs ($fp, "\r");
        fputs ($fp, "\r");
        fgets ($fp, 1024);
        fgets ($fp, 1024);
        while($con1)
        {
            for($i=0;$i<6;$i++)
            {
                fputs ($fp, "clear line ".$lala[1]."\n"); //clear the selected device's line number
                fgets ($fp, 1024);
                fputs ($fp, "\r\n");
                fgets ($fp, 1024);
                fgets ($fp, 1024);
            }
 
            fputs ($fp, "connect ".$lala[0]."\n"); //connect to the selected device
           
            //negotiation required to access other devices via terminal server
            fputs($fp,chr(0xFF).chr(0xFD).chr(0x01));
            fputs($fp,chr(0xFF).chr(0xFD).chr(0x03));
            fputs($fp,chr(0xFF).chr(0xFB).chr(0x18));
            fputs($fp,chr(0xFF).chr(0xFB).chr(0x1F));
            fputs($fp,chr(0xFF).chr(0xFA).chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x3C));
            fputs($fp,chr(0xFF).chr(0xF0));
            fputs($fp,chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x41).chr(0x4E).chr(0x53).chr(0x49));
            fputs($fp,chr(0xFF).chr(0xF0));
            //negotiation ends here
            fputs ($fp, "\r\n");
            while(true)
            {
                $xyz = fgets ($fp, 4096);
                //trying to open the selected device (loopback address of the selected device, line number of the selected device)
                if(stristr($xyz,'Trying '.$lala[0].' (192.5.1.1, 20'.$lala[1].')...') || stristr($xyz,'Trying '.$lala[0].' (192.5.1.1, 20'.$lala[1].')...'))
                {
                    $qwe = fgets ($fp, 4096);
                    if(stristr($qwe,'% Connection refused by remote host'))
                    {
                        $con2 = FALSE;
                        break;
                    }
                }
 
                if(stristr($xyz,'Trying '.$lala[0].' (192.5.1.1, 20'.$lala[1].')... Open') || stristr($xyz,'Trying '.$lala[0].' (192.5.1.1, 20'.$lala[1].')... Open'))
                {
                    $con2 = TRUE;
                    break;
                }
            }
            //end of opening an device
           
            if($con2)
            {
                fputs ($fp, "\r\n");
                fgets ($fp, 4096);
                while(true)
                {
                    $tyu = fgets ($fp, 4096);
 
                    if(stristr($tyu,'--------------------------------------'))
                    {
                        fputs ($fp, "\r\n");
                    }
 
                    if(stristr($tyu,'>') || stristr($tyu,'#'))
                    {
                        if(stristr($tyu,'>'))
                        {
                            $routername = explode(">", $tyu);
                        }
                        if(stristr($tyu,'#'))
                        {
                            $routername = explode("#", $tyu);
                        }
                        if(stristr($tyu, $routername[0].'>') || stristr($tyu, $routername[0].'#'))
                        {
                            break;
                        }
                    }
                }
                fputs ($fp, "\r\n");
                $xyz = fgets ($fp, 4096);
                if($control == "enable" || $control == "disable")
                {
                    fputs ($fp, "config t\n");
                    if($protocolType == "RIP")
                    {
                        if($control == "enable" && $networkAdd != "")
                        {
                            fputs ($fp, "router ".$protocolType."\n");
                            fputs ($fp, "network".$networkAdd."\n");
                            $moredetails .= fgets ($fp, 4096);
                            $moredetails .= fgets ($fp, 4096);
                        }
                        elseif($control == "disable" && $networkAdd == "")
                        {
                            fputs ($fp, "no router ".$protocolType."\n");
                            $moredetails .= fgets ($fp, 4096);
                        }
                    }
 
                    //set IGRP || EIGRP || BGP
                    if($protocolType == "IGRP" || $protocolType == "EIGRP" || $protocolType == "BGP")
                    {
                        if($control == "enable" && $networkAdd != "" && $autoSystem != "")
                        {
                            fputs ($fp, "router ".$protocolType."\n".$autoSystem."\n");
                            fputs ($fp, "network ".$networkAdd."\n");
                            $moredetails .= fgets ($fp, 4096);
                            $moredetails .= fgets ($fp, 4096);
                        }
                        elseif($control == "disable" && $autoSystem != "")
                        {
                            fputs ($fp, "no router ".$protocolType."\n".$autoSystem."\n");
                            $moredetails .= fgets ($fp, 4096);
                        }
                    }
 
                    //set OSPF
                    if($protocolType == "OSPF")
                    {
                        if($control == "enable" && $processID != "" && $networkAdd != "" && $wlidcardMask != "" && $areaNo != "")
                        {
                            fputs ($fp, "router ".$protocolType."\n".$processID."\n");
                            fputs ($fp, "network ".$networkAdd."\n".$wlidcardMask." area"." ".$areaNo."\n");
 
                            $moredetails .= fgets ($fp, 4096);
                            $moredetails .= fgets ($fp, 4096);
                        }
                        elseif($control == "disable" && $processID != "")
                        {
                            fputs ($fp, "no router ".$protocolType." ".$processID."\n");
                            $moredetails .= fgets ($fp, 4096);
                        }
                    }
 
                    //exit from config. mode
                    fputs ($fp,"exit\n");
                    fputs ($fp, "\r");
                    if(stristr($zxc,$routername[0].'(config)#'))
                    {
                        fputs ($fp,"exit\n");
                        fputs ($fp, "\r");
                    }
                }
 
                if(stristr($xyz,$routername[0].'>'))
                {
                    fputs ($fp, "en\n");
                    fputs ($fp, "terminal length 0\n");
                    //A value of zero disables pausing between screens of output.
                    fputs ($fp, "sh run\n");
                    //fputs ($fp, "show ip protocols\n"); //1:00 PM 5/8/2008
                }
                else
                {
                    fputs ($fp, "terminal length 0\n");
                    //A value of zero disables pausing between screens of output.
                    fputs ($fp, "sh run\n");
                    //fputs ($fp, "show ip protocols\n");//1:00 PM 5/8/2008
                }
                fputs ($fp, "\r\n");
 
 
                //Gets rid of the big mess
                while(true)
                {
                    $tmpecho = fgets ($fp,1024);
 
                    //Break from the loop
                    if(stristr($tmpecho,'Building configuration...'))
                    {
                        break;
                    }
                }
 
                while(true)
                {
                    $tmpecho = fgets ($fp, 4096);
                    $moredetails .= $tmpecho;
 
                    //break from the loop
                    if(stristr($tmpecho,'end'))
                    {
                        if(stristr($tmpecho,'boot-end-marker') === FALSE)
                        {
                            fputs ($fp, "\r");
                            $moredetails .= fgets ($fp, 4096);
                            $moredetails .= fgets ($fp, 4096);
                            break;
                        }
                    }
                }
                $con1 = FALSE;
                fclose($fp);
            }
            else
            {
                $con1 = TRUE;
            }
        }
    }
?>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Unified Platform Network Deployment System -- CISCO [Router Series 2800]</title>
<link href="../stylesheet/cssStyle.css" rel="stylesheet" type="text/css" />
<!--Retrieve validation scripts from ValidateRouting.js from javascript folder to provide validation-->
<script src="../javascript/ValidateRouting.js"></script>
</head>
 
<body class="loginCSS">
<center>
<table width="800px" cellpadding="0" cellspacing="0" border="0">
    <tr><td><img border="0" src="../assets/images/cisco/cisco2800.gif"></td></tr>
    <tr><td><?php include('menu_cisco2800.html'); ?></td></tr>
 
    <tr><td height="20px"></td></tr>
 
    <tr><td align="left">
        <!--CENTER SECTION-->
        <table width="100%" cellpadding="5" cellspacing="5" border="0">
 
            <!--HEADERS-->
            <tr align="left" valign="top"><td width="70%">
                <strong>R&nbsp;O&nbsp;U&nbsp;T&nbsp;I&nbsp;N&nbsp;G&nbsp;&nbsp;&nbsp;P&nbsp;R&nbsp;O&nbsp;T&nbsp;O&nbsp;C&nbsp;O&nbsp;L&nbsp;S</strong>
                <br /><hr size="1">
            </td>
            <td>
                <strong>U&nbsp;S&nbsp;E&nbsp;R&nbsp;&nbsp;&nbsp;A&nbsp;S&nbsp;S&nbsp;I&nbsp;S&nbsp;T&nbsp;A&nbsp;N&nbsp;C&nbsp;E</strong>
                <br><hr size="1">
            </td></tr>
 
            <!--LEFT COLUMN: DISPLAY INFORMATION & SETTINGS-->
            <tr><td align="center">
                <form name="Password" method="post" action="<?php echo $PHP_SELF; ?>" onSubmit="return Routingcheck();">
                <table width="75%" cellpadding="0" cellspacing="0" border="0">
 
                    <!--Display Box-->
                    <tr align="center"><td colspan="3">
                        <strong>INFORMATION:</strong><br />
                        <textarea cols="90" rows="15" readonly><?php echo $moredetails; ?></textarea>
                    </td></tr>
 
                    <tr><td colspan="3" height="20px"></td></tr>
 
                    <!--SETTINGS FOR CONFIGURATION-->
                    <tr align="center"><td colspan="3">
                        <strong>SETTINGS:</strong><br />
                        <hr size="1">
                    </td></tr>
 
                    <!--set routing protocol type-->
                    <tr>
                        <td width="25%"><strong>Protocols</strong></td>
                        <td><strong>:&nbsp;&nbsp;</strong></td>
                        <td>
                            <table width="100%">
                            <tr align="center">
                                <td width="10%">
                                    <?php if(isset($_GET['protocol'])==true && $_GET['protocol']=="RIP"){ ?>
                                    <input type="radio" name="protocoltype" value="RIP" onClick="location.href='Routing.php?protocol=RIP'" checked>
                                    <?php } else { ?>
                                    <input type="radio" name="protocoltype" value="RIP" onClick="location.href='Routing.php?protocol=RIP'">
                                    <?php } ?>
                                    <br>RIP
                                </td>
                                <td width="10%">
                                    <?php if(isset($_GET['protocol'])==true && $_GET['protocol']=="IGRP"){ ?>
                                    <input type="radio" name="protocoltype" value="IGRP" onClick="location.href='Routing.php?protocol=IGRP'" checked>
                                    <?php } else { ?>
                                    <input type="radio" name="protocoltype" value="IGRP" onClick="location.href='Routing.php?protocol=IGRP'">
                                    <?php } ?>
                                    <br>IGRP
                                </td>
                                <td width="10%">
                                    <?php if(isset($_GET['protocol'])==true && $_GET['protocol']=="EIGRP"){ ?>
                                    <input type="radio" name="protocoltype" value="EIGRP" onClick="location.href='Routing.php?protocol=EIGRP'" checked>
                                    <?php } else { ?>
                                    <input type="radio" name="protocoltype" value="EIGRP" onClick="location.href='Routing.php?protocol=EIGRP'">
                                    <?php } ?>
                                    <br>EIGRP
                                </td>
                                <td width="10%">
                                    <?php if(isset($_GET['protocol'])==true && $_GET['protocol']=="BGP"){ ?>
                                    <input type="radio" name="protocoltype" value="BGP" onClick="location.href='Routing.php?protocol=BGP'" checked>
                                    <?php } else { ?>
                                    <input type="radio" name="protocoltype" value="BGP" onClick="location.href='Routing.php?protocol=BGP'">
                                    <?php } ?>
                                    <br>BGP
                                </td>
                                <td width="10%">
                                    <?php if(isset($_GET['protocol'])==true && $_GET['protocol']=="OSPF"){ ?>
                                    <input type="radio" name="protocoltype" value="OSPF" onClick="location.href='Routing.php?protocol=OSPF'" checked>
                                    <?php } else { ?>
                                    <input type="radio" name="protocoltype" value="OSPF" onClick="location.href='Routing.php?protocol=OSPF'">
                                    <?php } ?>
                                    <br>OSPF
                                </td>
                            </tr>
                            </table>
                        </td>
                    </tr>
 
                    <tr><td colspan="3" height="15px"></td></tr>
 
                    <?php if(isset($_GET['protocol'])==true) { ?>
                    <tr><td colspan="3"><input type="text" style="background: transparent;border:0px;font-family:verdana;font-weight:900;font-size:14pt" name="protocol" value="<?php echo $_GET['protocol']." Configuration"; ?>"></td></tr>
 
                    <?php if($_GET['protocol']=="IGRP" || $_GET['protocol']=="BGP" || $_GET['protocol']=="EIGRP"){ ?>
                    <tr>
                        <td colspan="3">To <b>disable</b> <?php echo $_GET['protocol'] ?>, enter only the Automonous System no.<br></td>
                    </tr>
                    <?php } ?>
 
                    <?php if($_GET['protocol']=="OSPF"){ ?>
                    <tr>
                        <td colspan="3">To <b>disable</b> OSPF, enter only the process ID.<br>
                    </tr>
                    <?php } ?>
 
                    <tr><td colspan="3" height="10px"></td></tr>
 
                    <!--Control-->
                    <tr>
                        <td valign="top" width="25%"><strong>Controls</strong></td>
                        <td valign="top"><strong>:&nbsp;&nbsp;</strong></td>
                        <td valign="top">
                            <input type="radio" name="server" value="enable">&nbsp;Enable
                            &nbsp;&nbsp;&nbsp;
                            <input type="radio" name="server" value="disable">&nbsp;Disable
                        </td>
                    </tr>
 
                    <tr><td colspan="3" height="10px"></td></tr>
 
                    <!--set Network address-->
                    <tr>
                        <td valign="top" width="25%"><strong>Network Address</strong></td>
                        <td valign="top"><strong>:&nbsp;&nbsp;</strong></td>
                        <td valign="top"><input type="text" name="address" size="15"></td>
                    </tr>
 
                    <?php if($_GET['protocol']=="IGRP" || $_GET['protocol']=="BGP" || $_GET['protocol']=="EIGRP"){ ?>
 
                    <!--set Autonomous System No.-->
                    <tr>
                        <td valign="top" width="25%"><strong>Autonomous System No.</strong></td>
                        <td valign="top"><strong>:&nbsp;&nbsp;</strong></td>
                        <td valign="top"><input type="text" name="as_no" size="15"></td>
                    </tr>
 
                    <?php } else if ($_GET['protocol']=="OSPF") { ?>
 
                    <!--set Process ID-->
                    <tr>
                        <td valign="top" width="25%"><strong>Process ID</strong></td>
                        <td valign="top"><strong>:&nbsp;&nbsp;</strong></td>
                        <td valign="top"><input type="text" name="proc_id" size="15"></td>
                    </tr>
 
                    <!--set WildCard Mask-->
                    <tr>
                        <td valign="top" width="25%"><strong>WildCard Mask</strong></td>
                        <td valign="top"><strong>:&nbsp;&nbsp;</strong></td>
                        <td valign="top"><input type="text" name="wc_mask" size="15"></td>
                    </tr>
 
                    <!--set Area No.-->
                    <tr>
                        <td valign="top" width="25%"><strong>Area No.k</strong></td>
                        <td valign="top"><strong>:&nbsp;&nbsp;</strong></td>
                        <td valign="top"><input type="text" name="area_num" size="15"></td>
                    </tr>
                    <?php } } ?>
 
                    <tr><td colspan="3" height="20px"></td></tr>
 
                    <!--submit or cancel current configuration-->
                    <tr align="center"><td colspan="3">
                        <input type="submit" name="submit" id="submit" value="Save" />
                        &nbsp;&nbsp;&nbsp;
                        <input type="reset" name="cancel" id="cancel" value="Cancel" />
                    </td></tr>
                </table>
                </form>
            </td>
 
            <!--RIGHT COLUMN: INSTRUCTIONS-->
            <td align="left" valign="top">
                <?php if(isset($_GET['protocol'])==true) { ?>
                [<strong><?php echo $_GET['protocol']; ?></strong>] :
                <br />
 
                <?php if ($_GET['protocol']=="RIP" || $_GET['protocol']=="IGRP" || $_GET['protocol']=="EIGRP") { ?>
                Configures and specifies a list of networks for routing process.<br><br>
                <u>Network Address :</u><br>
                Assign IP address of the directly connected networks.
                <br /><br/>
                <?php } ?>
 
                <?php if ($_GET['protocol']=="BGP") { ?>
                Configures and specifies a list of networks for routing process.<br><br>
                <u>Network Address:</u><br>
                Assign a network address that BGP will advertise.
                <br /><br/>
 
                <?php } ?>
                <?php if ($_GET['protocol']=="IGRP" || $_GET['protocol']=="EIGRP" || $_GET['protocol']=="BGP") { ?>
                <u>Automonous System No. :</u><br>
                Assign a number that identifies the router to other IGRP routers.
                <br /><br/>
 
                <?php } ?>
                <?php if ($_GET['protocol']=="OSPF") { ?>
                Configures OSPF routing process and defines the interfaces on which OSPF runs and defines the area ID for those interfaces.<br><br>
                <u>Network Address :</u><br>
                Assign IP address.
                <br /><br/>
                <u>Process ID:</u><br>
                Assign any positive integer unique value for each OSPF routing process.
                <br><br>
                <u>WildCard Mask:</u><br>
                Assign an IP address type mask that includes "don't care" bits.
                <br>
                E.g.: 0.0.0.255
                <br><br>
                <u>Area No.:</u><br>
                Specify either a value or a subnet address as the value.
                <br><br>
                Area "0" enables OSPF for all other networks.<br>
                <br>
                <?php } } ?>
            </td>
            </tr>
        </table>
    </td></tr>
 
    <tr><td height="20px"></td></tr>
 
    <!--COPYRIGHT-->
    <tr><td align="center"><?php include ('../include/copyright.html') ?></td></tr>
</table>
</center>
</body>
</html>
 
Last edited by califdon on Tue Jul 14, 2009 7:53 pm, edited 1 time in total.
Post Reply