Results 1 to 8 of 8

Thread:

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User zombie13's Avatar
    Join Date: Oct:2012
    Location: Bulgaria - Ruse
    Posts: 63

    Thumbs up

    test.php
    PHP Code:

    <?php // Block 1
        
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
            
    $name $_POST['name'];
            
    $pass $_POST['pass'];
            
    $error '';
            if(empty(
    $name)){
                
    $error .= 'Enter a name<br>';
            }
            if(empty(
    $pass)){
                
    $error 'Enter a password<br>';
            }
            if(
    strlen($name) && strlen($pass)){
                
    $error .= '';
            }    
            
            if(
    $error){
                
    header('Location: test.php?status=error');
                exit;
            }else{
                
    header('Location: test.php?status=success');
                exit;
            }
        }
    ?>
    <?php 
    // Block 2
        
    if(isset($_GET['status'])){
            if(
    $_GET['status'] == 'error'){
                echo 
    'Error';
            }elseif(
    $_GET['status'] == 'success'){
                echo 
    'Thank you';
            }
        }
    ?>
    <form action="#" method="post">
        Name: <input type="text" name="name" id="name"><br>
        Pass: <input type="password" name="pass" id="pass"><br>
        <input type="submit" value="Send">

    </form>
    $error . , $error .
    . .

    2 1 , $error .

    .

  2. #2
    Mire-x
    Join Date: Apr:2005
    Location: Sofia
    Posts: 763
    , , - "header()" "echo []". , ( ) , GET ( "?status=error" / "?status=success" "?status=0" / "?status=1", , 0 "Thank you.", 1 "Enter a name." ..). :
    PHP Code:
    <?php
        
    // Status codes
        
    define('STATUS_OK'0);
        
    define('STATUS_EMPTY_NAME'1);
        
    define('STATUS_EMPTY_PASSWORD'2);

        
    // Status messages
        
    $statusMessages = array(
            
    => 'Thank you.',
            
    => 'Enter a name.',
            
    => 'Enter a password.'
            
    );
        
        
    // Sends a 'Location' header and exits
        
    function redirect($statusCode)
        {
            
    header('Location: ' $_SERVER['PHP_SELF'] . '?status=' strval($statusCode));
            exit;
        }
        
        
    // POST (name, password)
        
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            
    $name $_POST['name'];
            
    $pass $_POST['pass'];
            if (empty(
    $name)) {
                
    redirect(STATUS_EMPTY_NAME);
            } elseif (empty(
    $pass)) {
                
    redirect(STATUS_EMPTY_PASSWORD);
            } else {
                
    redirect(STATUS_OK);
            }
        }

        
    // GET (status)
        
    if (isset($_GET['status'])) {
            if (
    array_key_exists($_GET['status'], $statusMessages)) {
                echo 
    $statusMessages[$_GET['status']] . '<br />';
            } else {
                echo 
    'Unknown error.';
            }
        }
    ?>
    (10b) || !(10b)

  3. #3
    Registered User tedych's Avatar
    Join Date: Nov:2003
    Location:
    Posts: 17,654
    Tarvin.
    - , POST , - , ..
    , ( / / GET ) 4- ( ) , , (error, warning, notice ..), ( ). , View- .. , / - race condition ( ).

    - Tarvin ( ), , ( ) .

  4. #4
    ! vbTheKing's Avatar
    Join Date: Sep:2003
    Location:
    Posts: 4,138
    . - .
    ''? * *
    - !
    ...

  5. #5
    Registered User zombie13's Avatar
    Join Date: Oct:2012
    Location: Bulgaria - Ruse
    Posts: 63
    Tarvin , .
    , , 15 , , test.php?status=1&status=3&status=8&stat us=12 test.php?status=error test.php?status=success .
    vbKing

  6. #6
    Mire-x
    Join Date: Apr:2005
    Location: Sofia
    Posts: 763
    "status=1&status=3&status=8" , , $_GET['status']? 1, 3 8?

    , (.. 1 ), , . : 1 = Missing name. 2 = Missing password. 4 = Invalid name. 8 = Invalid password. , "status" . , , , ( 0, , ). , , "status=3" " 1 + 2", "status=12" " 4 + 8".

    : "" . , 1, 2, 4, 8..., "a", "b", "c", "d". "status=ab" "status=cd" "status=3" "status=12" . , , . , "?status=", "?status", URL-. empty($_GET['status']) , , .

    , , GET , , "?status=..." URL-. , , .

    EDIT: , , , .
    PHP Code:
     <?php
        
    // Status codes
        
    define('STATUS_EMPTY_NAME'1);
        
    define('STATUS_EMPTY_PASSWORD'2);
        
    define('STATUS_INVALID_NAME'4);
        
    define('STATUS_INVALID_PASSWORD'8);

        
    // Status messages
        
    $statusMessages = array(
            
    => 'Thank you.',
            
    => 'Enter a name.',
            
    => 'Enter a password.',
            
    => 'Name must be at least 3 characters long.',
            
    => 'Password must be at least 8 characters long.'
            
    );
        
        
    // Sends a 'Location' header and exits
        
    function redirect($statusCode)
        {
            
    header('Location: ' $_SERVER['PHP_SELF'] . '?status=' strval($statusCode));
            exit;
        }
        
        
    // POST (name, password)
        
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            
    $name $_POST['name'];
            
    $pass $_POST['pass'];
            
    $status 0;
            if (empty(
    $name)) {
                
    $status += STATUS_EMPTY_NAME;
            } elseif (
    strlen($name) < 3) {
                
    $status += STATUS_SHORT_NAME;
            }
            if (empty(
    $pass)) {
                
    $status += STATUS_EMPTY_PASSWORD;
            } elseif (
    strlen($pass) < 8) {
                
    $status += STATUS_SHORT_PASSWORD;
            }
            
    redirect($status);
        }

        
    // GET (status)
        
    if (isset($_GET['status'])) {
               
    $status $_GET['status'];
            if (
    is_numeric($status)) {
                if (
    $status == 0) {
                    echo 
    $statusMessages[0];
                } else {
                    foreach(
    $statusMessages as $value => $message) {
                        if (
    $status $value 0) {
                            echo 
    $message '<br />';
                        }
                    }
                }
            } else {
                echo 
    'Unknown error.';
            }
        }
    ?>
    Last edited by Tarvin; 20th March 2013 at 17:46.
    (10b) || !(10b)

Posting Permissions

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

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |