PHP session not working with jQuery Ajax?

Update, solution: After all this, I found out that I was invoking an old version of my code in an ajax update. 'boardControl.php' instead of 'boardUpdate.php' These are the errors that make programming fun.


I am writing a gomoku browser . I have an ajax instruction that allows a player to play a piece.
$(document).ready(function() {
    $("td").live('click',function(){
        var value = $(this).attr('id');
        $.get('includes/boardControl.php',{play: value, bid: bid});
    });
});

value = board square

bid = board ID

Before creating a user login to identify the player, the server-side php version had a temporary solution. It will rotate the piece state for the squares when pressed, instead of knowing which player creates them.

After creating the login materials, I set the session variable for the player ID. I was hoping to read the session id from php during an ajax request and find out which player they have.

session_start();

...

    $playerId = $_SESSION['char'];
    $Query=("SELECT p1, p2 FROM board WHERE bid=$bid");
    $Result=mysql_query($Query);
    $p1 = mysql_result($Result,0,"p1");
    $p2 = mysql_result($Result,0,"p2");
    $newPiece = 0; //*default no player
    if($playerId == $p1)
        $newPiece = 1;
    if($playerId == $p2)
        $newPiece = 2;

For some reason, when I run the full web application, the chunks are still looping, even after I removed the code to make them loop. In addition, after logging in. If I manually load the php page in the browser, it correctly modifies the database (where it plays only the parts belonging to this player) and displays the correct results.

It seems to me that the session is not portable when used with Ajax. However, Google search queries tell me that sessions work with Ajax.


Update: I am trying to provide additional information.
  • . , , .

  • ajax . firebug's . , (0,1,2).

  • boardUpdate.php , Ajax , - , .

  • firefox.

  • boardUpdate.php ( , ).

  • I double checked that session_start () is in php files and double checked the session id variables.

Hope this additional information helps, I'm running out of ideas on what to tell you. Should I download the full code?


Update 2:

After checking the Ajax response in the fire-bug, I realized that the "play" request does not get the result, and the board does not update until the next "update". I'm still learning this, but I'll post it here to you guys too.

boardUpdate.php Known places: Refresh board (line6) Place Piece (line20) ($ turnCount) (line63)

<?php
session_start();
require '../../omok/dbConnect.php';

    //*** Refresh Board ***
    if(isset($_GET['update']))
    {
        $bid = $_GET['bid'];
        $Query=("SELECT turn FROM board WHERE bid=$bid");
        $Result=mysql_query($Query);
        $turnCount=mysql_result($Result,0,"turn");

        if($_GET['turnCount'] < $turnCount) //** Turn increased
        {
            boardUpdate($turnCount);
        }
    }

    //*** Place Piece ***
    if(isset($_GET['play'])) // turn order? player detect?
    {
        $squareID = $_GET['play'];
        $bid = $_GET['bid'];

        $Query=("SELECT turn, boardstate FROM board WHERE bid=$bid");
        $Result=mysql_query($Query);
        $turnCount=mysql_result($Result,0,"turn");
        $boardState=mysql_result($Result,0,"boardstate");

        $turnCount++;

        $playerId = $_SESSION['char'];
        $Query=("SELECT p1, p2 FROM board WHERE bid=$bid");
        $Result=mysql_query($Query);
        $p1 = mysql_result($Result,0,"p1");
        $p2 = mysql_result($Result,0,"p2");
        $newPiece = 0; //*default no player
        if($playerId == $p1)
            $newPiece = 1;
        if($playerId == $p2)
            $newPiece = 2;

//      if($newPiece != 0)
//      {
            $oldPiece = getBoardSpot($squareID, $bid);
            $oldLetter = $boardState{floor($squareID/3)};
            $slot = $squareID%3;

            //***function updateCode($old, $new, $current, $slot)***
            $newLetter = updateCode($oldPiece, $newPiece, $oldLetter, $slot);
            $newLetter = value2Letter($newLetter);
            $newBoard = substr_replace($boardState, $newLetter, floor($squareID/3), 1);

            //** Update Query for boardstate & turn
            $Query=("UPDATE board SET boardState = '$newBoard', turn = '$turnCount' WHERE bid = '$bid'");
            mysql_query($Query);
//      }
        boardUpdate($turnCount);


    }

    function boardUpdate($turnCount)
    {
            $json = '{"turnCount":"'.$turnCount.'",';           //** turnCount **


            $bid = $_GET['bid'];
            $Query=("SELECT boardstate FROM board WHERE bid='$bid'");
            $Result=mysql_query($Query);
            $Board=mysql_result($Result,0,"boardstate");
            $json.= '"boardState":"'.$Board.'"';            //** boardState **


            $json.= '}';
            echo $json;
    }

    function letter2Value($input)
    {
        if(ord($input) >= 48 && ord($input) <= 57)
            return ord($input) - 48;
        else
            return ord($input) - 87;
    }

    function value2Letter($input)
    {
        if($input >= 10)
            return chr($input += 87);
        else
            return chr($input += 48);
    }


    //*** UPDATE CODE *** updates an letter with a new peice change and returns result letter.
    //***** $old : peice value before update
    //***** $new : peice value after update
    //***** $current : letterValue of code before update.
    //***** $slot : which of the 3 sqaures the change needs to take place in.
    function updateCode($old, $new, $current, $slot)
    {
        if($slot == 0)
        {// echo $current,"+((",$new,"-",$old,")*9)";
            return letter2Value($current)+(($new-$old)*9);
        }
        else if($slot == 1)
        {// echo $current,"+((",$new,"-",$old,")*3)";
            return letter2Value($current)+(($new-$old)*3);
        }
        else //slot == 2
        {// echo $current,"+((",$new,"-",$old,")";
            return letter2Value($current)+($new-$old);
        }
    }//updateCode()


    //**** GETBOARDSPOT *** Returns the peice value at defined location on the board.
    //****** 0 is first sqaure increment +1 in reading order (0-254).
    function getBoardSpot($squareID, $bid)
    {
        $Query=("SELECT boardstate FROM board WHERE bid='$bid'");
        $Result=mysql_query($Query);
        $Board=mysql_result($Result,0,"boardstate");


        if($squareID %3 == 2) //**3rd spot**
        {
            if( letter2Value($Board{floor($squareID/3)} ) % 3 == 0)
                return 0;
            else if( letter2Value($Board{floor($squareID/3)} ) % 3 == 1)
                return 1;
            else
                return 2;
        }
        else if($squareID %3 == 0) //**1st spot**
        {
            if(letter2Value($Board{floor($squareID/3)} ) <= 8)
                return 0;
            else if(letter2Value($Board{floor($squareID/3)} ) >= 18)
                return 2;
            else
                return 1;
        }
        else //**2nd spot**
        {
            return floor(letter2Value($Board{floor($squareID/3)}))/3%3;
        }
    }//end getBoardSpot()


?>


Please help, I would be happy to provide additional information, if necessary. Thanks in advance =)
+3
2

, , , . , session_start , , . var_dump of $_SESSION, , ( a die ). , - . , , ?

Firebug, AJAX, , script , .

, , , , , , session_start . , -, .

, , PHP cookie. , , .

+3

$.get - IE, . $.ajax , false:

$.ajax({
  type: 'GET',
  url: 'includes/boardControl.php',
  cache: false,
  data: {play: value, bid: bid}
});
+3

Source: https://habr.com/ru/post/1720042/


All Articles