1

I am currently working on a URL, which encodes the URL entered by URL variable. The user is then redirected to http://example.net/?enc_url=THE ENCODED LINK.

This is my previous code:

<!DOCTYPE html>
<html>
    <script type="text/javascript">
        setTimeout(function() {
            var uri = <?php echo $_GET["url"]; ?>
            var res = encodeURI(uri)
            location.href = 'http://example.com"/?enc_url={$res}'
        }
    </script>
</html>

Please help me, but remember that I do not have much experience with PHP yet. Thanks for any help!

Greetings!

iehrlich
  • 3,572
  • 4
  • 34
  • 43
Tobias
  • 49
  • 1
  • 8

2 Answers2

0

You're trying to use PHP syntax in Javascript. In Javascript use concatenation.

var res = encodeURIComponent(uri);
location.href = 'http://example.com/?enc_url=' + res;

You're also missing the closing ) for the setTimeout() call. And you should be using encodeURIComponent, not encodeURI; see Should I use encodeURI or encodeURIComponent for encoding URLs?

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Only PHP:

<?php
$enc_url = urlencode($_GET['url']);
header("Location: http://example.com/?enc_url=$enc_url");
?>

JavaScript:

<!DOCTYPE html>
<html>
<script type="text/javascript">
setTimeout(function() {
    var uri = "<?php echo urlencode($_GET["url"]); ?>";
    location.href = `http://example.com/?enc_url=${uri}`;
}, 3000);
</script>
</html>
Martin Schneider
  • 14,263
  • 7
  • 55
  • 58