如何匹配文件的特定信息进行登录?

时间:2013-11-09 08:29:35

标签: php forms validation file-io

有一个问题,我无法找到解决方案。在继续我的问题之前,只需简要概述一下代码的作用(这是一个开始php课程中的类项目)。用户自动在index.php页面上启动。一旦数量被输入,它将把它们带到login.php页面。如果他们有登录信息,则会将他们带到invoice.php页面。如果没有,那么它将把它们带到register.php页面,然后转到invoice.php页面。

这是我遇到问题的地方,如何将我的user.dat文件中的用户名和密码(从用户注册页面输入)与登录页面相匹配?这令人沮丧,因为我们真的没有在课堂上讨论它:(

任何帮助将不胜感激,随时给出反馈任何代码:)

非常感谢你们!这个网站肯定缓解了一些编码难题:P

的login.php

<h3>
    <center>
        Please Login
    </center>
</h3>
<?php
include "functions.inc";

//if the form data is clicked... if all valid.. display invoice... otherwise display error
$datafile = "users.dat";

$file = file_get_contents($datafile);
if(!strpos($file, "search string")) {
echo "String not found!";
}

   if (array_key_exists('submit', $_POST))
   {
     if(!strpos($file, "search string")) {
echo "String not found!";
}
      header('Location: registration.php');

   }

   else 
       if (array_key_exists('register', $_POST))
   {

      header('Location: invoice.php');
   }


?>








<?php//made the user login menu into a nice table that will center the username and password in the middle of the page.?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
    <form name="form1" method="post" action="invoice.php">
        <tr>
        <td>Username</td>
        <td>:</td>';    
        <td width="294"><input name="myusername" type="text" id="myusername"></td>
        </tr>
        <tr>
        <td>Password</td>
        <td>:</td>
        <td><input name="mypassword" type="text" id="mypassword"></td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
</table>
        <center><input type='submit' name='submit' value='Login'></center>
        </form>

        <?php
/*This code will allow a new user to go to the registration page and register for the site
 * before buying anything.
 */
        ?>
    <br>   
    <form name="form1" method="post" action="registration.php">
        <center><input type='submit' name='Register' value='New User? Click here.'></center>
        </form>

为registration.php

<html>
    <h4>
        <center>
            New User Registration
        </center>
    </h4>
    <body>

        <?php
        /*
         * What this code does, is it takes the users registration information, and stores it in a file.
         * After that what I hope to do is to retrive the same file when the user is on the login page and if 
         * the login matches whats in the file, then proceed to the invoice page.
         */
include "functions.inc";

    // Define the datafile to hold the $users array
    $datafile = "users.dat";

    // See if the user has submitted something
    if (array_key_exists('register', $_POST))
    {
        // Get the new user info
            $the_user = $_POST['newUser'];


// Load the file of users and store it in $users
        $users = arrayfile_to_array($datafile);


        // Validate user name and password
        if (user_exists($the_user['ID'], $users))
        {
            echo "<p><center>Please fill in all text boxes</center></p>";
        }
        else
        {
            // If valid, save to the file of users
            $users[] = $the_user;
            array_to_arrayfile($users, $datafile);
        }
    }
    else
    {
        if (!file_exists($datafile))  // Data file doesn't exist, so create it
        {
            $users = array();
            array_to_arrayfile($users, $datafile);
        }
    }
?>

        <?php
       // my defined error values 
      $errEmail    = "";
      $errUser     = "";
      $errPass     = "";


      if(isset($_POST["register"])){

        // User must be digits and letters
        if(preg_match("/^[0-9a-zA-Z]{5,}$/", $_POST['newUser']['ID']) === 0)
          $errUser = '<span class="error">Username must be more than 5 characters and contain letters and numbers.</span>';

        // Password must be strong
        if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST['newUser']['password']) === 0)
          $errPass = '<span class="error">Password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit</span>';

        //Email validation
        if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST['newUser']['email']) === 0)
          $errEmail = '<span class="error">example: chars(.chars)@chars(.chars).chars(2-4)</span>';
      }  
  ?>

        <form action = "<?= $_SERVER['PHP_SELF'] ?>" method= 'POST'> 
            <center>
                <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
                    <tr>
                        <td>Username</td>
                        <td>:</td>
                        <td ><input name="newUser[ID]" type="text" size="16" value="">
          <?php  if(isset($errUser) and $errUser !='') echo $errUser; ?>
                        </td >
                    </tr>
                     <tr>
                        <td>Password</td>
                        <td>:</td>
                        <td><input name="newUser[password]" type="password" size="16" value="">
          <?php  if(isset($errPass) and $errPass !='') echo $errPass; ?>
                        </td >
                    </tr>
                                         <tr>
                        <td>Email</td>
                        <td>:</td>
                        <td><input name="newUser[email]" type="text" size="50" value="">
          <?php  if(isset($errEmail) and $errEmail !='') echo $errEmail; ?>
                        </td >
                    </tr> 
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td> 
                    </tr>          
                </table>
                <input type='submit' name='register' value='Register'>
            </center>    
        </form>
    </body>
</html>

0 个答案:

没有答案