Base64 Not encoding/decoding correctly

I have a website called [Pastebin}(https://paste-bin.us), that is a minimalistic version of the original Pastebin, but I am having trouble pasting code. My server won’t accept it.

I tried encoding it in base64 which worked, and stored the data as base 64, and when I went to decode it through a tool it decoded perfectly, but when I view it at the generated pastebin link it displays like �������Y� ��T��T��ԑTUQT and I can’t figure it out. I has a mostly working version that encoded and decoded perfectly but it wasn’t just displaying the code, but displaying it with the html in it. So if I had <h1>Test</h1> in my code, it would display it as a header and not the plain code. I tried to find this again but it got lose in my revisions. Below I have my current (not working) code that has base64 encoding/decoding/displaying errors, I cant figure out which.

JS

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> window.onload = function() {
document.querySelector('#pasteForm').addEventListener('submit', function(e) {
e.preventDefault();
var textArea = document.querySelector('#textToPaste');
var formData = new FormData(e.target);
var text = formData.get('text').replace(/n/g, '<br>').replace(/^ /gm, ' ').replace(/^t/gm, ' ');
var encodedText = btoa(text); // Encode the text in base64
formData.set('text', '<pre>' + encodedText + '</pre>'); // Use the encoded text
var xhr = new XMLHttpRequest();
xhr.open('POST', 'create.php', true);
xhr.onload = function() {
if (xhr.status === 200) {
var password = formData.get('password');
if (password) {
document.querySelector('#linkBox').value = xhr.responseURL + '?password=' + encodeURIComponent(password);
} else {
document.querySelector('#linkBox').value = xhr.responseURL;
}
document.querySelector('#overlay').style.display = 'block';
document.querySelector('#popup').style.display = 'block';
} else {
console.error('An error occurred');
}
};
xhr.send(formData);
});
document.querySelector('#closeButton').addEventListener('click', function() {
document.querySelector('#overlay').style.display = 'none';
document.querySelector('#popup').style.display = 'none';
});
document.querySelector('#copyButton').addEventListener('click', function() {
var linkBox = document.querySelector('#linkBox');
linkBox.select();
document.execCommand('copy');
var copyButtonIcon = document.querySelector('#copyButton img');
var originalIconSrc = copyButtonIcon.src;
copyButtonIcon.src = 'https://cdn.cirrus.center/main/icons/check.png';
setTimeout(function() {
copyButtonIcon.src = originalIconSrc;
}, 2000);
});
};
</code>
<code> window.onload = function() { document.querySelector('#pasteForm').addEventListener('submit', function(e) { e.preventDefault(); var textArea = document.querySelector('#textToPaste'); var formData = new FormData(e.target); var text = formData.get('text').replace(/n/g, '<br>').replace(/^ /gm, ' ').replace(/^t/gm, ' '); var encodedText = btoa(text); // Encode the text in base64 formData.set('text', '<pre>' + encodedText + '</pre>'); // Use the encoded text var xhr = new XMLHttpRequest(); xhr.open('POST', 'create.php', true); xhr.onload = function() { if (xhr.status === 200) { var password = formData.get('password'); if (password) { document.querySelector('#linkBox').value = xhr.responseURL + '?password=' + encodeURIComponent(password); } else { document.querySelector('#linkBox').value = xhr.responseURL; } document.querySelector('#overlay').style.display = 'block'; document.querySelector('#popup').style.display = 'block'; } else { console.error('An error occurred'); } }; xhr.send(formData); }); document.querySelector('#closeButton').addEventListener('click', function() { document.querySelector('#overlay').style.display = 'none'; document.querySelector('#popup').style.display = 'none'; }); document.querySelector('#copyButton').addEventListener('click', function() { var linkBox = document.querySelector('#linkBox'); linkBox.select(); document.execCommand('copy'); var copyButtonIcon = document.querySelector('#copyButton img'); var originalIconSrc = copyButtonIcon.src; copyButtonIcon.src = 'https://cdn.cirrus.center/main/icons/check.png'; setTimeout(function() { copyButtonIcon.src = originalIconSrc; }, 2000); }); }; </code>
    window.onload = function() {
        document.querySelector('#pasteForm').addEventListener('submit', function(e) {
            e.preventDefault();
            var textArea = document.querySelector('#textToPaste');
            var formData = new FormData(e.target);

            var text = formData.get('text').replace(/n/g, '<br>').replace(/^ /gm, ' ').replace(/^t/gm, ' ');
            var encodedText = btoa(text); // Encode the text in base64
            formData.set('text', '<pre>' + encodedText + '</pre>'); // Use the encoded text

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'create.php', true);

            xhr.onload = function() {
                if (xhr.status === 200) {
                    var password = formData.get('password');
                    if (password) {
                        document.querySelector('#linkBox').value = xhr.responseURL + '?password=' + encodeURIComponent(password);
                    } else {
                        document.querySelector('#linkBox').value = xhr.responseURL;
                    }
                    document.querySelector('#overlay').style.display = 'block';
                    document.querySelector('#popup').style.display = 'block';
                } else {
                    console.error('An error occurred');
                }
            };

            xhr.send(formData);
        });

        document.querySelector('#closeButton').addEventListener('click', function() {
            document.querySelector('#overlay').style.display = 'none';
            document.querySelector('#popup').style.display = 'none';
        });

        document.querySelector('#copyButton').addEventListener('click', function() {
            var linkBox = document.querySelector('#linkBox');
            linkBox.select();
            document.execCommand('copy');

            var copyButtonIcon = document.querySelector('#copyButton img');
            var originalIconSrc = copyButtonIcon.src;
            copyButtonIcon.src = 'https://cdn.cirrus.center/main/icons/check.png';

            setTimeout(function() {
                copyButtonIcon.src = originalIconSrc;
            }, 2000);
        });
    };

PHP

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!file_exists('paste')) {
mkdir('paste');
$redirectIndex = '<?php' . PHP_EOL;
$redirectIndex .= 'header("Location: ..");' . PHP_EOL;
$redirectIndex .= 'exit;' . PHP_EOL;
file_put_contents('paste/index.php', $redirectIndex);
}
$dir = uniqid();
mkdir("paste/$dir");
$decodedText = '';
if (isset($_POST['text']) && $_POST['text'] !== '') {
$decodedText = base64_decode($_POST['text']); // Decode the base64 text
$decodedText = mb_convert_encoding($decodedText, 'UTF-8', 'auto'); // Convert the encoding to UTF-8
}
$index = '<?php' . PHP_EOL;
if (isset($_POST['password']) && $_POST['password'] !== '') {
$index .= 'if (!isset($_GET["password"]) || $_GET["password"] !== "' . $_POST['password'] . '") {' . PHP_EOL;
$index .= ' echo "<h1>Incorrect password</h1>";' . PHP_EOL;
$index .= ' exit;' . PHP_EOL;
$index .= '}' . PHP_EOL;
}
$index .= 'echo <<<EOT' . PHP_EOL; // Use the decoded text
$index .= $decodedText . PHP_EOL;
$index .= 'EOT;' . PHP_EOL;
if (isset($_POST['burn'])) {
$index .= 'file_put_contents(__FILE__, "<h1>This paste has been deleted</h1>");' . PHP_EOL;
$index .= 'exit;' . PHP_EOL;
}
$index .= '?>' . PHP_EOL;
$index .= '<!DOCTYPE html>' . PHP_EOL;
$index .= '<html>' . PHP_EOL;
$index .= '<head>' . PHP_EOL;
$index .= ' <title>' . ($_POST['title'] ?? 'Pastebin') . '</title>' . PHP_EOL;
$index .= '</head>' . PHP_EOL;
$index .= '<body>' . PHP_EOL;
$index .= '' . PHP_EOL;
$index .= '</body>' . PHP_EOL;
$index .= '</html>';
file_put_contents("paste/$dir/index.php", $index);
header("Location: /paste/$dir");
echo "/paste/$dir";
}
</code>
<code><?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!file_exists('paste')) { mkdir('paste'); $redirectIndex = '<?php' . PHP_EOL; $redirectIndex .= 'header("Location: ..");' . PHP_EOL; $redirectIndex .= 'exit;' . PHP_EOL; file_put_contents('paste/index.php', $redirectIndex); } $dir = uniqid(); mkdir("paste/$dir"); $decodedText = ''; if (isset($_POST['text']) && $_POST['text'] !== '') { $decodedText = base64_decode($_POST['text']); // Decode the base64 text $decodedText = mb_convert_encoding($decodedText, 'UTF-8', 'auto'); // Convert the encoding to UTF-8 } $index = '<?php' . PHP_EOL; if (isset($_POST['password']) && $_POST['password'] !== '') { $index .= 'if (!isset($_GET["password"]) || $_GET["password"] !== "' . $_POST['password'] . '") {' . PHP_EOL; $index .= ' echo "<h1>Incorrect password</h1>";' . PHP_EOL; $index .= ' exit;' . PHP_EOL; $index .= '}' . PHP_EOL; } $index .= 'echo <<<EOT' . PHP_EOL; // Use the decoded text $index .= $decodedText . PHP_EOL; $index .= 'EOT;' . PHP_EOL; if (isset($_POST['burn'])) { $index .= 'file_put_contents(__FILE__, "<h1>This paste has been deleted</h1>");' . PHP_EOL; $index .= 'exit;' . PHP_EOL; } $index .= '?>' . PHP_EOL; $index .= '<!DOCTYPE html>' . PHP_EOL; $index .= '<html>' . PHP_EOL; $index .= '<head>' . PHP_EOL; $index .= ' <title>' . ($_POST['title'] ?? 'Pastebin') . '</title>' . PHP_EOL; $index .= '</head>' . PHP_EOL; $index .= '<body>' . PHP_EOL; $index .= '' . PHP_EOL; $index .= '</body>' . PHP_EOL; $index .= '</html>'; file_put_contents("paste/$dir/index.php", $index); header("Location: /paste/$dir"); echo "/paste/$dir"; } </code>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!file_exists('paste')) {
        mkdir('paste');

        $redirectIndex = '<?php' . PHP_EOL;
        $redirectIndex .= 'header("Location: ..");' . PHP_EOL;
        $redirectIndex .= 'exit;' . PHP_EOL;
        file_put_contents('paste/index.php', $redirectIndex);
    }

    $dir = uniqid();
    mkdir("paste/$dir");

    $decodedText = '';
    if (isset($_POST['text']) && $_POST['text'] !== '') {
        $decodedText = base64_decode($_POST['text']); // Decode the base64 text
        $decodedText = mb_convert_encoding($decodedText, 'UTF-8', 'auto'); // Convert the encoding to UTF-8
    }

    $index = '<?php' . PHP_EOL;

    if (isset($_POST['password']) && $_POST['password'] !== '') {
        $index .= 'if (!isset($_GET["password"]) || $_GET["password"] !== "' . $_POST['password'] . '") {' . PHP_EOL;
        $index .= '    echo "<h1>Incorrect password</h1>";' . PHP_EOL;
        $index .= '    exit;' . PHP_EOL;
        $index .= '}' . PHP_EOL;
    }

    $index .= 'echo <<<EOT' . PHP_EOL; // Use the decoded text
    $index .= $decodedText . PHP_EOL;
    $index .= 'EOT;' . PHP_EOL;

    if (isset($_POST['burn'])) {
        $index .= 'file_put_contents(__FILE__, "<h1>This paste has been deleted</h1>");' . PHP_EOL;
        $index .= 'exit;' . PHP_EOL;
    }

    $index .= '?>' . PHP_EOL;

    $index .= '<!DOCTYPE html>' . PHP_EOL;
    $index .= '<html>' . PHP_EOL;
    $index .= '<head>' . PHP_EOL;
    $index .= '    <title>' . ($_POST['title'] ?? 'Pastebin') . '</title>' . PHP_EOL;
    $index .= '</head>' . PHP_EOL;
    $index .= '<body>' . PHP_EOL;
    $index .= '' . PHP_EOL;
    $index .= '</body>' . PHP_EOL;
    $index .= '</html>';

    file_put_contents("paste/$dir/index.php", $index);

    header("Location: /paste/$dir");
    echo "/paste/$dir";
}

New contributor

Caelen Cater is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật