1

您好,我现在确实在一项任务挑战中找到了自己,基本上我已经能够完全实现挑战,但有一件事我应该能够在浏览刷新上更改 $_SESSION 超全局我已经查看了所有类型的可能替代方案,但似乎没有工作,感谢任何帮助这是我的代码我正在寻找的是一种在浏览器刷新时更改 $_SESSION 的方法,每个工作版本似乎有可能我不知道尝试哪种其他方式我曾尝试取消设置或销毁会话,但它会删除整个表单:

<?php
session_start();

if(!isset($_SESSION['secret'])) {
  $possible_chars = array_merge(range('A','Z'),range('0','9'));
  shuffle($possible_chars);
  $string = substr(implode($possible_chars),0,5);
  $_SESSION['secret'] = $string;
}
else {
?>
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>THIS FORM POST TO ITSELF TO BE SURE THE USER ENTERS THE RIGHT INFO</title>
    <IMG SRC="index.php" alt="captcha"/>
  </head>
  <body>
    <h1>Captcha Form</h1> 
    <form method="post" action="index.php">
      <fieldset>
        <ul>
          <li>
            <label for="email">Email</label>
            <br>
            <input name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" />
          </li>
          <li>
            <label for="comments">Comments</label>
            <br>
            <input name="comments" id="comments"  value = "<?php if(isset($_POST['comments'])) echo $_POST['comments']; ?>" />
          </li>
          <li>
            <label for="captcha">Please input CAPTCHA:</label>
            <br>
            <input name="captcha" id="captcha"/>
          </li>
          <li>
            <input type="submit" name="submit" value="Submit My Captcha!"/>
          </li>
        </ul>
        <?php print_r($_SESSION['secret']); ?>
        <br\>
        <br\>
        <?php
        $answer = (isset($_POST['captcha']) ? $_POST['captcha'] : null);
        $email = (isset($_POST['email']) ? $_POST['email']: null);
        $comments = (isset($_POST['comments']) ? $_POST['comments']: null);
        if( !empty( $_POST )) {
          if (strcasecmp($answer,$_SESSION['secret']) != 0) {
            //($_SESSION['secret'] != $answer )
            echo "Fee Fi Fo Fum Fot I smell the blood of an automated bot!";
            //$_SESSION['secret'] = NULL;
            header('captcha_challenge.php');
            //$_SESSION['secret'] = NULL;
          }
          elseif (strcasecmp($answer,$_SESSION['secret']) == 0) {
            //($_SESSION['secret'] == $answer)
            unset ($_SESSION['secret']);
            $_SESSION['email'] = $_POST['email'];
            $_SESSION['comments'] = $_POST['comments'];
            $query = ("INSERT INTO comments (email, content) VALUES ( '$email', '$comments')");
            $results = $db->query($query);
            header('Location:goodMessage.php');
          }
        }
}
?>
4

1 回答 1

2

这是我使用的会话超时。

最初创建会话时:

session_start();
$_SESSION['LAST_ACTIVITY'] = time();

导航到新页面时在其他页面中调用以更新会话超时的函数

function check_timeout($timeout, &$SESSION)
    {
        if (isset($_SESSION['LAST_ACTIVITY']))
        {        
            if((time() - $_SESSION['LAST_ACTIVITY']) > $timeout)
            {
                end_session($SESSION);
            }
            $_SESSION['LAST_ACTIVITY'] = time();
        }
        else 
        {
            end_session($SESSION);
        }
    }

我有一个单独的函数 (end_session()) 来结束会话。参数中的 & 允许函数更新会话。

在每一页上,我调用:

session_start();
check_timeout($timeout, $SESSION); 
于 2013-03-19T17:53:56.117 回答