会话计时器并将用户名传递到结束页面

时间:2015-05-30 09:16:31

标签: php html

如何显示从第一页的下拉列表中选择的用户名,并在最后一页显示用户名,而不是一页又一页地传递变量?另外,如果我想在中间页面添加计时器,我该如何将它放入php会话中,并使用用户名显示结束页面上的时间。谢谢! 这是登录页面的代码

<p id="Hello world 1"></p>
<input type="text" value="Hello world 1">
<center>Centre Name
<select id="Centre_Name" id="Centre_Name" , name="Centre_Name">
<option value="Please Select">Please Select</option>
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
<option value="DDD">DDD</option>
</select>
<a href="hello%20test%20file/Hello%20world%202.html"><input type="button"     id="btnQueryString" value="Submit" /></a>

2。第二页有一个javascript函数,记录当前时间。

<p id="Hello world 2"></p><br>
<button onclick="sessionstart()">start lesson</button>
<p id="starttime"></p>
<script>
function addZero(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}
var d = new Date();
    var x = document.getElementById("starttime");
    var a = addZero(d.getHours());
    var b = addZero(d.getMinutes());
function sessionstart() {
    x.innerHTML = a + ":" + b ;
}
</script>
<a href="file:///C:/Users/LLJ/Desktop/hello%20test%20file/Hello%20world%203.html"><input type="button" id="button"value="next" onclick="display_c(start)"></a>

1 个答案:

答案 0 :(得分:0)

第1页上,下载并提交表单。将提交指向第2页。

<form action="page2.php" method="POST">
    //your dropdown
    <input type="submit" value="Submit" />
</form>
<?php
    session_start();
    //clean out a possible previous session
    session_unset();
?>

第2页上,使用session_start()启动php会话 将发布的值存储在session_var中:

<?php 
    //DON'T forget to sanitize your $_POST variables!!!
    session_start();
    $_SESSION['myvar1']=$_POST['myvar1'];
    $_SESSION['myvar2']=$_POST['myvar2'];

    //if you want a timer
    $_SESSION['time_started']=time();

    echo $_SESSION['myvar1'];
    echo $_SESSION['myvar2'];

    echo '<a href="page3.php">goto page 3</a>';
?>

现在,只要用户打开浏览器,您就可以访问$_SESSION['variable']而无需通过每个页面传递它:

第3页

<?php 
    session_start();
    if(!isset($_SESSION['myvar1'],$_SESSION['myvar1']){
        // ====> redirect to page 1
        }

    //check your timer: -900 is 15 minutes in seconds
    if($_SESSION['time_started']<time()-900){
         echo 'Too bad, it took you too long and now you failed';
         die;
         }

    echo $_SESSION['myvar1'];
    echo $_SESSION['myvar2'];

    echo '<a href="page3.php">let's page 4</a>';
?>