存储变量会话php

时间:2013-08-30 12:17:38

标签: php session-variables

嗨,我在理解会话是如何工作方面遇到了麻烦。我正在尝试编写一个程序,它将重复请求输入一个数字,每次输入数字时,程序都要打印出来:

  1. 那个号码
  2. 输入的所有数字的总和
  3. 我提交的次数
  4. 到目前为止,这是我的代码:

    <?php
    session_start();
    if(isset($_session['count']))
    {
        $_session['count'] = $count;
    }else{
        $_session['count'] = 0; 
    }
    ?>
    
    <html>
        <head>
            <title>number</Title>
                <style>
                </style>
            <body>
                <form action = "numbers.php" method = "post">
                    Numbers: <input type "text" name = "number" size = "6"/>
                    <input type = "submit" value = "submit" name = "submit"/>
                    <p>
                </form>
            </body> 
        </head>
    </Html>
    <?php   
        if(isset($_POST["submit"]))
        {
            $number = $_POST['number'];
            If (is_numeric($number))
            {   
                $count = $_session['count'] + $number;
                print "Last number entered: ".$number;
                print "<br>Total internal numbers: ".$count;
            }
        }   
    ?>
    

    我试图存储我的$count变量,所以每次提交时都会输出输入数字的总和

4 个答案:

答案 0 :(得分:2)

在设置名为$_session['count'] = $count;的变量之前设置$count。在设置$count之后,您必须更新会话值:

$count = $_session['count'] + $number;
$_session['count'] = $count;

然后,您可以使用以下命令替换脚本顶部:

if(!isset($_session['count']))
{
    $_session['count'] = 0; 
}

答案 1 :(得分:0)

<?php   
    if(isset($_POST["submit"]))
    {
        $number = $_POST['number'];
        If (is_numeric($number))
        {   
            $count = $_session['count'] + $number;
            $_session['count'] = $count;
            print "Last number entered: ".$number;
            print "<br>Total internal numbers: ".$count;
        }
    }   
?>

答案 2 :(得分:0)

一开始你使用未定义的变量$ count。将其编辑为:

if(!isset($_session['count']))
    $_session['count'] = 0; 

然后添加一个新行:

If (is_numeric($number))
{   
    $count = $_SESSION['count'] + $number;
    $_SESSION['count'] = $count;
    print "Last number entered: ".$number;
    print "<br>Total internal numbers: ".$count;
}

或第二部分编辑为:

If (is_numeric($number))
{   
    $_SESSION['count'] += $number;
    print "Last number entered: ".$number;
    print "<br>Total internal numbers: ".$_SESSION['count'];
}

答案 3 :(得分:0)

这是工作示例。的 [TESTED]

您必须指定$_SESSION['count']=$count;

<?php
session_start();
//Code commented as not required.
/*if(isset($_session['count']))
{
    $_session['count'] = $count;
}else{
    $_session['count'] = 0;
}
*/?>

<html>
<head>
    <title>number</Title>
    <style>
    </style>
    <body>
    <form action = "" method = "post">
        Numbers: <input type "text" name = "number" size = "6"/>
        <input type = "submit" value = "submit" name = "submit"/>
        <p>
    </form>
    </body>
</head>
</Html>
<?php
if(isset($_POST["submit"]))
{
    $number = $_POST['number'];
    if (is_numeric($number))
    {

        $count = $_SESSION['count'] + $number;
        $_SESSION['count']=$count;
        print "Last number entered: ".$number;
        print "<br>Total internal numbers: ".$count;
    }
}
?>