函数不会在PHP中返回任何内容

时间:2012-11-12 12:07:54

标签: php string function

我有这个功能的问题,它不会为我返回任何东西,我不知道我想要获得两个字符串的xor的原因`

<?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>
`

你对这个问题有任何想法,我提前谢谢你

--------------------编辑--------------------------

我考虑了你的答案,但同样的问题:函数返回的字符串上没有显示任何内容 这是新代码:

    <?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>

你有任何其他想法,谢谢

1 个答案:

答案 0 :(得分:1)

您的代码几乎是正确的。主要问题是,所有小写字母都将落在可打印范围之外。

您可以使用bin2hex()查看十六进制的结果:

bin2hex(xor_this($string));

仅考虑小写字母,ASCII范围为:[97, 122]或二进制:

0110_0001
0111_1010

这意味着,对于一个小写字母,结果总是如下所示:000x_xxxx

可能获得的最高数字如下:0001_1111,即31,因此所有小写字母将映射到不可打印的字符

更好的循环

当$ text短于$ key时,你可以通过在内循环中引入一个附加条件来修复Jon指出的边缘情况。

但是,请考虑以下替代方案:

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