PHP baseconvert custom user input?

Discussion in 'HTML & Website Design' started by xparasite9, Jul 16, 2008.

  1. #1
    I've been trying to make a base converter that allows a guest to input a custom frombase and tobase, but every time I try to code these values as variables determined by the form, I get an error.
    Is it possible to do what I'm trying to do, or should I settle for just having the frombase and tobase limited to the common options?
     
    xparasite9, Jul 16, 2008 IP
  2. riamathews

    riamathews Peon

    Messages:
    306
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you taking about php base64 encoding and decoding ??

    <?php
    $str = 'This is an encoded string';
    echo base64_encode($str);
    ?>
    The above example will output:
    VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

    To Decode:
    <?php
    $str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
    echo base64_decode($str);
    ?>

    The above example will output:
    This is an encoded string
     
    riamathews, Jul 16, 2008 IP
  3. el_jentel1

    el_jentel1 Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You basically want to make two forms, one to decode and one to encode right? I'm new to PHP but it should be pretty simple to make.

    Create a file called base.php and put this in it

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Encode &amp; Decode</title>
    </head>
    <body>
    Encode:<br />
    <form action="base.php" method="post">
    <input name="encode" size="50" />&nbsp;<input type="submit" value="Submit" />
    </form>
    <br />
    
    Decode:<br />
    <form action="base.php" method="post">
    <input name="decode" size="50" />&nbsp;<input type="submit" value="Submit" />
    </form>
    <br />
    
    <?php 
    $encode = $_POST['encode'];
    $decode = $_POST['decode'];
    
    if ($encode == true) {
       echo base64_encode($encode);
    }
    
    elseif ($decode == true) { 
       echo base64_decode($decode);
    }
    ?>
    
    </body>
    </html>
    Code (markup):
     
    el_jentel1, Jul 17, 2008 IP