0

I am using php session and it seems the embedded code is being called when I am changing pages. When adding the embedded code it causes my logout function to misbehave, resulting in the logout function acting as if the user is logged out instead of acting like the user is logged in.

Index.php:

<a href="inc/Log_in_out.php"><button>Login/Logout</button></a>
...
var x;

function timedCount() {


    if (isLogged == 1) {
        x = setTimeout(function(){TimeMeOut();}, timeLeft*1000);

     }   
}

function TimeMeOut() {
    //Embeded PHP Code being called
    // Removing this code makes the logout work perfectly fine

    <?php
    //Set session empty
    $_SESSION['user'] = '';

    // remove all session variables
    session_unset(); 

    // destroy the session 
    session_destroy(); 
    ?>

    location.reload();
    alert('You where logged out.');
}

Here is the second page. What should happen when this page is called is that it redirects me to Index.php but instead I am redirected to Login.php. However like mention before when I remove the embedded code this page functions as normal.

Log_in_out.php:

<?php
session_start();
    if(isset($_SESSION['user'])) {
            /* When Already Logged In*/

            //Set Cookie to false
            $_SESSION['user'] = '';

            // remove all session variables
            session_unset(); 

            // destroy the session 
            session_destroy(); 

            //change page
            header("Location: http://cornelis.website/GeoLocationSite/Index.php"); /* Redirect browser */


        } else {
            /* When user is logged out*/
            header("Location:http://cornelis.website/GeoLocationSite/Login.php"); /* Redirect browser */

        }
?>
Lundin
  • 195,001
  • 40
  • 254
  • 396
user40380
  • 76
  • 8
  • Yes I do. I made sure it is ontop of all my php pages. – user40380 Apr 20 '15 at 11:56
  • Oh, you're mixing PHP and JavaScript thinking they work at the same time. They don't. Your PHP, in "index.php", doesn't wait for the JS conditional, the PHP fires right off the bat. – bloodyKnuckles Apr 20 '15 at 12:01
  • PHP runs first, and when it's running the only thing executed is what's between PHP tags. The rest is plain text to be delivered to the client (browser). – bloodyKnuckles Apr 20 '15 at 12:13
  • @bloodyKnuckles, is there a way to make it work then? AJAX perhaps? – user40380 Apr 20 '15 at 12:40
  • 1
    Yes, an XHR object (ajax) is a great way to connect JavaScript and PHP. Another possibility is use JS to simply delete the session cookie. – bloodyKnuckles Apr 20 '15 at 12:44
  • Thnx ofr the help. Just as a added question. Why does it still log me out then at the end when I just wait out the timer? – user40380 Apr 20 '15 at 14:19
  • You mean with the PHP code removed from the "index.php" page, you're still getting logged out? – bloodyKnuckles Apr 20 '15 at 14:30

0 Answers0