-1

Im getting syntax error when adding php code to javascript. I think I did everything right but cant seem to get past the error. I dont know what im missing. Below is snippet of my code. The script will generate a meta redirect html file with the redirect url encoded in base64.

<?php

$id = $line;
$str = 'https://subdomain.cpanel.com/?id=shared&documentid='.$id.'';

//html file string
$filehtml='<!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <meta name="referrer" content="origin">
          <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
          <title> Redirecting...</title>
    <script>
          (() => {
            const b = '<?php base64_encode($str); ?>';
            setTimeout(function () {
              window.location.href = atob(b);
            }, 0);
          })();
      </script>

      </head>
      <body>

      </body>
      
      </html>';

?>
  • 6
    You've put single quotes and PHP tags inside a PHP string which is a) delimited by single quotes and b) already inside a PHP block. But..don't put your entire HTML document inside a variable, that's just a recipe for these kinds of problems. No idea why you're doing that. – ADyson Sep 28 '21 at 17:20
  • 1
    You're hiding the concrete error message - improve your question by adding it. Likely an error is this line: `const b = '';` But then it's just how to encode a string or the output - as ADyson already commented. – hakre Sep 28 '21 at 17:33
  • If you're learnng PHP, take a look at ways of declaring string variables in this language: https://www.php.net/manual/en/language.types.string.php — it imakes a difference whether quotes are single or double, and then there are these other two ways. – Matvey Andreyev Sep 28 '21 at 17:48

1 Answers1

0

Below code should work.

However, Please consider using a templating engine such as Twig

<?php
    
$id = $line;
$str = 'https://subdomain.cpanel.com/? 
id=shared&documentid='.$id.'';
$encodedStr = base64_encode($str); 

?>


//html file string
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="referrer" content="origin">
    <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
    <title> Redirecting...</title>
<script>
    (() => {
      const b = "<?php echo $encodedStr;?>";
      setTimeout(function () {
        window.location.href = atob(b);
      }, 0);
    })();
</script>

</head>
<body>

</body>

</html>