简体   繁体   English

如何在php中访问同一页面上会话的增量值

[英]How to access increment value of session on same page in php

How to access the incremented value of session on the same page where we have defined the initial value of session, I am using this method for incrementation of value but on another page 如何在我们定义了会话初始值的同一页面上访问会话的增量值,我在另一页上使用此方法来增加值

<?php
    $_SESSION['indexValue'] = 1;
?>

This is my test.php page where i have decrelaed the inital value of session. 这是我的test.php页面,其中我已减小了会话的初始值。

<?php
    $Incrementvalue = $_SESSION['indexValue'];
    $counter = (int)$Incrementvalue;
    if ($counter=$counter) { 
        $counter++;
        $_SESSION['indexValue'] = $counter;             
    }
    echo "$_SESSION['indexValue']";
?>

This is my getdata.php page where I have implmented the increment value function. 这是我的getdata.php页面,其中实现了增量值功能。 Now i have to pass this increment value of Session again on test.php page . 现在,我必须在test.php页面上再次传递Session的增量值。 How can I perform this? 我该如何执行呢?

your code seems not to be thought off 您的代码似乎没有被考虑

first of all, you need to call session_start() at beginning then you can access everyewhere your $_SESSION variable (if you call session_start() before) 首先,您需要在开始时调用session_start(),然后才能访问$ _SESSION变量的所有位置(如果之前调用过session_start())

if ($counter=$counter) { 
      $counter++;
     $_SESSION['indexValue'] = $counter;             
}

this seems useless 这似乎没用

$_SESSION['indexValue']++;

is enough 足够

First of all there are few errors in code like 首先,代码中几乎没有错误,例如

if ($counter=$counter) should be if ($counter == $counter) if ($counter=$counter)应该是if ($counter == $counter)

And

echo "$_SESSION['indexValue']" should be echo $_SESSION['indexValue'] . echo "$_SESSION['indexValue']"应该是echo $_SESSION['indexValue']

Now answer to your question is that session is accessible anywhere throughout the project/application. 现在回答您的问题是,在整个项目/应用程序中的任何地方都可以访问会话。 So you need not to pass it , just access it as $_SESSION['indexValue'] . 因此,您无需传递它,只需将其作为$_SESSION['indexValue']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM