Getting Alert to work After a redirect on a login page











up vote
0
down vote

favorite












I have a page titled login.php, if a user successfully logs in, then he is redirected to map.php. There is also a hyperlink on login.php to map.php



map.php starts as follows:



 <?php 
session_start();
if ($_SESSION){
?>


then output a bunch of HTML and JAVASCRIPT then...



 <?php } else {
header ('Location: ../login.php');
exit();
$message = "wrong answer";
echo "<script type='text/javascript'>alert('$message');
</script>";
}
?>


If I start on login.php, and then click on the hyperlink to jump to the map without entering a username and password I see that the browser is refreshed and I have redirected myself back to the same page as desired, however how do I get the alert to work? I wish to let the user know that he must be logged in in order to view map.php










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a page titled login.php, if a user successfully logs in, then he is redirected to map.php. There is also a hyperlink on login.php to map.php



    map.php starts as follows:



     <?php 
    session_start();
    if ($_SESSION){
    ?>


    then output a bunch of HTML and JAVASCRIPT then...



     <?php } else {
    header ('Location: ../login.php');
    exit();
    $message = "wrong answer";
    echo "<script type='text/javascript'>alert('$message');
    </script>";
    }
    ?>


    If I start on login.php, and then click on the hyperlink to jump to the map without entering a username and password I see that the browser is refreshed and I have redirected myself back to the same page as desired, however how do I get the alert to work? I wish to let the user know that he must be logged in in order to view map.php










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a page titled login.php, if a user successfully logs in, then he is redirected to map.php. There is also a hyperlink on login.php to map.php



      map.php starts as follows:



       <?php 
      session_start();
      if ($_SESSION){
      ?>


      then output a bunch of HTML and JAVASCRIPT then...



       <?php } else {
      header ('Location: ../login.php');
      exit();
      $message = "wrong answer";
      echo "<script type='text/javascript'>alert('$message');
      </script>";
      }
      ?>


      If I start on login.php, and then click on the hyperlink to jump to the map without entering a username and password I see that the browser is refreshed and I have redirected myself back to the same page as desired, however how do I get the alert to work? I wish to let the user know that he must be logged in in order to view map.php










      share|improve this question















      I have a page titled login.php, if a user successfully logs in, then he is redirected to map.php. There is also a hyperlink on login.php to map.php



      map.php starts as follows:



       <?php 
      session_start();
      if ($_SESSION){
      ?>


      then output a bunch of HTML and JAVASCRIPT then...



       <?php } else {
      header ('Location: ../login.php');
      exit();
      $message = "wrong answer";
      echo "<script type='text/javascript'>alert('$message');
      </script>";
      }
      ?>


      If I start on login.php, and then click on the hyperlink to jump to the map without entering a username and password I see that the browser is refreshed and I have redirected myself back to the same page as desired, however how do I get the alert to work? I wish to let the user know that he must be logged in in order to view map.php







      php session redirect login alert






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 9:32

























      asked Nov 18 at 2:06









      grey krav

      84




      84
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted











          i wish to let the user know that he must be logged in in order to see the map.




          You have mainly two different solutions to be able to handle the redirect.



          Handle the redirect with PHP



          Instead of showing an alert, you can output a normal text and use the refresh header call in combination with the url header call in order to send a header to the client browser that refresh the page after a certain number of seconds.



          <?php
          }
          else
          {
          $message = "wrong answer";
          header('Refresh: 5; url=login.php');
          exit($message);
          }


          Handle the redirect with Javascript



          Just output the alert script that you have already written and then handle the redirect through javascript instead of using header() PHP function.



          <?php
          }
          else
          {
          $message = "wrong answer";
          echo "<script>
          alert('$message');
          window.location.href='login.php';
          </script>";
          exit;
          }




          I personally recommend to handle the redirect through PHP, since it is more elegant as solution and because you will not have to worry about users that have JavaScript disabled in their browser.






          share|improve this answer























          • I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
            – grey krav
            Nov 18 at 9:49












          • just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
            – grey krav
            Nov 18 at 20:17










          • You can set up your <a> tag as <a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
            – gomd
            Nov 19 at 1:37










          • Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
            – grey krav
            Nov 20 at 2:16











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357288%2fgetting-alert-to-work-after-a-redirect-on-a-login-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted











          i wish to let the user know that he must be logged in in order to see the map.




          You have mainly two different solutions to be able to handle the redirect.



          Handle the redirect with PHP



          Instead of showing an alert, you can output a normal text and use the refresh header call in combination with the url header call in order to send a header to the client browser that refresh the page after a certain number of seconds.



          <?php
          }
          else
          {
          $message = "wrong answer";
          header('Refresh: 5; url=login.php');
          exit($message);
          }


          Handle the redirect with Javascript



          Just output the alert script that you have already written and then handle the redirect through javascript instead of using header() PHP function.



          <?php
          }
          else
          {
          $message = "wrong answer";
          echo "<script>
          alert('$message');
          window.location.href='login.php';
          </script>";
          exit;
          }




          I personally recommend to handle the redirect through PHP, since it is more elegant as solution and because you will not have to worry about users that have JavaScript disabled in their browser.






          share|improve this answer























          • I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
            – grey krav
            Nov 18 at 9:49












          • just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
            – grey krav
            Nov 18 at 20:17










          • You can set up your <a> tag as <a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
            – gomd
            Nov 19 at 1:37










          • Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
            – grey krav
            Nov 20 at 2:16















          up vote
          0
          down vote



          accepted











          i wish to let the user know that he must be logged in in order to see the map.




          You have mainly two different solutions to be able to handle the redirect.



          Handle the redirect with PHP



          Instead of showing an alert, you can output a normal text and use the refresh header call in combination with the url header call in order to send a header to the client browser that refresh the page after a certain number of seconds.



          <?php
          }
          else
          {
          $message = "wrong answer";
          header('Refresh: 5; url=login.php');
          exit($message);
          }


          Handle the redirect with Javascript



          Just output the alert script that you have already written and then handle the redirect through javascript instead of using header() PHP function.



          <?php
          }
          else
          {
          $message = "wrong answer";
          echo "<script>
          alert('$message');
          window.location.href='login.php';
          </script>";
          exit;
          }




          I personally recommend to handle the redirect through PHP, since it is more elegant as solution and because you will not have to worry about users that have JavaScript disabled in their browser.






          share|improve this answer























          • I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
            – grey krav
            Nov 18 at 9:49












          • just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
            – grey krav
            Nov 18 at 20:17










          • You can set up your <a> tag as <a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
            – gomd
            Nov 19 at 1:37










          • Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
            – grey krav
            Nov 20 at 2:16













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted







          i wish to let the user know that he must be logged in in order to see the map.




          You have mainly two different solutions to be able to handle the redirect.



          Handle the redirect with PHP



          Instead of showing an alert, you can output a normal text and use the refresh header call in combination with the url header call in order to send a header to the client browser that refresh the page after a certain number of seconds.



          <?php
          }
          else
          {
          $message = "wrong answer";
          header('Refresh: 5; url=login.php');
          exit($message);
          }


          Handle the redirect with Javascript



          Just output the alert script that you have already written and then handle the redirect through javascript instead of using header() PHP function.



          <?php
          }
          else
          {
          $message = "wrong answer";
          echo "<script>
          alert('$message');
          window.location.href='login.php';
          </script>";
          exit;
          }




          I personally recommend to handle the redirect through PHP, since it is more elegant as solution and because you will not have to worry about users that have JavaScript disabled in their browser.






          share|improve this answer















          i wish to let the user know that he must be logged in in order to see the map.




          You have mainly two different solutions to be able to handle the redirect.



          Handle the redirect with PHP



          Instead of showing an alert, you can output a normal text and use the refresh header call in combination with the url header call in order to send a header to the client browser that refresh the page after a certain number of seconds.



          <?php
          }
          else
          {
          $message = "wrong answer";
          header('Refresh: 5; url=login.php');
          exit($message);
          }


          Handle the redirect with Javascript



          Just output the alert script that you have already written and then handle the redirect through javascript instead of using header() PHP function.



          <?php
          }
          else
          {
          $message = "wrong answer";
          echo "<script>
          alert('$message');
          window.location.href='login.php';
          </script>";
          exit;
          }




          I personally recommend to handle the redirect through PHP, since it is more elegant as solution and because you will not have to worry about users that have JavaScript disabled in their browser.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 18 at 2:52

























          answered Nov 18 at 2:39









          gomd

          1237




          1237












          • I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
            – grey krav
            Nov 18 at 9:49












          • just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
            – grey krav
            Nov 18 at 20:17










          • You can set up your <a> tag as <a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
            – gomd
            Nov 19 at 1:37










          • Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
            – grey krav
            Nov 20 at 2:16


















          • I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
            – grey krav
            Nov 18 at 9:49












          • just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
            – grey krav
            Nov 18 at 20:17










          • You can set up your <a> tag as <a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
            – gomd
            Nov 19 at 1:37










          • Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
            – grey krav
            Nov 20 at 2:16
















          I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
          – grey krav
          Nov 18 at 9:49






          I was able to implement both solutions, thank you for that, however the problem I have with them is map.php is just a white screen flash, yet I wish to keep them on the same page (retroactively login.php). I was thinking of sending an alert with fancybox, and then refocusing back to the login form. UX should be that if the link is clicked, and a session variable does not exist, either 1) alert with jscript or 2) output html to the same page so it looks nice. Or am I doing this incorrectly? i.e. its only a link if there is a session else it can follow either of the above rules.
          – grey krav
          Nov 18 at 9:49














          just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
          – grey krav
          Nov 18 at 20:17




          just to clarify that what i would like to do is keep the user on the same page and only make the link into a link if there is a session variable otherwise it will output an alert, so i am guessing i need to do this in the header file itself where the link is located?
          – grey krav
          Nov 18 at 20:17












          You can set up your <a> tag as <a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
          – gomd
          Nov 19 at 1:37




          You can set up your <a> tag as <a <?php echo $_SESSION ? 'href="map.php"' : 'href="javascript:alert('You need to log in.');"' ?>>Map</a>. Using ternery operators, if the session array exists you set the href attribute to redirect to map.php, otherwise set the href attribute to show up a window alert. p.s. There are more efficient methods to check if a user is logged in, search for it.
          – gomd
          Nov 19 at 1:37












          Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
          – grey krav
          Nov 20 at 2:16




          Thank you, your advice was quite helpful I did it the first way you suggested it was easy to implement. I was just curious how to do it the other way. from a completely academic standpoint..
          – grey krav
          Nov 20 at 2:16


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357288%2fgetting-alert-to-work-after-a-redirect-on-a-login-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]