会话令牌无法正常工作

时间:2016-05-05 17:06:41

标签: php forms

我有两个相同的联系表单,但后端表单上的不同STMP信息除外。

让我感到困惑的是,当我完成这两个表单并重定向到一个表单时,即当我选择通过浏览器按钮返回时,我仍然看到表单上仍然存在所有信息。然而,在另一台服务器上,当我通过浏览器的后退按钮返回时,表单已自行刷新。

为什么行为在不同服务器上的相同表单上如此一致地不同?

修改

(还要注意表格是剥离版本,即我拿出了css,jquery的东西,页脚,导航等,以保持简短快速阅读)

我决定从一台服务器中包含该表单,当您回击时,该表单始终显示联系人的信息。我想要注意的是,这种行为非常一致,并且使用相同的浏览器。他们只是在不同的服务器上。此外,即不同的PHP版本,如果这有任何区别。它是PHP会话驱动的页面。即,浏览器不应该保留信息,对吗?

联系表单

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>

<?php
session_start(); //allows use of session variables

if (!isset($_SESSION['token']) && $_SESSION['token'] == $_POST['token']) {

  $token = base64_encode(openssl_random_pseudo_bytes(32));
  $_SESSION['token'] = $token;
}

else {
  $token = base64_encode(openssl_random_pseudo_bytes(32));
  $_SESSION['token'] = $token;
}

var_dump($_POST, FILE.__LINE__);

?>

<div id="main-container" class="col-xs-12">

  <div>
    <label>* Required fields </label>
  </div>


  <form class="form-horizontal"  method="POST" action="ughAgain.php" id="contactForm">

    <input type="hidden" name="token" value="<?php echo $token; ?>" />

    <div class="form-group">
      <label for="cfirstname" class="col-sm-2 control-label">* First Name</label>
      <div class="col-sm-10">
      <input class="form-control"  name="first_name" id="cfirstname" placeholder="First Name" type="text">
      </div>
    </div>

    <div class="form-group">
      <label for="clastname" class="col-sm-2 control-label">* Last Name</label>
      <div class="col-sm-10">
      <input class="form-control" name="last_name" id="clastname" placeholder="Last Name" type="text">
     </div>
    </div>

    <div class="form-group">
      <label for="cemail" class="col-sm-2 control-label">* Email</label>
      <div class="col-sm-10">
      <input class="form-control" name="email" id="cemail" placeholder="Email" type="email">
      </div>
    </div>

    <div class="form-group">
      <label for="cmessage" class="col-sm-2 control-label">* Message</label>
      <div class="col-sm-10">
      <textarea class="form-control" rows="2" placeholder="Please type in your message" name="message" id="cmessage"></textarea>
      </div>
    </div>

    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
    <div class="g-recaptcha" data-sitekey=""></div>
    <input type="hidden" name="recaptcha" data-rule-recaptcha="true">
  </div>
</div>

    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default" name="Submit">Submit</button>
    </div>
  </div>
</form>


</div>
</body>
</html>

行动表格:UghAgain.php

<?php
session_start();

  require_once('recaptchalib.php');
  require 'PHPMailerAutoload.php';



  $captcha = $_POST['g-recaptcha-response'];
  $secretKey = "";
  $ip = $_SERVER['REMOTE_ADDR'];
  $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
  $responseKeys = json_decode($response,true);
  if(intval($responseKeys["success"]) !== 1) {

      header('location: url');
 }

else  {


  if ($_POST['token'] == $_SESSION['token']) {

  $first_name = $_POST['first_name'];
  $last_name = $_POST['last_name'];
  $email = $_POST['email'];
  $message = $_POST['message'];


  $mail = new PHPMailer;
  $mail->isSMTP();  
  //$mail->SMTPDebug = 4; // debugging: 1 = errors and messages, 2 = messages only
  $mail->Host = ''; // Specify main and backup SMTP servers
  $mail->SMTPAuth = true;                               // Enable SMTP authentication
  $mail->Username = '';                 // SMTP username
  $mail->Password = ''; 
  $mail->From = '';
  $mail->FromName = 'Staff';
  $mail->addAddress( $email, $first_name );
  $mail->addAddress( '', 'Staff' );
  $mail->addReplyTo( $email, $first_name );
  $mail->isHTML(true);                                  // Set email format to HTML
  $mail->Subject = 'Hotel Contact';
  $mail->Body = "<table width='600'><tr><td colspan='2' height='20'><p style='text-align:center; font-size:20px; color:#ddd; padding:0px; margin:0px;'>Copy of Your Contact Form</p></td></tr>"
                ."<tr><td colspan='2'  height='80' width='600'><p style='color:#d1502d;font-size:50px;text-align:center;padding:0px; margin:0px;'>Hotel</p></td></tr>"
                ."<tr><td colspan='2'  height='60' width='600' style='text-align:center; color:#606060;font-size:20px;padding:0px; margin:0px;'> Los Angeles, CA </td></tr>"
                ."<tr><td colspan='2'  height='30' width='600' style='text-align:center;'><h3 style='color:#d1502d; '>Come and Explore Little Tokyo in Los Angeles</h3></td></tr>"
                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Your First Name: </p></td><td height='20' width='300'> <p style='font-size:20px; color:gray; padding:10px; margin:0px;'>$first_name</p></td></tr>"
                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Your Last Name: </p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px;'>$last_name</p></td></tr>"
                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Your Email Address: </p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px;'>$email</p></td></tr>"
                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Your Message:</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $message</p></td></tr></table>"
                ."<tr><td colspan='2'  height='15' width='600'><p style=' text-align:center;padding:0px; margin:0px;'><a style='color:#d1502d;font-size:15px;text-align:center;padding:0px; margin:0px;text-decoration:none;'href='http://whimsicalhope.com/testing_hotel_again4/index.html'>Visit Our Site</a></p></td></tr>";

    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';

    if(!$mail->send()) {
        header('location: url');

        } 
    else {
        header('location: url');
      }

   } 
}

