session token not setting the first time

2019-09-12 12:20发布

I have a basic php backend page, and a basic html form page.

There's been a problem with the current setup, i.e, that the unexpectedReserveError.html page gets triggered.

Finally figured out that it happens on the first time for any user when they come to the form page, and try to submit it. Then they get the unexpectedReserveError.html page. But when they try to resubmit it, it works fine. It's only the first time ... It's due to session token issue ... not sure how to solve it ...

This is the current session stuff in the basic html form (just highlighting it so you don't have to search for it) ...

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

?>

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

Basic PHP Backend Page

<?php

session_start();

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

        foreach($_POST as $key => $value) {
        if(ini_get('magic_quotes_gpc'))
          $_POST[$key] = stripslashes($_POST[$key]);
          $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
    }



                    $nights = $_POST['nights']; 
                    $arrivals = $_POST['arrivals'];
                    $male = $_POST['male']; 
                    $female = $_POST['female']; 
                    $rooms = $_POST['rooms']; 
                    $type = $_POST['type']; 
                    $name = $_POST['name']; 
                    $address = $_POST['address']; 
                    $zip = $_POST['zip']; 
                    $telephone = $_POST['telephone']; 
                    $email = $_POST['email_again']; 
                    $cell = $_POST['cell']; 
                    $message = $_POST['message']; 
                    $date = $_POST['date'];

                    require 'PHPMailerAutoload.php';

                  $mail = new PHPMailer;
                  $mail->CharSet = 'UTF-8';
                  $mail->isSMTP();  
                  //$mail->SMTPDebug = 4; // debugging: 1 = errors and messages, 2 = messages only
                  $mail->Host = 'tls://smtp.gmail.com:587'; // Specify main and backup SMTP servers
                  $mail->SMTPAuth = true;                               // Enable SMTP authentication
                  $mail->Username = '';                 // SMTP username
                  $mail->Password = ''; 
                  $mail->addReplyTo( '', 'Hotel' );
                  $mail->AddCC('', 'Hotel');
                  $mail->addAddress(  $email, $first_name );
                  $mail->SetFrom('', '');
                  $mail->isHTML(true);                             
  // Set email format to HTML

                    $mail->Subject = 'Hotel Room Request';


                    $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 Reserve 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  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 Number of Nights: </p></td><td height='20' width='300'> <p style='font-size:20px; color:gray; padding:10px; margin:0px;'>$nights</p></td></tr>"
                                                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Your Arrival Time: </p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px;'>$arrivals</p></td></tr>"
                                                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Date:</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $date</p></td></tr>"
                                                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Number of Guests - (Gender: male): </p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px;'>$male</p></td></tr>"
                                                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Number of Guests - (Gender: female):</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $female</p></td></tr>"
                                                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Number of Rooms:</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $rooms</p></td></tr>"
                                                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Type of Room:</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $type</p></td></tr>"
                                                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Your Name:</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $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 Address:</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $address</p></td></tr>"
                                                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Your Zip:</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $zip</p></td></tr>"
                                                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Your Telephone:</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $telephone</p></td></tr>"
                                                ."<tr><td height='20' width='300'><p style='font-size:20px; color:gray; padding:10px; margin:0px; text-align:right;'>Your Cell:</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $cell</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:</p></td><td height='20' width='300'><p style='font-size:20px; color:gray; padding:0px; margin:10px;'> $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>"
                                                ."<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='url'>Visit Our Site</a></p></td></tr></table>";


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

                    if(!$mail->send()) {
                        header('location: URL/reserveError.html');

                    } else {
                        header('location: URL/reserveResult.html');

                    }

    }

    else {
            header('location: URL/unexpectedReserveError.html');
    }

Basic HTML Form

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
  <link href="favicon.ico" rel="icon" type="image/x-icon" />
  <title>Hotel</title>
  <link rel="stylesheet" type="text/css" href="css/default.css">
  <link rel="stylesheet" type="text/css" href="css/default.date.css">  
  <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>
</head>


<body>

    <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 () {

              $('#reserveForm').validate({ 
                  rules: {
                      name: {
                          required: true
                      },
                      address: {
                          required: true
                      },
                      zip: {
                        required: true
                      },
                      telephone: {
                        required: true
                      },
                      email: {
                        required: true
                      },
                      email_again: {
                        equalTo: "#cemail"
                      },
                      nights: {
                        required: true
                      },
                      arrivals: {
                        required: true
                      },
                      male: {
                        required: true
                      },
                      female: {
                        required: true
                      },
                      rooms: {
                        required: true
                      },
                      type: {
                        required: true
                      },
                      date: {
                        required: true
                      }
                  }
              });
          });
      </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;
}


?>


  <nav class="navbar navbar-default navbar-fixed-top">
    <a class="brand pull-left" href="index.html">
            <img id="Logo" src="./images/logo.gif" alt="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">Los Angeles</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="reserve.php">English</a></p>
        <p class="pull-right language_choice_japanese"><a href="reserve-ja.php"><span lang="ja">日本語</span></a></p>
    </ul>
  </div><!-- /.container-fluid -->
