GorillaForce
GorillaForce

Reputation: 57

PHP Trying To Figure out A way to Refresh Session on Browser Refresh

Hello I do find my self in an assignment challenge at the moment basically I have been able to implement the challenge completly but one thing I should be able to change the $_SESSION superglobal on Browse Refresh I had look all type of possible alternatives but none seem to work, any help is kindly apprecciated this is my code what Im looking for is a way for the $_SESSION to be changed on browser refresh a per the working version it seems to be possible I don't know which other way to try I had tried session unset or destroy but it deletes the whole form :

<?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');
          }
        }
}
?>

Upvotes: 1

Views: 5835

Answers (1)

Jeff Hawthorne
Jeff Hawthorne

Reputation: 568

this is a session timeout i use.

when the session is initially created:

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

the function that gets called in other pages to update the session timeout when navigating to new pages

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);
        }
    }

i have a separate function (end_session()) to end a session. the & in the parameter allows the session to be updated by the function.

on each page, i call:

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

Upvotes: 2

Related Questions