1

I am building a game on html5(phaser js) for which i need to build a leaderboard. the code snippet is this:

restart_game: function() {  
// Start the 'main' state, which restarts the game
//this.game.time.events.remove(this.timer);  
    //this.game.time.events.remove(this.timer2);  
    //this.game.state.start('main');
    var string="score.php?score="+this.score;
    window.open(string);
},

in the window.open function i wish to pass the value of score to another page where i will ask for the player's name and then insert both the score and the name to the database. But i am having trouble passing the score value across three pages. How can i do this? Do I need AJAX or just PHP and Javascript is sufficient?

4

4 回答 4

2

你可以使用浏览器cookie吗?您可以在 cookie 中保存分值并在需要时访问它?阅读这篇关于如何使用 cookie 的链接https://developer.mozilla.org/en-US/docs/Web/API/document.cookie

要像这样保存到cookie:

document.cookie="score=54; expires=Thu, 18 Dec 2013 12:00:00 GMT";

在 PHP 中,您可以读取 cookie

if(isset(($_COOKIE['score'])) {
    $score = $_COOKIE['score'];
}

在 JS 中读取 cookie:

var score = document.cookie;
于 2014-04-25T08:29:31.200 回答
1

您可以使用 session 变量将变量保存在内存中,并且在您的会话处于活动状态之前可以访问它。

<?php
error_reporting(E_ALL);
session_start();
if (isset($_POST['session'])) {
    $session = eval("return {$_POST['session']};");
    if (is_array($session)) {
        $_SESSION = $session;
        header("Location: {$_SERVER['PHP_SELF']}?saved");
    }
    else {
        header("Location: {$_SERVER['PHP_SELF']}?error");
    }
}

$session = htmlentities(var_export($_SESSION, true));
?>

欲了解更多信息,请看这里

于 2014-04-25T08:29:06.163 回答
1

查找jQuery

restart_game: function() {  
  var score = this.score;
  $.ajax({
    url: 'save_score.php',
    data: {score: score},
    method: 'POST'
  }).done(function() {
    window.location = "other_page.php";
  });
},

save_score.php

session_start();

if(isset($_POST['score']) && strlen($_POST['score']) > 0) {
  $score = intval($_POST['score']);
  $_SESSION['score'] = $score;
}

other_page.php

session_start();
var_dump($_SESSION);
于 2014-04-25T08:30:37.617 回答
0

您可以在 php 中使用 $_SESSION 变量来跟踪会话中的用户相关数据。它需要 cookie。

于 2014-04-25T08:28:58.253 回答