1

So, I am creating a HTML 5 game and to create saves I want to store the save data in cookies. In trying to understand how this works I created a form app:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="cookieset.php" method="post">
    <p> Astate <input type="text" name="astate" size="10px" /></p>
    <p> Wstate <input type="text" name="wstate" size="10px" /></p>
    <p> Cstate <input type="text" name="cstate" size="10px" /></p>
    <p> Gstate <input type="text" name="gstate" size="10px" /></p>
    <p> G2state <input type="text" name="g2state" size="10px" /></p>
    <p> G3state <input type="text" name="g3state" size="10px" /></p>
    <p> AKCstate <input type="text" name="akcstate" size="10px" /></p>
    <p> TKCstate <input type="text" name="tkcstate" size="10px" /></p>
    <p> FKCstate <input type="text" name="fkcstate" size="10px" /></p>
    <input type="submit" name="submit" value="Submit" />
</form> 

<form action="retreive.php" method="get">
    <input type="submit" name="retreive" value="Retreive" />
</form>



</body>
</html

also, please forgive my noobness as I am sure it is just a stupid mistake or my lack of basic understanding of something, here is cookie set.php, the error handeling is working, I am just getting null for every value insted of the value I put into the form app:

<?php

$_POST['$astate'];
$_POST['$wstate'];
$_POST['$cstate'];
$_POST['$gstate'];
$_POST['$g2state'];
$_POST['$g3state'];
$_POST['$akcstate'];
$_POST['$tkcstate'];
$_POST['$fkcstate'];

setcookie("rqs1", $astate);
setcookie("rqs2", $wstate);
setcookie("rqs3", $cstate);
setcookie("rqs4", $gstate);
setcookie("rqs5", $g2state);
setcookie("rqs6", $g3state);
setcookie("rqs7", $akcstate);
setcookie("rqs8", $tkcstate);
setcookie("rqs9", $fkcstate);


if(isset($_COOKIE['rqs1'])){
    echo "$name : $value <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs1']))){
        echo "null \n";
        }

if(isset($_COOKIE['rqs2'])){
    echo "$name : $value <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs2']))){
        echo "null \n";
        }
if(isset($_COOKIE['rqs3'])){
    echo "$name : $value <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs3']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs4'])){
    echo "$name : $value <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs4']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs5'])){
    echo "$name : $value <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs5']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs6'])){
    echo "$name : $value <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs6']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs7'])){
    echo "$name : $value <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs7']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs8'])){
    echo "$name : $value <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs8']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs9'])){
    echo "$name : $value <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs9']))){
    echo "null \n";
    }


?>
4

7 回答 7

2

我认为您在获取数据时遇到问题

$_POST['$astate'];需要像$_POST['astate'];在帖子字段名称中不需要 $ sing 一样。

下面设置 cookie 是最好的例子

$astate = $_POST['astate'];

<?php
$expire=time()+60*60*24*30;
setcookie("astate", $astate, $expire);
?>

试试这样,如果你有问题,请告诉我

于 2013-06-26T05:16:16.933 回答
1

将后变量分配给变量而不是使用它们。

它会起作用的

   <?php
    $astate = $_POST['astate'];
    $wstate = $_POST['wstate'];
    $cstate = $_POST['cstate'];
    $gstate = $_POST['gstate'];
    $g2state = $_POST['g2state'];
    $g3state = $_POST['g3state'];
    $akcstate = $_POST['akcstate'];
    $tkcstate = $_POST['tkcstate'];
    $fkcstate = $_POST['fkcstate'];
   ?>
于 2013-06-26T05:14:49.113 回答
0

我假设游戏逻辑是用 Javascript 编写的?您可以从 javascript 设置 cookie,也可以使用localstore,这将是大数据的更好选择。

于 2013-06-26T05:11:07.210 回答
0

我认为你需要改变这一点:

$_POST['$astate'];
$_POST['$wstate'];
$_POST['$cstate'];
$_POST['$gstate'];
$_POST['$g2state'];
$_POST['$g3state'];
$_POST['$akcstate'];
$_POST['$tkcstate'];
$_POST['$fkcstate'];

对此:

$astate=$_POST['astate'];
$wstate=$_POST['wstate'];
$cstate=$_POST['cstate'];
$gstate=$_POST['gstate'];
$g2state=$_POST['g2state'];
$g3state=$_POST['g3state'];
$akcstate=$_POST['akcstate'];
$tkcstate=$_POST['tkcstate'];
$fkcstate=$_POST['fkcstate'];
于 2013-06-26T05:16:33.327 回答
0

