PHP试图找出一种在浏览器刷新时刷新会话的方法

时间:2013-03-19 17:36:17

标签: php session

你好我现在确实在一个任务挑战中发现我的自我基本上我已经能够完全实现挑战了但有一点我应该能够在浏览刷新上更改$ _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');
          }
        }
}
?>

1 个答案:

答案 0 :(得分:1)

这是我使用的会话超时。

最初创建会话时:

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())来结束会话。和&amp;在参数中允许通过函数更新会话。

在每个页面上,我打电话:

session_start();
check_timeout($timeout, $SESSION);