Hello, what im trying to do with this php code is first check if $string1 is equal to $string2, that part works great, when the strings are equal it echos some javascript code. The problem is the cookie check php, If the strings are equal i want it to check if cookie exist, If i does then redirect, if it dont exist then move on to the javascript code, Now when i run the check cookie by itself it works great, but when added to this code it is ignored and dont work, What am i doing wrong here?

<?php
$string1 = $row['title'];
$string2 = ["Game One", "Game Two", "Game Three"];

// Check if any of the titles in $string2 are equal to $string1
foreach ($string2 as $title) {
    if ($title == $string1) {
// Check if the cookie 'myCookie' exists
    if (isset($_COOKIE['myCookie'])) {
        // Send the redirect header
        header('Location: https://www.mywebsite.com');
        echo <<<EOF
<script type="text/javascript">
//Run some javascript code
</script>
EOF;
    }
}
?>

[MOD: added "code" tags]

header doesn't stop script execution, so your code is still going on to output the JavaScript code.

Meanwhile, that's a block of JavaScript code looks like it belongs inside an HTML page. You didn't say what "dont work" means, but I'm guessing you're being told that headers have already been sent (or you've turned off error message reporting). See the paragraph right up near the top of the manual page:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP...

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>

    Yes i had die; where you have exit; but the header was still ignored, i want to run the javascript only if the cookie dont exist.

      Maybe I'm missing something, but seems like you could just handle the redirect case at the top of everything, and then go ahead with the HTML and JavaScript if it wasn't redirected?

      <?php  // absolutely nothing preceding this line
      // whatever populates $row, then...
      $string1 = $row['title'];
      $string2 = ["Game One", "Game Two", "Game Three"];
      if(in_array($string1, $string2) && isset($_COOKIE['myCookie'])) {
          header('Location: https://www.mywebsite.com');
          exit;
      }
      ?><!DOCTYPE html>
      <html>
      <head>
      <title>Title of the document</title>
      <script type="text/javascript">
      //Run some javascript code
      </script>
      </head>
      
      <body>
      The content of the document......
      </body>
      
      </html> 
      

      The original code posted is not valid PHP -- it's missing a closing curly brace.

      sneakyimp I should do what you do. Write only one line. Because all the lines that follow are always ignored.

      Weedpacket Oh nooo! Your responses are always extremely informative and thoughtful. You've taught me so many things with your detailed and generous answers. My answers in recent years have been short because a) you've usually diagnosed the problem, often shrewdly reading user intent between the lines of a poorly worded post, and b) I'm dead inside from the endless parade of bots and script kids wanting people to write code for them.

        mike-345
        I have just copied your code into a test file in eclipse and it produces an"unexpected EOF" error on the last line.
        So check your syntax.

          NogDog
          This works. Maybe there's something here you can use. Note the use of strtolower() to make the comparison case insensitive.

          <html>
          <head>
          <?php
          $string1 = "Game Two";
          $string2 = ["Game One", "Game two", "Game Three"];
          
          // Check if any of the titles in $string2 are equal to $string1
          echo '<script type="text/javascript">'."\n";
          echo 'var lst = "";'."\n";
          foreach ($string2 as $title) {
              
          //echo "//" . $title ."<br />"; if (strtolower($title) == strtolower($string1)) { //echo "//" . $string1 ."<br />"; // Check if the cookie 'myCookie' exists if (isset($_COOKIE['myCookie'])) { // Send the redirect header header('Location: https://www.mywebsite.com'); } else { echo 'lst += "' . $title . '&";'."\n"; } } } echo '</script>'; ?> </head> <body> <script type="text/javascript"> strarray = lst.split("&"); for (let i=0; i<strarray.length-1; i++){ alert(strarray[i]); } </script> </body>

          This is the code that it creates in the browser.

          <html>
          <head>
          <script type="text/javascript">
          var lst = "";
          lst += "Game two&";
          </script>
          </head>
          <body>
          <script type="text/javascript">
          
          strarray = lst.split("&");
          
          for (let i=0; i<strarray.length-1; i++){
          	alert(strarray[i]);
          }
          </script>
          </body>
          
            Write a Reply...