Mehmet Eren Yener
Mehmet Eren Yener

Reputation: 2036

Calling Php Function With Ajax Request to change session variable

I have a communication problem between home.php page and user.php page.

on Homepage there is link for Log out

<span class="log_out"> <a id="logOut">Log Out</a></span>

When a user click this page ajax call will be started

Here is my ajax call

<script>
    $( document ).ready(function() {
        $( "#logOut" ).click(function() {
        $.ajax({
         url: 'class/user.php',
         data: "logout=1",
         success: function(data) {
            $('body').append(data);
         }
         });
        });
});

in user.php I have this

<?php
    if(isset($_GET['logout'])){
        echo "alert";
        $_SESSION['user'] = 0;
    }
?>

When I click logout, alert is being appended in body, but session variable was not changed at all.

I dont know what's going on here.

Upvotes: 0

Views: 723

Answers (2)

Mehmet Eren Yener
Mehmet Eren Yener

Reputation: 2036

if(isset($_GET['logout'])){
    if(!isset($_SESSION)) 
    { 
        session_start(); 
    } 
    $_SESSION['user'] = 0;
    Print_r ($_SESSION);

}

I found solution thanks for helping guys, I just need to add session validation on my if else clause, although I have this validation on top of the page, when I added it inside the function, problem fixed

Upvotes: 1

Ryan
Ryan

Reputation: 1338

You need to add session_start(); to the top of your user.php file and also debug with the echo after a session is set, otherwise you'll get the warning you're getting at the moment.

Upvotes: 1

Related Questions