2016-06-29 12 views
0

私は、以下のフォームの送信時にチェックしようとしているトークンが、私が送信するために使用するフォームのトークン関数であり、次にトークンをチェックする場所のフォーム処理です。何らかの理由でトークン機能が動作しないのですか?phpトークンのフォーム処理

<?php 
function getToken() 
{ 
    if(!isset($_SESSION['user_token'])) 
    { 
    $_SESSION['user_token'] = md5(uniqid()); 
    } 
} 

function checkToken($token) 
{ 
    if($token != $_SESSION['user_token']) 
    { 
    header('location: 404.php'); 
    exit; 
    } 
} 
function getTokenField() 
{ 
    return '<input type="hidden" name="token" value="'.$_SESSION['user_token'].'" />'; 
} 
function destroyToken() 
{ 
    unset($_SESSION['user_token']); 
} 

registerscript.phpページにリダイレクトするregister.phpという名前の登録フォーム。私はそれがトークンをチェックしたい提出に取り扱うフォームで

<?php getToken(); ?> 
<form method="post" action="registerscript.php"> 
<fieldset> 
    <legend>Registration Form</legend> 
    <table width="400" border="0" cellpadding="10" cellspacing="10"> 
     <tr> 
      <td></td> 
      <td style="font-weight: bold;color:red"> 

      <?php 
       if(isset($_SESSION['error'])) 
       { 
        echo '<p>'.$_SESSION['error']['firstname'].'</p>'; 
        echo '<p>'.$_SESSION['error']['lastname'].'</p>'; 
        echo '<p>'.$_SESSION['error']['username'].'</p>'; 
        echo '<p>'.$_SESSION['error']['email'].'</p>'; 
        echo '<p>'.$_SESSION['error']['password'].'</p>'; 
        echo '<p>'.$_SESSION['error']['country'].'</p>'; 
        unset($_SESSION['error']); 
       } 
      ?> 

      </td> 
     <tr> 
      <td style="font-weight: bold"><div align="right"><label for="firstname">First Name</label></div></td> 
      <td><input name="firstname" type="text" class="input" size="25" required /></td> 
     </tr>   <tr> 
      <td style="font-weight: bold"><div align="right"><label for="lastname">Last Name</label></div></td> 
      <td><input name="lastname" type="text" class="input" size="25" required /></td> 
     </tr>   <tr> 
      <td style="font-weight: bold"><div align="right"><label for="username">User Name</label></div></td> 
      <td><input name="username" type="text" class="input" size="25" required /></td> 
     </tr>   
     <tr> 
      <td style="font-weight: bold"><div align="right"><label for="password">Password</label></div></td> 
      <td><input name="password" type="password" class="input" size="25" required /></td> 
     </tr> 
     <tr> 
      <td style="font-weight: bold"><div align="right"><label for="confirmpw">Confirm Password</label></div></td> 
      <td><input name="confirmpw" type="password" class="input" size="25" required /></td> 
     </tr> 
     <tr> 
      <td style="font-weight: bold"><div align="right"><label for="email">Email</label></div></td> 
      <td><input name="email" type="email" class="input" size="25" required /></td> 
     </tr> 
     <tr> 
      <td style="font-weight: bold"><div align="right"><label for="country">Country</label></td> 
      <td><select name="country"> 
       <option selected value="0">Select Country</option> 
        <?php 
          while ($rowCerts = $resultcategories->fetch_assoc()) { 
          echo "<option value=\"{$rowCerts['country']}\">"; 
          echo $rowCerts['country']; 
          echo "</option>"; 
          } 
        ?> 

      </select></td> 
     </tr> 
     <tr> 
      <td height="23"></td> 
      <td><div align="right"> 
      <input type="submit" name="submit" value="Register!" /> 
      <?php echo getTokenField(); ?> 

は、次のコードで有効ですが、何のトークンが存在しないかのように、それは404.phpに私を取るが、私は、ページのソースを表示する場合がありますトークン

  <?php 
       session_start(); 
       include_once('conn.php'); 
       if(isset($_POST['submit'])) 
       { 
        checkToken($token); 
        $firstname = $_POST["firstname"]; 
        $lastname = $_POST["lastname"]; 
        $username = $_POST["username"]; 
        $password = $_POST["password"]; 
        $confirmpw = $_POST["confirmpw"]; 
        $country = $_POST["country"]; 

        if(preg_match("/[^a-zA-Z0-9]+/", $firstname)) 
         { 
          $_SESSION['error']['firstname'] = "Incorrect characters used in Firstname.<br>"; 
         } 
       } 

私のソースコードは示しています

  <input type="submit" name="submit" value="Register!" /> 
      <input type="hidden" name="token" value="f0de3e58dbd6196ac0f964203577abb0" />    </div></td> 
     </tr> 
    </table> 
</fieldset> 
+0

PHPスクリプトの先頭でセッションを開始しましたか? –

答えて

0

は、この時点では$token設定されていますか?

if(isset($_POST['submit'])) 
{ 
    checkToken($token); 
} 

それとも、前の回答が正しい可能性が高い

if(isset($_POST['submit'])) 
{ 
    checkToken($_POST['token']); 
} 
+0

登録フォームのソースを確認すると、生成されたトークンがあるので、それをissetと仮定しますか? – nathzOO

0

でなければなりません。 conn.phpファイルの中には何がありますか?ライン

header('location: 404.php'); 

echo "--$token--$_SESSION[user_token]--";die; 

、あなたは何を得るかと言う:

は、このような文字列を追加してください。

+0

これは、register.phpからregisterscript.phpへのリダイレクトがセッショントークンが異なるためですか? – nathzOO

+0

私は---- f0de3e58dbd6196ac0f964203577abb0--を取得しました。if($ token!= $ _SESSION ['user_token']) – nathzOO

+0

あなたの$ _SESSION ["user_token"]は空です。 register.phpとregisterscript.phpは同じです。ドメインをwww.domain.comからdomain.comのように変更しないでください。 – Vasya