PHP会话不工作与jQuery阿贾克斯? [英] PHP session not working with JQuery Ajax?

查看:124
本文介绍了PHP会话不工作与jQuery阿贾克斯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新,解决了: 这一切之后我发现我在呼唤在更新阿贾克斯的老版我的code。 boardControl.php'而不是'boardUpdate.php这是种错误,使编程的乐趣。

Update, Solved: After all this I found out that I was calling an old version of my code in the update ajax. 'boardControl.php' instead of 'boardUpdate.php' These are the kinds of mistakes that make programing fun.


我正在写一个浏览器五子棋游戏。我有AJAX语句,它允许玩家扮演的一块。


I'm writing a browser gomoku game. I have the ajax statement that allows the 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});
	});
});

值=板上方位置 - 出价=主板ID

value = board square location
bid = board ID

在创建用户登录名球员身份,在服务器端PHP有一个临时的解决方案。点击它知道什么玩家来创建它们的时候,而不是将旋转件状态的方格。

Before creating a user login for player identification, the server side php had a temporary solution. It would rotate the piece state for the squares when clicked instead of knowing what player to create them for.

创建登录的东西后,我设置了玩家的ID会话变量。我希望Ajax请求中读取从PHP会话ID,并找出他们是从那里什么样的球员。

After creating login stuff I set a session variable for the player's ID. I was hoping to read the session ID from the php during the ajax request and figure out what player they are from there.

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;

由于某些原因,当我运行完整的Web应用程序,碎片仍然周期不过,即使我删除了code,使其循环。 此外,登录如果我手动加载PHP页面在浏览器后,它正确地修改数据库(它只能播放属于该玩家件),并输出正确的结果。

For some reason when I run the full web app, the pieces still cycle though, even after I deleted the code to make them cycle. Furthermore, after logging in If i manually load the php page in the browser, it modifies the database correctly (where it only plays pieces belonging to that player) and outputs the correct results.

在我看来,这次会议使用Ajax使用时未结转。然而,谷歌搜索跟我​​说,会做的工作与阿贾克斯。

It seems to me that the session is not being carried over when used with Ajax. Yet Google searches tell me that, sessions do work with Ajax.


更新:我想提供更多的信息。


Update: I'm trying to provide more information.

  1. 测井在正常工作。我的 ID是公认的,我打印它 在明年向董事会保证 我是正确的检索它。

  1. Logging in works correctly. My ID is recognized and I printed it out next to the board to ensure that I was retrieving it correctly.

Ajax请求并更新 板。传递的值是 正确和Firebug的证实 安慰。然而,而不是把 只为游戏者棋子它们 属于它的周期,虽然片 国家(0,1,2)。

The ajax request does update the board. The values passed are correct and confirmed with firebug's console. However instead of placing pieces only for the player they belong to it cycles though the piece states (0,1,2).

在手动浏览到 boardUpdate.php并把在 从阿贾克斯发送相同的价值观 结果可以看出,在echo'ed响应 表示相应的 每个时间片播放 意。

When manually browsing to boardUpdate.php and putting in the same values sent from the Ajax the results seen in the echo'ed response indicates that the corresponding piece is played each time as intended.

在我的笔记本电脑相同的结果后, 火狐新鲜的负担。

Same results on my laptop after fresh load of firefox.

手动浏览到 boardUpdate.php没有登录 前手离开董事会 不变(如预期在没有用户 在会议上被发现)。

Manually browsing to boardUpdate.php without logging in before hand leave the board unchanged (as intended when no user is found in the session).

我双重检查的那 在session_start()是在PHP文件 和双重检查会话ID 变量。

I've double checked the that session_start() is on the php files and double checked the session ID variables.

希望这些额外的信息可以帮助,我跑出来的想法有什么要告诉你。我应该加载了完整的code?


更新2:

Hope this extra information helps, i'm running out of ideas what to tell you. Should I load up the full code?


Update 2:

在火错误检查阿贾克斯的反响后,我意识到,'玩'的要求没有得到结果,和董事会不更新,直到下一个更新。我仍然在寻找到这一点,但我将它张贴在这里为你们了。

After checking the Ajax responce in fire-bug I realized that the 'play' request does not get a result, and the board is not updated till the next 'update'. I'm still looking into this but I'll post it here for you guys too.

boardUpdate.php 值得注意的地方是: 刷新板(LINE6) 地方件(line20) 功能boardUpdate($ turnCount)(line63)

boardUpdate.php Notable places are: Refresh Board(line6) Place Piece(line20) function boardUpdate($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'd be glad to provide more information if needed. Thanks in advance =)

推荐答案

从一小段code,我们有,这是很难说你的问题可能是。我能说的是,在session_start 应该是你做的每一页,在这里你希望使用的会话的第一件事情之一。在那之后,我只想立刻做了的var_dump $ _ SESSION 一看就知道数据在那里(放的模具之后的)。这很可能是你真正的问题出在别的地方,那会其实工作。是否与您的登录code的一个问题,例如,导致它消灭会议?

From the small snippet of code we have, it's difficult to tell what your problem might be. What I can say is that session_start should be one of the first things you do on each page where you're expecting to use the session. After that, I would just immediately do a var_dump of $_SESSION to see that the data is in there (put a die right after that). It is quite possible that your true problem lies somewhere else, and that the session is in fact working. Is there a problem with your login code, for example, that is causing it to wipe out the session?

您可以使用萤火虫来看看你的AJAX调用原始的结果,这应该是有帮助的,因为你的脚本似乎工作,如果你直接访问的页面。

You can use Firebug to look at the raw results of your AJAX calls, which should be helpful, since your script appears to work if you directly visit the page.

在那里我看到会议如预期不起作用案件一般都认为在session_start 正在调用过于频繁或过晚。另一种可能性是,你有一个疯狂的短暂停,但听起来不太可能。

Cases where I've seen sessions not work as expected have generally been that session_start is being called too often or too late. The other possibility is that you have an insanely short timeout, but that sounds unlikely.

最后,你可以确保你的PHP安装设置为使用cookie的会话。这不太可能在这一点上,它不会是,但你可以看看。

Finally, you can make sure that your PHP install is set to use cookie sessions. It's very unlikely at this point that it wouldn't be, but you could look.

这篇关于PHP会话不工作与jQuery阿贾克斯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