整个脚本

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
  <link href="favicon.ico" rel="icon" type="image/x-icon" />
  <title> Hotel</title>
  <link href="css/bootstrap.min.css" rel="stylesheet" />
  <link href="css/style.css" rel="stylesheet" />
    <style type="text/css">
    .error {
      color:red;
    }
    body {
      padding-top: 146px;
      padding-top:175px;
    }
    .language_choice_english {
      padding-right: 1%
    }

    .language_choice_japanese {
        padding-right: 3%;
    }
  </style>

    <script src="js/jquery-1.11.2.min.js"></script>
    <script src="js/jquery.validate.js"></script>
    <script src="js/additional-methods.js"></script>
      <script> 
          $(document).ready(function () {

              $('#contactForm').validate({ 
                  ignore: '.no-validation',
                  rules: {
                      email: {
                          required: true,
                          email: true
                      },
                      message: {
                          required: true,
                          minlength: 5                      }, 
                      first_name: {
                        required: true,
                        lettersonly: true, 
                      },
                      last_name: {
                        required: true,
                        lettersonly: true,
                      }
                  }
              });

              $.validator.addMethod("recaptcha", function(value, element) {
                var grecaptcha = window.grecaptcha || null;
                return !grecaptcha || grecaptcha.getResponse();
                }, "Please fill reCAPTCHA");

          });
      </script>
  <script src="js/bootstrap.min.js"></script>
</head>
<body>

<?php
session_start(); //allows use of session variables

if (!isset($_SESSION['token']) && $_SESSION['token'] == $_POST['token']) {

  $token = base64_encode(openssl_random_pseudo_bytes(32));
  $_SESSION['token'] = $token;
}

else {
  $token = base64_encode(openssl_random_pseudo_bytes(32));
  $_SESSION['token'] = $token;
}

var_dump($_POST, FILE.__LINE__);

?>


  <nav class="navbar navbar-default navbar-fixed-top">
    <a class="brand pull-left" href="index.html">
            <img id="Logo" src="./images/logo_72H.gif" alt="New Hotel">
            </a>
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
       <a class="navbar-brand" href="index.html"><span id="overall_title"> Hotel</span><small id="description_of_city">Little Tokyo, Los Angeles, Downtown</small></a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="about.html">About</a></li>
        <li><a href="rooms.html">Rooms & information</a></li>
        <li><a href="reserve.php">Book to Reserve</a></li>
        <li><a href="location.html">Location Info</a></li>
        <li><a href="additional.html">Additional Info</a></li>
        <li><a href="contact.php">Contact</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
    <ul class="nav navbar-nav navbar-right">
        <p class="pull-right language_choice_english"><a href="contact.php">English</a></p>
        <p class="pull-right language_choice_japanese"><a href="contact-ja.php"><span lang="ja">日本語</span></a></p>
    </ul>
  </div><!-- /.container-fluid -->
</nav>
<div id="main-container" class="col-xs-12">

  <div>
    <label>* Required fields </label>
  </div>


  <form class="form-horizontal"  method="POST" action="ughAgain.php" id="contactForm">

    <input type="hidden" name="token" value="<?php echo $token; ?>" />

    <div class="form-group">
      <label for="cfirstname" class="col-sm-2 control-label">* First Name</label>
      <div class="col-sm-10">
      <input class="form-control"  name="first_name" id="cfirstname" placeholder="First Name" type="text">
      </div>
    </div>

    <div class="form-group">
      <label for="clastname" class="col-sm-2 control-label">* Last Name</label>
      <div class="col-sm-10">
      <input class="form-control" name="last_name" id="clastname" placeholder="Last Name" type="text">
     </div>
    </div>

    <div class="form-group">
      <label for="cemail" class="col-sm-2 control-label">* Email</label>
      <div class="col-sm-10">
      <input class="form-control" name="email" id="cemail" placeholder="Email" type="email">
      </div>
    </div>

    <div class="form-group">
      <label for="cmessage" class="col-sm-2 control-label">* Message</label>
      <div class="col-sm-10">
      <textarea class="form-control" rows="2" placeholder="Please type in your message" name="message" id="cmessage"></textarea>
      </div>
    </div>

    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
    <div class="g-recaptcha" data-sitekey=""></div>
    <input type="hidden" name="recaptcha" data-rule-recaptcha="true">
  </div>
</div>

    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default" name="Submit">Submit</button>
    </div>
  </div>
</form>


</div>


 <footer class="col-xs-12 roomsFooter">  

    <div class="row">
        <div class="col-md-4">2015 Hotel -- All rights reserved</div>
        <div class="col-md-4">Terms of Use</div>
       <div class="col-md-4">Facebook / Twitter / Instagram </div>
    </div>  
  </footer>

  <script src='https://www.google.com/recaptcha/api.js'></script>

</body>
</html>

0 个答案:

没有答案