我试图使用PHP将数据从表单应用程序保存到cookie

时间:2013-06-26 05:07:38

标签: php forms cookies

所以,我正在创建一个HTML 5游戏并创建我希望将保存数据存储在cookie中的保存。在尝试理解其工作原理时,我创建了一个表单应用程序:

<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

另外,请原谅我的noobness,因为我确定这只是一个愚蠢的错误,或者我对某些东西缺乏基本的理解,这里是cookie set.php,错误handeling正在工作,我只是因为每个值的内容都为null我在表单应用中输入的值:

<?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";
    }


?>

7 个答案:

答案 0 :(得分:2)

我认为你在获取数据时存在问题

需要

$_POST['$astate'];,例如$_POST['astate'];无需在帖子字段名称中唱歌。

下面的设置cookie的

是最好的例子

$astate = $_POST['astate'];

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

尝试这样,如果您遇到问题,请告知我

答案 1 :(得分: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'];
   ?>

答案 2 :(得分:0)

我认为游戏逻辑是用Javascript编写的?您可以set cookies from javascript,也可以使用localstore,这对于大数据来说是更好的选择。

答案 3 :(得分: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'];

答案 4 :(得分:0)

查看你的setcookie.php,你正在以错误的方式接收提交的变量。要接收通过帖子提交的变量,请使用$variable=$_POST['formVariable']; 您也以错误的方式设置cookie。要设置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";
    }
    ?>

答案 5 :(得分: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 />";
    }
  }
?>

答案 6 :(得分: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条件,因为它与你的第一个条件恰好相反)