Error in this PHP code generates null or empty strings

Asked

Viewed 42 times

0

It’s my first time here. I’m having a problem with a code and I wanted help from someone to solve

I took a javascript code and wanted to turn it into PHP, but when calling the function it does not give error, but also does not return anything. And I’ve broken my head trying to see where I went wrong and I can’t....

error_reporting of hosting is disabled. I tried to enable . htaccess or cPanel, (which deduces that there are apparently no errors here) and I got no result.

I know this doesn’t use anything advanced encryption, I just need it to shuffle the String I want and untangle it when trying to repackage the result, as long as everything works within the string specified in the $ref in PHP. Just this isn’t working and I don’t know what else to do. Please help me!

This is the Javascript code

<SCRIPT LANGUAGE="JavaScript">
<!-- 
function encode (OrigString, CipherVal) {

       Ref="0123456789abcdefghijklmnopqrstuvwxyz._~ABCDEFGHIJKLMNOPQRSTUVWXYZ"

        CipherVal = parseInt(CipherVal)

        var Temp=""

        for (Count=0; Count < OrigString.length; Count++) {

                var TempChar = OrigString.substring (Count, Count+1)

                var Conv = cton(TempChar)

                var Cipher=Conv^CipherVal

                Cipher=ntoc(Cipher)

                Temp += Cipher

        }

        return (Temp)

}

function cton (Char) {
        return (Ref.indexOf(Char));

}

function ntoc (Val) {
        return (Ref.substring(Val, Val+1))
}
// -->
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
var EncodedText = encode ("Encode the text", 4);
// -->
</SCRIPT>

Javascript works normally, but I need the same code to work in PHP as well.

This is the same code above that "tried" convert to PHP:

<?php
$ref="0123456789abcdefghijklmnopqrstuvwxyz._~ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function encode ($string) {
 $temp=""; $cipherval = 4;

        for ($i=0; $i < strlen($string); $i++) {
                $tempchar = substr ($string, $i, $i+1);
                $conv = cton($tempchar);
                $cipher=$conv^$cipherval;

                $cipher=ntoc($cipher);

                $temp .= $cipher;
        }
        return ($temp);
}

function cton ($char) { return (strpos($char, $ref)); }

function ntoc ($val) { return (substr($ref, $val, $val+1)); }

?>;

<?php echo "Texto: " . encode("Eu quero codificar esta frase!"); ?>

It was supposed to appear anything, but if I put it in a file, ex: index.php and I run on the server, it only appears below:


Text:

  • I noticed that there are no blanks in the string, but I have tested this code with a string with no bank spaces, and it still didn’t work.

1 answer

1

The function is not working because you put the variable $ref outside the scope of its functions. You can place it within the function encode() or pass it as a parameter.

function encode ($string) {
    $ref="0123456789abcdefghijklmnopqrstuvwxyz._~ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $temp=""; $cipherval = 4;

    for ($i=0; $i < strlen($string); $i++) {
        $tempchar = substr ($string, $i, $i+1);
        $conv = cton($tempchar, $ref);
        $cipher = $conv^$cipherval;

        $cipher = ntoc($cipher, $ref);

        $temp .= $cipher;
    }
    return ($temp);
}

function cton ($char, $ref) { 
    return (strpos($char, $ref)); 
}

function ntoc ($val, $ref) { 
    return (substr($ref, $val, $val+1)); 
}


echo "Texto: " . encode("Eu quero codificar esta frase!");

I declared the variable $ref within the function encode() and passed it as parameter to the functions cton() and ntoc().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.