PHP user launch with or without AJAX

My colleague and I are having a hard time solving this problem. We have a dedicated online store because we have clients and sub-clients. If the registered user is a sub-client, we want to show some additional html on our page. This works, but if a sub-client logs out and a regular client logs in, the additional html is still displayed, but we don't understand how this is possible. The problem is also that: if the first logged in user is a regular user, then log out, then the subclient connects, the additional html is not displayed.

1.loginck.php

// after the user types their email password, we check if it is a regular user or a sub-user. If a regular user then => $_SESSION['multiklant'] = 0;

else sub-user then => $_SESSION['multiklant'] = 1;

else$_SESSION['multiklant'] = 0; //user not found

2.index.php

if ($_SESSION['multiklant'] == 1) {
   $userid = $_SESSION['userid'];

echo "<div class='col-md-3'>";
echo "<label for='leveradres'>Leveradres*:</label><br/>";
echo "<select id='leveradres' class='form-control'>";
echo "<option value='0'>Selecteer...</option>";

$qry = "SELECT * FROM LEVERADRESSEN WHERE LA_EMAIL = '" . $_SESSION['klemail'] . "'";
$res = mysqli_query($link, $qry);
while ($row = mysqli_fetch_assoc($res)) {
echo "<option value='" . $row['LA_ID'] . "'>" . $row['LA_NAAM'] . "</option>";
}

echo "</select>";
echo "</div>"; 
}

      

3.1 logout click on index.php

$("#logout").click(function () {
    var lgout = $.get("logout.php");
    lgout.done(function (data) {

        $(".show-2").trigger("click");
        $("#logout").addClass("hidden");

    });
});

      

3.2 logout.php

<?php
    session_start();

    $_SESSION = array();
    session_unset();
    session_destroy();
    header("Location:index.php");
    exit();
?>

      

As you can see, we used AJAX here, but without even a problem. If possible we would like to keep the AJAX, but if not, it can be removed. Also a combination where the redirect is not in php but in the javascript part.

Could this be a caching issue? Because if we reload our browser without cache, it works.

We have been searching the internet including this site for 6 hours ...

Code tested in Chrome on MAC and Internet Explorer 11 on Windows makes no difference.

+3


source to share


3 answers


YES!

After a long search and trying some of your suggestions (for which we are grateful, I supported the most useful ones), we found a solution. That was after reading this , it was all clear. Index.php is our only content page. So, yes, we start a session there, but if we change the session variables after the AJAX, index.php doesn't track those changes.

This is why we tried to refresh the page after logging out, so the session variables will be updated. It didn't work. This was until we also refreshed after logging in, but still worked, albeit with some page refresh for users.

So, we put these blocks of code in separate php files and check if necessary with AJAX

This is our solution:

1. When the user logs in:

$("#blockleveradressen").load("leveradressen.php");

var testmultiklant = $.get("testmultiklant.php");

testmultiklant.done(function (data){

    if(data == 3){
        $("#een").removeClass().addClass("col-md-3");
        $("#twee").removeClass().addClass("col-md-3");
        $("#drie").removeClass().addClass("col-md-3");
    }else {
        $("#een").removeClass().addClass("col-md-4");
        $("#twee").removeClass().addClass("col-md-4");
        $("#drie").removeClass().addClass("col-md-4");
    }
});

      

2.leveradressen.php



include "include/session.php";
include "include/MyConnect.php";


if ($_SESSION['multiklant'] == 1) {

    $userid = $_SESSION['userid'];


    echo "<div class='col-md-3'>";
    echo "<label for='leveradres'>Leveradres*:</label><br/>";
    echo "<select id='leveradres' class='form-control'>";
    echo "<option value='0'>Selecteer...</option>";

    $qry = "SELECT * FROM LEVERADRESSEN WHERE LA_EMAIL = '" . $_SESSION['klemail'] . "'";
    $res = mysqli_query($link, $qry);
    while ($row = mysqli_fetch_assoc($res)) {
        echo "<option value='" . $row['LA_ID'] . "'>" . $row['LA_NAAM'] . "</option>";
    }

    echo "</select>";
    echo "</div>";

}

      

3.testmultiklant.php

include "include/session.php";

if ($_SESSION['multiklant'] == 1) {
    echo 3;
} else
{
    echo 4;
}

      

4.index.php

<div id="blockleveradressen">

</div>

<div id="een" class="col-md-4">
    <label for="datepicker">Leveringsdatum*:</label><br/>
    <input type="text" id="datepicker" readonly="readonly"/>
    <input type="hidden" id="datepickerAlt" readonly="readonly" visible="false">
</div>
<div id="twee" class="col-md-4">
    <label for="timepicker">Leveringstijdstip*:</label><br/>
    <input type="text" id="timepicker" class="time"/>
</div>
<div id="drie" class="col-md-4">
    <label for="betaalMethode">Betaalmethode*:</label><br/>
    <select id="betaalMethode" class="form-control">
        <option value="Overschrijving">Overschrijving</option>
        <option value="Visa">Visa</option>
        <option value="Cash">Cash</option>
    </select>
</div>

      

Just need to clean up the code, but it works. Just learned another lesson: single page sites and php (more specific session) are not best friends :-)

0


source


Your logout has the location of the logout header. In other words: is it being fulfilled $.get('logout.php')

?

Why are you doing a redirect server? This way the full page and scripts will be reloaded! And you are waiting for "done". Do it:



  • remove header lines and exit logout.php

After the session is destroyed, the page is ready and the .done callback is executed.

+2


source


Look at the response header after logging out and check "cache-control". I think the problem is in the cache.

+1


source







All Articles