Can you kill a PHP session through JS?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Can you kill a PHP session through JS?

    I have this function being called when a user clicks on a logout link, but i'm unsure if i can kill the current session by calling session_destory (); from JS??
    Code:
    function logout() {
        //kill session here ex. session_destory();
    }
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    you may use an ajax-call to call a php-script that does what you want ...

    kind regards

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      Any easy tutorials on AJAX you'd recommend, never used i before??

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        here you will find a simple example and some explaination

        kind regards

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          Ok, I've got this far now, I'm not sure how to set it so it works on a click of a link?
          Code:
          <script type="text/javascript">
          function logout()
          {
          var xmlHttp;
          try
            {
            // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
            }
          catch (e)
            {
            // Internet Explorer
            try
              {
              xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
              }
            catch (e)
              {
              try
                {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
              catch (e)
                {
                alert("Your browser does not support AJAX!");
                return false;
                }
              }
            }
            xmlHttp.onreadystatechange=function()
              {
              if(xmlHttp.readyState==4)
                {
                document.logout.value=xmlHttp.responseText;
                }
              }
            xmlHttp.open("GET","logout.php",true);
            xmlHttp.send(null);
           }
          </script>
          Code:
          <a href="#" name="logout" onclick="logout(); return false;">Logout</a>

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            doesn't it work ... from a first quick look it should do?

            kind regards

            Comment

            • ziycon
              Contributor
              • Sep 2008
              • 384

              #7
              I've gone over all the code and i've put a pop up message in the logout.php file but it's not showing the pop up when i click logout so it's never getting to the logout.php file?

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                the logout.php is called async ... and you will just get a notification in the onreadystatecha nge-handler of the request, so just echo something in the php-script and alert the responseText, to see that the script is called ...

                Comment

                • ziycon
                  Contributor
                  • Sep 2008
                  • 384

                  #9
                  I tried this but nothing:

                  Code:
                  ...
                  xmlHttp.onreadystatechange=function()
                      {
                          if(xmlHttp.readyState==4)
                          {
                              document.logout.value=xmlHttp.responseText;
                              alert(xmlHttp.responseText);
                          }
                      }
                  ...

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    try the following please:

                    Code:
                    xmlHttp.onreadystatechange = function() {
                        if (xmlHttp.readyState == 4) {
                            alert(xmlHttp.responseText);
                        }
                    }

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5390

                      #11
                      simplified example for firefox:

                      Code:
                      <html>
                      <script type="text/javascript">
                      function logout() {
                          var xmlHttp;
                      
                          xmlHttp = new XMLHttpRequest;
                         
                          xmlHttp.onreadystatechange = function() {
                              if (xmlHttp.readyState == 4) {
                                  alert(xmlHttp.responseText);
                              }
                          }
                         
                          xmlHttp.open("GET", "foo.php", true);
                          xmlHttp.send(null);
                      }
                      </script>
                      <body>
                          <a href="#" onclick="logout(); return false;">foo</a>
                      </body>
                      </html>
                      and foo.php should just echo/print something. then you should get the printed text in the alert box. do you get any errors?

                      Comment

                      • ziycon
                        Contributor
                        • Sep 2008
                        • 384

                        #12
                        Originally posted by gits
                        try the following please:

                        Code:
                        xmlHttp.onreadystatechange = function() {
                            if (xmlHttp.readyState == 4) {
                                alert(xmlHttp.responseText);
                            }
                        }
                        This worked! How do i get it to work without the alert box?

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5390

                          #13
                          yes ... the php-script is called and executed ... and you should know whether it does what it should ... in case you wrote it for yourself? ;)

                          kind regards

                          Comment

                          • ziycon
                            Contributor
                            • Sep 2008
                            • 384

                            #14
                            Ah yes, it's working now in firefox but not in IE??

                            Comment

                            • gits
                              Recognized Expert Moderator Expert
                              • May 2007
                              • 5390

                              #15
                              in case you use your above shown code tho create an instance of the XMLHttpRequest-object it should work. do you get any errors?

                              Comment

                              Working...