Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在简单的php登录时间中添加计时器_Php_Timer_Login Script - Fatal编程技术网

在简单的php登录时间中添加计时器

在简单的php登录时间中添加计时器,php,timer,login-script,Php,Timer,Login Script,我想知道如何在一个简单的php登录表单中添加一个计时器,用于显示用户保持登录的时间(优先级) 如果你不介意的话,你能告诉我如何乘以每小时的时间价格比吗 例如:Q先生保持登录3小时,价格为2美元,因此主页将在计时器旁边显示总计6美元,并使用会话变量跟踪持续时间。当用户登录时,记录时间并将其粘贴到会话变量中。这只会在登录时发生,并且在他们注销时会被删除 每次刷新页面时,获取当前时间和会话时间戳之间的差值,然后进行乘法以获取成本 <?php // session_start(); goes at

我想知道如何在一个简单的php登录表单中添加一个计时器,用于显示用户保持登录的时间(优先级)

如果你不介意的话,你能告诉我如何乘以每小时的时间价格比吗


例如:Q先生保持登录3小时,价格为2美元,因此主页将在计时器旁边显示总计6美元,并使用会话变量跟踪持续时间。当用户登录时,记录时间并将其粘贴到会话变量中。这只会在登录时发生,并且在他们注销时会被删除

每次刷新页面时,获取当前时间和会话时间戳之间的差值,然后进行乘法以获取成本

<?php
// session_start(); goes at the top of any page where you need to create or access session variables
session_start(); // you have to tell PHP to start the session before you add a session variable 

function login() {
  // do logic and log in the user
  // set a session variable
  if ($logged_in) {
    $_SESSION['logged_in_time'] = time();
  }
}

function logout() {
  // do logic and log OUT the user
  // clear the session variable
  unset($_SESSION['logged_in_time']);
}

html



你在问如何做3*2??或者你在问如何计算他们登录后的小时数?还是怎样这两个都很容易在谷歌上找到。目前还不清楚你的问题具体是什么,我有一个答案可以帮助你解决,但我决定给你一个机会,说明你将如何处理这个问题以及你感到困惑的部分。在这里获得帮助的基本要求是自己尝试first@ADyson,我的意思是如何添加一个计时器,显示用户保持登录的时间,如果可能的话还显示会话的成本,3*2只是一个例子,我知道这对这里的大多数人来说似乎是一个简单的问题,但我是第一个php的绝对初学者script@kinglish,如果你能解释一下如何作为一个绝对的初学者来处理这个问题,那将非常有帮助,我知道某个地方可能会有一个网站解释它,但是对于一些人来说,在php程序中添加一个计时器是很难找到这么简单的方法的。我已经发布了一种方法,其中有很多注释来解释代码和一些描述性文本。看看这对你来说是否有意义你应该解释一下代码
<?php 
if ($is_logged_in) {
  // get the duration in minutes (time() is in seconds so we divide by 60 to get minutes) - and ceil() rounds the number with decimal to a whole number
  $duration_minutes = ceil((time() - $_SESSION['logged_in_time']) / 60);       
  $cost_per_min = 2/60; // get the cost per minute based on the $2 hourly rate
  $my_cost = $cost_per_min * $duration_minutes;
  if ($duration_minutes > (60 * 24) ) {
    // test to see if we need an 's' appended to our duration
    $s = ($duration_minutes/(60 * 24) > 1) ? "s" : "";
    $duration_display = toFixed($duration_minutes/(60 * 24), 1) . " day" . $s;
  } else if ($duration_minutes > 60) {
    $s = ($duration_minutes/60 > 1) ? "s" : "";
    $duration_display = ceil($duration_minutes/60) . " hour" . $s ;
  } else {
    // test to see if we need an 's' appended to our duration
    $s = ($duration_minutes > 1) ? "s" : "";
    $duration_display = $duration_minutes . " minute" . $s;
  }  
  $message = "You have been logged in " . $duration_display . " ($" . $my_cost . ")";
}
// in the above functions toFixed($value, 1) takes a number like 3.34567 and makes it 3.3
?>
<div class='cost_header'><?php echo $message?></div>