0

I need the ability to enter an IP address in a form and then that IP would be used to obtain a status.php page that is running on http://'IP':port/status.php and then display the contents below the form.

I attached the two index.php and status.php files I have. They work outside of zend.

How would I do this with zend 1.12 ?

status.php

<?php

/*error_log(date("U").": ".implode(", ",array_keys(($_REQUEST)))."\n",3,"/tmp/phperror.log");
if(!isset($_REQUEST['ip']))
exit(0);
*/
$ip = $_POST['IP'];


function getIP($ip)
{
    $port = ( $_REQUEST[ 'port' ] != "" ? $_REQUEST[ 'port' ] : '1234' );
    $site='http://'.$ip.':'.$port.'/status.php';
    //foreach( $_REQUEST as $k => $v)
    if ( $_REQUEST[ 'mode' ] != 'json' ) {
        //error_log(date("U").": ".$site." : ".implode($_REQUEST)."\n",3,"/tmp/phperror.log");
        error_log( date( "U" ) . ": " . $site . " : " . implode( ", ", array_keys( ( $_REQUEST ) ) ) . "\n", 3, "/tmp/phperror.log" );
        $file = file_get_contents( $site, false, $context );
        echo $file;
    }
    else {
        if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) {
            $r    = $site . "?d=v";
            $data = array();
            foreach ( $_REQUEST as $key => $value ) {
                $r .= "&$key=$value";
                $data[ $key ] = $value;
            }
            $options = array( 'http' => array(
                'method'  => 'POST',
                'content' => http_build_query( $data )
            ) );
            error_log( date( "U" ) . ": " . $r . "\n", 3, "/tmp/phperror.log" );
            $context = stream_context_create( $options );
            //$file = file_get_contents($r, false, $context);
            $file = file_get_contents( $site, false, $context );
            echo $file;
        }
    }
}

getIP($ip);
?>

index. php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
    <meta charset="UTF-8">
    <meta name=description content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <h1 class="text-center">IP Address</h1>
                <form action="status.php" method="post" role="form" class="form-horizontal">

                    <div class="form-group">
                        <label for="IP" class="col-sm-2 control-label">Enter IP Address</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="IP" id="IP" placeholder="IP Address">
                        </div>
                    </div>


                    <button type="button" class="btn btn-primary" onclick="deleteFile()">Submit</button>
                    <button type="reset" class="btn btn-danger">Clear</button>
                </form>
            </div>
            <div id="section"></div>
        </div>
    </div>

    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Bootstrap JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script>

        /* Deletes the selected file */
        function deleteFile() {
            $.ajax({
                type: "POST",
                url: "status.php",
                data: {
                    IP: $( "#IP" ).val()
                }
            })
                .success(function (data) {
                    $('#section').html(data);
                });
        }
    </script>
</body>

EDIT:

I followed Lucian's steps and I believe I do not have my indexaction on the controller correct.

I pasted status.php there and its erroring.... What do I have wrong

indexcontroller.php

<?php

class IndexController extends Zend_Controller_Action
{

public function init()
{
    /* Initialize action controller here */
}

public function indexAction()
{


/*error_log(date("U").": ".implode(", ",array_keys(($_REQUEST)))."\n",3,"/tmp/phperror.log");
if(!isset($_REQUEST['ip']))
exit(0);
*/
$ip = $_POST['IP'];


function getIP($ip)
{
    $port = ( $_REQUEST[ 'port' ] != "" ? $_REQUEST[ 'port' ] : '1234' );
    $site='http://'.$ip.':'.$port.'/status.php';
    //foreach( $_REQUEST as $k => $v)
    if ( $_REQUEST[ 'mode' ] != 'json' ) {
        //error_log(date("U").": ".$site." : ".implode($_REQUEST)."\n",3,"/tmp/phperror.log");
        error_log( date( "U" ) . ": " . $site . " : " . implode( ", ", array_keys( ( $_REQUEST ) ) ) . "\n", 3, "/tmp/phperror.log" );
        $file = file_get_contents( $site, false, $context );
        echo $file;
    }
    else {
        if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) {
            $r    = $site . "?d=v";
            $data = array();
            foreach ( $_REQUEST as $key => $value ) {
                $r .= "&$key=$value";
                $data[ $key ] = $value;
            }
            $options = array( 'http' => array(
                'method'  => 'POST',
                'content' => http_build_query( $data )
            ) );
            error_log( date( "U" ) . ": " . $r . "\n", 3, "/tmp/phperror.log" );
            $context = stream_context_create( $options );
            //$file = file_get_contents($r, false, $context);
            $file = file_get_contents( $site, false, $context );
            echo $file;
        }
    }
}

getIP($ip);
    ?>

}
3
  • 1
    And whats wrong with Zend? Commented Sep 10, 2015 at 18:01
  • what leads you to think it won't work in zend? Commented Sep 10, 2015 at 19:04
  • I never worked with it before so I do not know what I would add to what and what syntax to use. Commented Sep 10, 2015 at 19:06

1 Answer 1

1

First of all create a layout (layout.phtml) using the html content of your index.php that is static and gets repeated on every page.

Second create a controller and put the php from the status.php inside an action within the controller.

Third create a corresponding view to the controller and echo out everything that is computed in the action of the controller.

Fourth create a Zend Form for the html form...

You could do a lot more, but i would adivse you to start with the basics (Step 1 - 3). For more information on view / controller / action in zend, have a look at:

http://framework.zend.com/manual/1.12/de/zend.controller.action.html

EDIT:

There are some quick start skeletons that can help you to jumpstart your application. Have a look at

https://github.com/RichardKnop/zend-v112-skeleton

You will find the mentioned stuff like "controller","action","view" among the files / folders inside the skeleton, just customize them to your needs.

EDIT 2

You can pass data from the controller to the view using $this->view from the controller. For that checkout this answer on stackoverflow: Zend Framework, passing variables to view

Sign up to request clarification or add additional context in comments.

3 Comments

Can you expound on the 3rd step on how to "echo out everything that is computed" from my example code.
I completed the layout, pasted status.php code into indexaction for indexcontroller.php and setup the form. So far I have an error "( ! ) Parse error: syntax error, unexpected end of file in /var/www/html/test/application/controllers/IndexController.php on line 60" I don't know if this will clear with me "echo'ing out everything in view"
please post a new question for the further problems you have. try to setup a running page using ONLY the skeleton. when everything works without errors, start customizing the controller/view to your needs at a step by step basis, meaning change something & try immediatly if it works. think in small steps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.