简体   繁体   中英

function doesn't return anything in php

I have a problem with this function, it doesn't return anything for me, I don't know the cause I want to obtaining the xor of the both of string `

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if (array_key_exists("back", $_POST)) {
        header('Location: index.php');
        exit;
    }
}
function xor_this($string) {

// Let's define our key here
 $key = ('ma');

 // Our plaintext/ciphertext
 $text =$string;

 // Our output text
 $outText = '';

 // Iterate through each character
 for($i=0;$i<strlen($text);)
 {
     for($j=0;$j<strlen($key);$j++,$i++)
     {
         $outText .= $text{$i} ^ $key{$j};
         echo 'i='.$i.', '.'j='.$j.', '.$outText{$i}.'<br />'; //for debugging
         echo $outText;
     }
 }  
 return $outText;
}
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form name="confirm" action="index.php" method="POST">

            <table>
                <tr>
                    <td>La chaine a chiffrer :</td>
                    <td><?php echo $_POST['chaine']; ?></td>
                </tr>
                <tr>
                    <td>La cle de meme taille :</td>
                    <td><?php echo $_POST['cle']; ?></td>
                </tr>
                <tr>
                    <td>Le XOR :</td>
                    <td><?php  echo xor_this($_POST['chaine']); ?></td>
                </tr>
            </table>     
            <input type="submit" name="back" value="retour"/>

        </form>

    </body>
</html>
`

do you any idea about this problem, I thank you in advance

--------------------edit--------------------------

I took into account your answers but the same problem : nothing is displayed on the string returned by the function here is the newly code :

    <?php
session_start();
$flag = false;

function xor_this($chaine, $cle) {
 $chiffre = '';
 // Iterate through each character
 for($i=0;$i<strlen($chiffre);)
 {
         $chiffre .= $chaine{$i} ^ $cle{$i};         
         echo $chiffre;     
 }  
 return $chiffre;
}

?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $data = array("chaine" => $_POST["chaine"], "cle" => $_POST["cle"]);
        }
        ?>
        <form name="logon" action="index.php" method="POST" >
            <table>
                <tr>
                    <td>La chaine a chiffrer :</td>
                    <td><input type="text" name="chaine" value="<?php if (!empty($data["chaine"])) echo $data["chaine"]; else echo ""; ?>" ></td>
                </tr>
                <tr>
                    <td>La cle de meme taille :</td>
                    <td><input type="text" name="cle" value="<?php if (!empty($data["cle"])) echo $data["cle"]; else echo ""; ?>" ></td>
                    <td>
                        <?php
                        if ($_SERVER["REQUEST_METHOD"] == "POST") {                            
                                if (strlen($_POST['chaine']) <> strlen($_POST['cle']))
                                    echo "la chaine et la cle sont de taille differente";
                        }
                        ?>
                    </td>
                </tr>
            </table>                                   
            <input type="submit" value="Chiffrer">
            <table>
                <tr>
                    <td>La chaine chiffre : </td>
                    <td>                        
                        <?php
                        if ($_SERVER["REQUEST_METHOD"] == "POST") {                            
                                if (strlen($_POST['chaine']) == strlen($_POST['cle']))
                                    echo bin2hex(xor_this($_POST["chaine"],$_POST["cle"]));
                        }
                        ?>
                    </td>
                </tr>
            </table>
        </form>  
    </body>
</html>

do you have any other idea, thanks

Your code is almost correct. The primary issue is, that all lower-case letters will fall outside the printable range.

You can use bin2hex() to see what the result is in hexadecimal:

bin2hex(xor_this($string));

Considering only lower case letters, the ASCII range is: [97, 122] or in binary:

0110_0001
0111_1010

What this means is, that for one lowecase letter, the result is always going to look like this: 000x_xxxx .

The highest number you could get is the following: 0001_1111 , that's 31 , so all lower case letters will map to nonprintable characters.

A better for loop

You could fix the edge-case as pointed out by Jon when the $text is shorter than $key by introducing an additional condition in the inner loop.

However, consider the following alternative:

$key = str_pad("", strlen($text), "ma");
for($i = 0; $i < strlen($text); $i++) {
    $outText .= $text[i] ^ $key[$i];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM