session variable increments only once

五迷三道 提交于 2019-12-13 07:14:28

问题


I am using session values to increment 2 variable across pages with ajax..Simple one.. I have used session_start in Page1 and initialize the session variable to 0 in page 1.. In page2 I am initializing them based on condition.. I seems to increment once, them there is no effect..Here is my action page..

    <?php 
session_start(); 
?>
<html>
<body>
<?php

$j=isset($_POST['commit']) && $_POST['commit'];
    if($j)

    {
    $num=rand(0, 100);
    ?>


<div>
    <div id="inner_1">
        <?php 
        if($num<27){ 
            echo "Result:Hit";
            $_SESSION['off']=$_SESSION['off']=+1;
                echo "Offensive Player:";
            if(isset($_SESSION['off'])){echo $_SESSION['off'];}else{echo '0';}
                echo "Defensive Player:";
             if(isset($_SESSION['on'])){echo $_SESSION['on'];}else{echo '0';} 
                } 
        else 
                {
            echo "Result:Miss";
            $_SESSION['on']=$_SESSION['on']=+1;
            echo "Offensive Player:";
            if(isset($_SESSION['off'])){echo $_SESSION['off'];}else{echo '0';}
            echo "Defensive Player:";
            if(isset($_SESSION['on'])){echo $_SESSION['on'];}else{echo '0';}
                }    ?>
    </div>


</div>



    <?php } ?>

</body>
</html>

Page1

        <?php
       session_start();
       $_SESSION['off']=0;
       $_SESSION['on']=0;
       ?>

回答1:


$_SESSION['on']=$_SESSION['on']=+1;

What should this do? assign twice +1 to $_SESSION['on']?

To add some number to a variable, use:

$_SESSION['on'] += 1;

(Do the same for your $_SESSION['off'])




回答2:


its because

$_SESSION['off']=$_SESSION['off']=+1;
                                 ^
                                 +---unwanted = sign 

it should be

 $_SESSION['off']=$_SESSION['off']+1;

also same apply to the $_SESSION['on']




回答3:


$_SESSION['off']=+1 means it assign value 1 to session variable

Change

$_SESSION['off']=$_SESSION['off']=+1;

to

$_SESSION['off'] = $_SESSION['off']+1;

also change same as $_SESSION['on']=$_SESSION['on']=+1; above




回答4:


Declare your session variable inside your HTML PAGE in the first php tag and it should solve your problem.



来源:https://stackoverflow.com/questions/16153396/session-variable-increments-only-once

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!