</nav>

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


  <form class="form-horizontal"  method="POST" action="bookInformation.php" id="reserveForm" autocomplete="off">

 <p><a href="rules.html" target="_blank">Rules and Regulations of  Hotel</a></p>

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


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


            <div class="form-group">
              <label for="cnights" class="col-sm-2 control-label">* Number of Nights</label>
              <div class="col-sm-10">
              <select id="cnights" class="form-control" name="nights">                 
                <option value="">Number of Nights</option>
                <option <?php if ($nights == 1 ) echo 'selected' ; ?> value="1">1</option>
                <option <?php if ($nights == 2 ) echo 'selected' ; ?> value="2">2</option>
                <option <?php if ($nights == 3 ) echo 'selected' ; ?> value="3">3</option>
                <option <?php if ($nights == 4 ) echo 'selected' ; ?> value="4">4</option>
                <option <?php if ($nights == 5 ) echo 'selected' ; ?> value="5">5</option>
                <option <?php if ($nights == 6 ) echo 'selected' ; ?> value="6">6</option>
                <option <?php if ($nights == 7 ) echo 'selected' ; ?> value="7">7</option>
                <option <?php if ($nights == 8 ) echo 'selected' ; ?> value="8">8</option>
                <option <?php if ($nights == 9 ) echo 'selected' ; ?> value="9">9</option>
                <option <?php if ($nights == 10 ) echo 'selected' ; ?> value="10">10</option>
              </select>
            </div>
            </div>


            <div class="form-group">
              <label for="carrivals" class="col-sm-2 control-label">* Time of Arrival</label>
               <div class="col-sm-10">
              <select id="carrivals" class="form-control" name="arrivals">
                <option value="">Time of Arrival</option>
                <option <?php if ($arrivals == 14 ) echo 'selected' ; ?> value="14">14</option>
                <option <?php if ($arrivals == 15 ) echo 'selected' ; ?> value="15">15</option>
                <option <?php if ($arrivals == 16 ) echo 'selected' ; ?> value="16">16</option>
                <option <?php if ($arrivals == 17 ) echo 'selected' ; ?> value="17">17</option>
                <option <?php if ($arrivals == 18 ) echo 'selected' ; ?> value="18">18</option>
                <option <?php if ($arrivals == 19 ) echo 'selected' ; ?> value="19">19</option>
                <option <?php if ($arrivals == 20 ) echo 'selected' ; ?> value="20">20</option>
                <option <?php if ($arrivals == 21 ) echo 'selected' ; ?> value="20">21</option>
              </select>
            </div>
            </div>

          <div class="form-group">
            <label for="cdate" class="col-sm-2 control-label">* Check-In Date</label>
              <div class="col-sm-10">
                <input class="datepicker" type="text" placeholder="Please pick a date" id="cdate" name="date">
              </div>
          </div>


           <div class="form-group">
              <label for="cmale" class="col-sm-2 control-label">* Number of People</label>
               <div class="col-sm-10">
              <select id="cmale" class="form-control" name="male">
                <option value="">Gender Male</option>
                <option <?php if ($male === 0 ) echo 'selected' ; ?> value="0">0</option>
                <option <?php if ($male == 1 ) echo 'selected' ; ?> value="1">1</option>
                <option <?php if ($male == 2 ) echo 'selected' ; ?> value="2">2</option>
                <option <?php if ($male == 3 ) echo 'selected' ; ?> value="3">3</option>
                <option <?php if ($male == 4 ) echo 'selected' ; ?> value="4">4</option>
                <option <?php if ($male == 5 ) echo 'selected' ; ?> value="5">5</option>
                <option <?php if ($male == 6 ) echo 'selected' ; ?> value="6">6</option>
                <option <?php if ($male == 7 ) echo 'selected' ; ?> value="7">7</option>
                <option <?php if ($male == 8 ) echo 'selected' ; ?> value="8">8</option>
                <option <?php if ($male == 9 ) echo 'selected' ; ?> value="9">9</option>
                <option <?php if ($male == 10 ) echo 'selected' ; ?> value="10">10</option>
              </select>
            </div>
            </div>


            <div class="form-group">
              <label class="col-sm-2 control-label">* Number of People</label>
               <div class="col-sm-10">
              <select class="form-control" name="female">
                <option value="">Gender Female</option>
                <option <?php if ($female === 0 ) echo 'selected' ; ?> value="0">0</option>
                <option <?php if ($female == 1 ) echo 'selected' ; ?> value="1">1</option>
                <option <?php if ($female == 2 ) echo 'selected' ; ?> value="2">2</option>
                <option <?php if ($female == 3 ) echo 'selected' ; ?> value="3">3</option>
                <option <?php if ($female == 4 ) echo 'selected' ; ?> value="4">4</option>
                <option <?php if ($female == 5 ) echo 'selected' ; ?> value="5">5</option>
                <option <?php if ($female == 6 ) echo 'selected' ; ?> value="6">6</option>
                <option <?php if ($female == 7 ) echo 'selected' ; ?> value="7">7</option>
                <option <?php if ($female == 8 ) echo 'selected' ; ?> value="8">8</option>
                <option <?php if ($female == 9 ) echo 'selected' ; ?> value="9">9</option>
                <option <?php if ($female == 10 ) echo 'selected' ; ?> value="10">10</option>
              </select>
            </div>
            </div>




            <div class="form-group">
              <label class="col-sm-2 control-label">* Number of Rooms</label>
               <div class="col-sm-10">
              <select class="form-control" name="rooms">
                <option value="">Number of Rooms</option>
                <option <?php if ($rooms == 1 ) echo 'selected' ; ?> value="1">1</option>
                <option <?php if ($rooms == 2 ) echo 'selected' ; ?> value="2">2</option>
                <option <?php if ($rooms == 3 ) echo 'selected' ; ?> value="3">3</option>
                <option <?php if ($rooms == 4 ) echo 'selected' ; ?> value="4">4</option>
                <option <?php if ($rooms == 5 ) echo 'selected' ; ?> value="5">5</option>
                <option <?php if ($rooms == 6 ) echo 'selected' ; ?> value="6">6</option>
                <option <?php if ($rooms == 7 ) echo 'selected' ; ?> value="7">7</option>
                <option <?php if ($rooms == 8 ) echo 'selected' ; ?> value="8">8</option>
                <option <?php if ($rooms == 9 ) echo 'selected' ; ?> value="9">9</option>
                <option <?php if ($rooms == 10 ) echo 'selected' ; ?> value="10">10</option>
              </select>
            </div>
            </div>

            <div class="form-group">
              <label class="col-sm-2 control-label">* Type of Room</label>
               <div class="col-sm-10">
              <select class="form-control" name="type">
                <option value="">Type of Room</option>
                <option <?php if ($type == B ) echo 'selected' ; ?> value="B">B</option>
                <option <?php if ($type == C ) echo 'selected' ; ?> value="C">C</option>
                <option <?php if ($type == D ) echo 'selected' ; ?> value="D">D</option>
                <option <?php if ($type == E ) echo 'selected' ; ?> value="E">E</option>
                <option <?php if ($type == F ) echo 'selected' ; ?> value="F">F</option>
                <option <?php if ($type == G ) echo 'selected' ; ?> value="G">G</option>
                <option <?php if ($type == H ) echo 'selected' ; ?> value="H">H</option>
                <option <?php if ($type == I ) echo 'selected' ; ?> value="I">I</option>
                <option <?php if ($type == J ) echo 'selected' ; ?> value="J">J</option>
                <option <?php if ($type == K ) echo 'selected' ; ?> value="K">K</option>
              </select>
            </div>
            </div>

        <div class="form-group">
            <label for="cname" class="col-sm-2 control-label">* Name</label>
             <div class="col-sm-10">
                <input class="form-control" placeholder="Name" id="cname" type="text" name="name" value="<?php if(isset($name)) { echo $name; }?>">
              </div>
        </div>


            <div class="form-group">
                <label for="caddress" class="col-sm-2 control-label">* Address</label>
                 <div class="col-sm-10">
                <input class="form-control" id="caddress" placeholder="Address" type="text" name="address" value="<?php if(isset($address)) { echo $address; }?>">
                </div>
            </div>


            <div class="form-group">
                <label for="czip" class="col-sm-2 control-label">* Zip</label>
                 <div class="col-sm-10">
                <input id="czip" class="form-control" placeholder="Zip" type="text" name="zip" value="<?php if(isset($zip)) { echo $zip; }?>">
              </div>
            </div>


           <div class="form-group">
                <label for="ctelephone" class="col-sm-2 control-label">* Telephone Number</label>
                 <div class="col-sm-10">
                <input id="ctelephone" class="form-control" placeholder="Telephone Number" type="text" name="telephone" value="<?php if(isset($zip)) { echo $zip; }?>">
                </div>
            </div>

            <div class="form-group">
                <label for="ccellphonenumber" class="col-sm-2 control-label">Cell Phone Number</label>
                 <div class="col-sm-10">
                <input id="ccellphonenumber" class="form-control" placeholder="Cell Phone Number" type="text" name="cell" value="<?php if(isset($cell)) { echo $cell; }?>">
              </div>
            </div>



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

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


          <div class="form-group">
            <label class="col-sm-2 control-label">Message</label>
             <div class="col-sm-10">
             <textarea class="form-control" rows="3" name="message" value="<?php if(isset($message)) { echo $message; }?>"></textarea>
           </div>
          </div>


    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default">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="js/picker.js"></script>
<script src="js/picker.date.js"></script>
<script>
$(document).ready(function(){
  $('.datepicker').pickadate();
});
</script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/scripts.js"></script>

</body>
</html>

EDIT

enter image description here**

enter image description here

enter image description here

Apache Environment Differences

Just compared to the hotel's production server as opposed to my test server ... if you look at the Enviromental's variables, you'd see that in the the production server is lacking cookies! What does this mean?

0条回答
登录 后发表回答