title: Triggering or preventing a javascript function if $_SESSION['SESS_USER_ID'] isn't set
body: <p>This php function currently redirects the user to another page if they aren't logged in </p>
  
  <pre><code>if(!isset($_SESSION['SESS_USER_ID']) || (trim($_SESSION['SESS_USER_ID']) == '')){
  header("location: login_failed.html");
  exit();
  } 
  </code></pre>
  
  <p>How can I change the function so that instead of redirecting the user it does the following:</p>
  
  <ul>
  <li>Triggers the javascript function: <code>loginFailed();</code></li>
  <li>Prevents specific iframes which are preloaded from launching <em>(these require a user to be logged in, and are currently opening as empty iframes if they're not logged in)</em>. I've included the script for this below. Alternatively, preventing iframes from launching which have a specific class or data-fancybox-type, or those which require <code>!isset($_SESSION['SESS_USER_ID'])</code>.</li>
  </ul>
  
  <p>The functions which I want to trigger/prevent</p>
  
  <pre><code>function loginFailed() {
      $().toastmessage('showToast', {
          text: 'Log In Failed&lt;br/&gt;&lt;br/&gt;Please check your username and password',
          sticky: false,
          position: 'top-center',
          type: 'message',
          closeText: '',
          close: function () { console.log("toast is closed ...") }
      });
  } 
  $(function () {
      $(".fancybox").fancybox({
        preload: true,
      });
      $(".fancybox").eq(0).trigger('click');
  });   
  </code></pre>
  
  <p>I've tried:</p>
  
  <pre><code>echo "&lt;script&gt;loginFailed();&lt;/script&gt;";
  echo "&lt;script type="text/javascript"&gt;loginFailed();&lt;/script&gt;";
  echo "&lt;script type="text/javascript"&gt;loginFailed;&lt;/script&gt;";
  </code></pre>