本文介绍了PHP会话变量没有解析,但ISSET表示它正在运行&在0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 好的,所以我有一个计数器,它一次只能计数一秒。这个第二个计数器我希望它是我的会话变量。这个坏主意吗?此外,当我在第二页上执行isset时,它在我的第二页上显示您的会话正在运行0。我确保在我的页面开始时我的php会话周围没有额外的白色位置,但我不认为这是问题所在。我也试过在我的countUP函数上应用清除间隔,但它不起作用?我非常感谢你的帮助。Ok, so I have a counter which simply counts up a second at a time.This second counter I want it to be my session variable.Is this a bad idea? Also when I do a isset on my second page its says on my second page "Your session is running 0". I have ensured there are no extra white places around my php session at the start of my page but i dont think thats the problem. I ahve also tried applying clear interval on my countUP function but it doesnt work? Im really thankful for any help. 第一页.php <?php session_start();if(!isset($_SESSION["counter"])){ //checkingto see if a variable called my score exist $_SESSION["counter"]=0; //if not, initates this variable with 100 } ?> page one_javascript <script type="text/javascript"> var counter=0; //initiates score to 100var timer_container = document.getElementById("timer_container"); //get div that wil display the scoretimer_container.innerHTML="Time = " + counter; //popovar timer; function countUP() { counter = counter +1;//increment the counter by 1 //display the new value in the div document.getElementById("timer_container").innerHTML = counter; } </script> page2.php <?php session_start(); if(isset($_SESSION['counter'])){ echo "Your session is running ".$_SESSION['counter']; } else { echo "Session not running";} ?> 第2页javascript <script type="text/javascript"> var counter=<?php echo $_GET['counter'];?>; //initiates score to 100var timer_container =document.getElementById("timer_container"); //get div that will display the scoretimer_container.innerHTML="Time="+counter;//var timer;function countUP() { counter = counter+1; //increment the counter by 1 //display the new value in the div document.getElementById("timer_container").innerHTML = counter; } </script>我之前在另一场比赛中使用过它,它运作得很完美。谢谢I have used this before in another game and it works perfect. thanks推荐答案没看过你的html,但你可能会遗漏一些东西要启动并运行定时器功能 - 这可能会丢失:Haven't seen your html but you might be missing something to start and run the timer function - this might be missing: <body onload="intTimer = setInterval(function(){ countUP() }, 1000);">此外,我不太清楚你是如何提交页面但我看不到任何通过计时器对会话变量的值。如果您使用提交按钮在页面上放置一个表单,那么您可以获取隐藏输入的值,其值使用javascript设置。如果您需要,当计数器达到某个数字时,您可以使用 submit(); 自动提交页面:Also I am not quite clear how you are submitting the page but I can't see anything that passes the value of the timer to the session variable. If you put a form on your page with a submit button then you could pick up the value of a hidden input with its value being set with javascript. You could submit the page automatically using submit(); in your javascript when the counter reaches a certain number if you want that: page1.html (可以称为 page1,php 但第1页中没有php,所以实际上并不是必需的)page1.html (could be called page1,php but there is no php in page 1 so that is not actually necessary) <!DOCTYPE html> <html> <head> <title>Page 1</title> <script language="javascript"> var intTimer = ''; var counter = 0; function countUP(){ counter++; //increment the counter by 1 //display the new value in the div document.getElementById("timer_container").innerHTML = "Session Time Counter: " + counter; document.getElementById("tim_val").value = counter; } </script> </head> <body onload="intTimer = setInterval(function(){ countUP() }, 1000);"> <form name="form" id="form" action="page2.php" method="post"> <input type="hidden" id="tim_val" name="tim_val" value="" /> <input type="submit" name="submit"/> </form> <div id="timer_container">Session Time Counter: 0</div> </body> </html> 您没有引用 $ _ SESSION [counter] 在第1页上,你在JavaScript中初始化为0而不是100。You aren't referencing $_SESSION["counter"] on page 1 and you are initialising to 0 not 100 in your JavaScript.在你的第2页脚本中 page2.php 你会有这个 - 你也可以将这个页面提交给自己,如果只是要改变一部分内容但是计时器需要继续或在提交时记录。In your page 2 script page2.php you would have this - you could also submit this page to itself if only a part of the content is to be changed but the timer needs to continue or be logged at the point of submission.<?php session_start();?><!DOCTYPE html><html><head><title>Page 2</title><?phpif(!isset($_SESSION["counter"])){//if not, initiate this variable with 100 $_SESSION["counter"] = 100; }else{// give it the posted value$_SESSION["counter"] = (int) $_POST['tim_val']; } ?><script language="javascript">clearInterval(intTimer);var intTimer = ''; var counter = <?php echo $_SESSION["counter"]; ?>;function countUP(){counter++; //increment the counter by 1//display the new value in the divdocument.getElementById("timer_container").innerHTML = "Session Time Counter: " + counter;document.getElementById("tim_val").value = counter;}</script> </head> <body onload="intTimer = setInterval(function(){ countUP() }, 1000);"> <form name="form" id="form" action="page2.php" method="post"> <input type="hidden" id="tim_val" name="tim_val" value="<?php echo $_SESSION["counter"]; ?>" /> <input type="submit" name="submit"/> </form> <div id="timer_container">Session Time Counter: <?php echo $_SESSION["counter"]; ?></div> </body></html>在你的page2.php JavaScript中你需要 clearInterval()并重新启动它。 http://www.w3schools.com/jsref/met_win_clearinterval.asp And in your page2.php JavaScript you would need to clearInterval() and restart it. http://www.w3schools.com/jsref/met_win_clearinterval.asp var counter=<?php echo intval($_SESSION["counter"]);?>; //initiates score to submitted value您可能不需要使用会话变量使用隐藏输入,所以你可以使用:You probably won't need to use a session variable as you are using the hidden input, so you could use: var counter=<?php echo intval($_POST["counter"]);?>; //initiates score 您还可以添加 1 以约为延迟提交它You could also add 1 to it to approximately account for the delay submitting it var counter = <?php echo $_SESSION["counter"] + 1; ?>; var counter=<?php echo intval($_POST["tim_val"] + 1);?>; //initiates score 你可以坚持使用 GET 但我只是喜欢 POST 。You could stick with using GET but I just prefer POST.这有更多关于隐藏输入的信息: 重复获取并回显来自textarea的输入 This has a bit more about hidden inputs:Get and echo inputs from textarea repeatedly以及关于计时器脚本的更多信息: http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock and a bit more about the timer script: http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock 这篇关于PHP会话变量没有解析,但ISSET表示它正在运行&在0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 03:52