PHP Classes

File: index.php

Recommend this page to a friend!
  Packages of Leandro Antonello   Request   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Example of usage
Class: Request
Retrieve request values assuming defaults
Author: By
Last change:
Date: 15 years ago
Size: 2,954 bytes
 

 

Contents

Class file image Download
<?php
require_once('request.class.php');
?>
<html>
    <body>
        <h3>Request Class Example</h3>
        <pre>
        <?php
        // Check if the method is POST
        if( Request::Method() == 'POST' )
        {
            echo 'POST Method <br />';
            
            // Get posted vars and print them out.
            echo 'The name is <b>', Request::post('name'), '</b><br />';

            // Is possible to defines a default value for a variable.
            $age = Request::post('age', 30);
            echo 'The age is <b>', $age, '</b><br />';
            
            // Vars can be write down as is. There are a getAsString() method too.
            echo 'Raw data: <b>', Request::postAsString(), '</b><br />';
            
            // We can define the POST OR GET data. There are a setGet() method too.
            Request::setPost("name", "Antonello");
            Request::setPost("age", 37);
            echo 'Redefined data: <b>', Request::postAsString(), '</b><br />';
            
            // The Request class automatically clean the vars.
            echo 'SQL Injection: <b>', Request::post("sql"), '</b><br />';
        }
        else
        {
            echo 'GET Method <br />';
            
            // GET variables
            $name = Request::get("name");
            $age  = Request::get("age");
            
            if( !empty($name) )
                echo 'The name is <b>', $name, '</b><br />';
            
            if( !empty($age) )
                echo 'The age is <b>', $age, '</b><br />';
        }
        
        // Some times we need pass variables in diferent methods.
        // So we have the method that search var in GET and POST methods
        echo '<br />';
        echo 'In this case, the variable <b>job</b> is passed in an input hidden field:<br />';
        echo 'The job is <b>', Request::any("job"), '</b><br />';
        ?>
        </pre>
        <hr />
        <form action="index.php" method="post">
            <input type="hidden" name="job" value="PHP Developer" />
            <fieldset>
                <legend>This fields will be passed with POST method</legend>
                <label for="iname">Name: </label>
                <input id="iname" name="name" type="text" value="" />
                <br />
                <label for="iage">Age: </label>
                <input id="iage" name="age" type="text" value="" />
                <br />
                <label for="isql">SQL injection:</label>
                <input id="isql" name="sql" type="text" value="drop table users;" />
                <br />
                <button type="submit">Send Now!</button>
            </fieldset>
            <fieldset>
                <legend>This link pass variables in QueryString format (GET method)</legend>
                <a href="index.php?name=Leandro&age=37">index.php?name=Leandro&age=37</a>
            </fieldset>
        </form>
    </body>
</html>