查看您的 setcookie.php,您以错误的方式接收提交的变量。要接收通过 post 提交的变量,您使用$variable=$_POST['formVariable']; You are also set the cookie in the wrong way. 要设置 cookie,请使用setcookie($cookiename, $cookievalue, $timetoexpire);. 因此,您的 setcookie.php 应如下所示

    <?php
    $astate=$_POST['astate'];
    $wstate=$_POST['wstate'];
    $cstate=$_POST['cstate'];
    $gstate=$_POST['gstate'];
    $g2state=$_POST['g2state'];
    $g3state=$_POST['g3state'];
    $akcstate=$_POST['akcstate'];
    $tkcstate=$_POST['tkcstate'];
    $fkcstate=$_POST['fkcstate'];

    //Expire after 1 Hour for example
    $timetoexpire=time()+60*60;

    //set cookies
    setcookie("rqs1", $astate,$timetoexpire);
setcookie("rqs2", $wstate,$timetoexpire);
setcookie("rqs3", $cstate,$timetoexpire);
setcookie("rqs4", $gstate,$timetoexpire);
setcookie("rqs5", $g2state,$timetoexpire);
setcookie("rqs6", $g3state,$timetoexpire);
setcookie("rqs7", $akcstate,$timetoexpire);
setcookie("rqs8", $tkcstate,$timetoexpire);
setcookie("rqs9", $fkcstate,$timetoexpire);

if(isset($_COOKIE['rqs1'])){
    echo "$name : ".$_COOKIE['rqs1']." <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs1']))){
        echo "null \n";
        }

if(isset($_COOKIE['rqs2'])){
    echo "$name : ".$_COOKIE['rqs2']." <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs2']))){
        echo "null \n";
        }
if(isset($_COOKIE['rqs3'])){
    echo "$name : ".$_COOKIE['rqs3']." <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs3']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs4'])){
    echo "$name : ".$_COOKIE['rqs4']." <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs4']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs5'])){
    echo "$name : ".$_COOKIE['rqs5']." <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs5']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs6'])){
    echo "$name : ".$_COOKIE['rqs6']." <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs6']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs7'])){
    echo "$name : ".$_COOKIE['rqs7']." <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs7']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs8'])){
    echo "$name : ".$_COOKIE['rqs8']." <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs8']))){
    echo "null \n";
    }

if(isset($_COOKIE['rqs9'])){
    echo "$name : ".$_COOKIE['rqs9']." <br />\n";
    }
    elseif(!(isset($_COOKIE['rqs9']))){
    echo "null \n";
    }
    ?>
于 2013-06-26T05:18:22.930 回答
0

只是稍微更改了您的代码:\

<?php
  // `$_POST['$some_var']` doesnt create and assign a value to variable `$some_var`,
  // Its only check key `$some_var` exists in `$_POST` array and its if exists return its value
  // so doing it is WRONG
  $params = array( 'astate', 'wstate', 'cstate', 'gstate', 'g2state', 'g3state', 'akcstate', 'tkcstate', 'fkcstate' );

  foreach( $params as $param ) {
    $cookie = ( isset( $_POST[$param] ) ) ? trim( $_POST[$param] ) : null;
    $expire = ( time() + 3600 * 24 * 365 );
    if ( $cookie ) {
      // everything seems to be fine, now set the cookie
      setcookie( $param, $cookie, $expire, "/" );
    }
  }

  // now check here
  if ( isset( $_COOKIE["astate"] ) ) {
    // yes
  }
  else {
    // no
  }

  // OR

  foreach( $params as $param ) {
    if ( isset( $_COOKIE[$param] ) ) {
      echo $param." : ".$_COOKIE[$param]." <br />";
    }
  }
?>
于 2013-06-26T05:20:45.417 回答
0

HTML

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="cookieset.php" method="post">
<p> Astate <input type="text" name="state[a]" size="10px" /></p>
<p> Wstate <input type="text" name="state[w]" size="10px" /></p>
<p> Cstate <input type="text" name="state[c]" size="10px" /></p>
<p> Gstate <input type="text" name="state[g]" size="10px" /></p>
<p> G2state <input type="text" name="state[g2]" size="10px" /></p>
<p> G3state <input type="text" name="state[g3]" size="10px" /></p>
<p> AKCstate <input type="text" name="state[akc]" size="10px" /></p>
<p> TKCstate <input type="text" name="state[tkc]" size="10px" /></p>
<p> FKCstate <input type="text" name="state[fkc]" size="10px" /></p>
<input type="submit" name="submit" value="Submit" />
</form> 

<form action="retreive.php" method="get">
<input type="submit" name="retreive" value="Retreive" />
</form>

</body>
</html>

PHP

<?php
$state = $_POST['state'];
$x = 1;
foreach($state as $name => $value) {
  setcookie("rqs".$x, $value);
  print $name."state : ".$value." <br>";
  $x++;
}
?>

(顺便说一句,您不需要 else 语句中的第二个 if 条件,因为它与您的第一个 if 条件相反)

于 2013-06-26T05:44:35.197 回答