0

我是 php 新手,正在努力学习它。我正在通过执行 php 代码来练习,以便当用户在登录屏幕上并使用用户名和密码登录时,当他们单击登录按钮时,页面将检查 .txt 文件以查看用户名是否并且密码存在于 .txt 文件中,否则将显示并要求用户注册。

当用户点击注册时,它会将用户重定向到注册页面。完成注册表并用户点击提交后,它会将用户写入 .txt 文件。我还希望它检查密码是否为最小值和最大值。

我正在玩的登录页面代码是这样的:

// Useful variables
$datafile = "users.txt";

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

   if (array_key_exists('submit_btn', $_POST))
   {
     if(!strpos($file, "search string")) {
echo "String not found!";
}

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

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

      header('Location: 03registration.php');
   }


?>
<!DOCTYPE html>
<html> <center>
    <style>
        body
        {
            background-color:aqua;
            font-family:"Times New Roman";
            font-size:20px;
        }
        h1
        {
            color:orange;
            text-align: center;
        }
        p
        {
            font-size:24px;
        }
        table
        {
            background-color:green;
            font-family:"Helvetica";
            font-size:20px;
            font-weight:bold;
        }
    </style>
    <h1>
        Welcome to the Pay'N'Go Kiosk
    </h1>
    <body>
        <form action= "<?php $_SERVER["PHP_SELF"] ?>" method="POST">
            <p>Username:    <br>
<input style="background-color: yellow; color:black" type='text' name='username'
                              value='<?php if(isset($_POST['username'])) 
        echo $_POST['username'] ?>'>
                              <BR>
            Password:   <br>
<input style="background-color:magenta" type='password' name='userpass'><BR>


            <input type='submit' name='submit_btn' value='Sign In'>
            <br/>
            Not a user? Sign up today! </br>
            <input type='submit' name='register' value='Sign Up'>
        </form>
    </body>
</center>
</html>

我的注册页面代码是:

<center>
<form action = '<?= $_SERVER['PHP_SELF'] ?>' method= 'POST'> 
    Username:   <br>

    <INPUT TYPE="TEXT"  name="username" value = "<?php     
    if(isset($_POST['username'])) 
        echo $_POST['username'] ?>"><br>
Password: <br>

        <INPUT TYPE="password" name = 'password'>
<?php
    if (isset($errors['password_short'])) 
        echo " <font color='red'>{$errors['password_short']}</font>";
    if (isset($errors['password_long']))  
        echo " <font color='red'>{$errors['password_long']}</font>"; 
        ?>

    <br><br>
<input type='submit' name='submit' value='Sign Up'>
</form>

<?php

   if (array_key_exists('submit', $_POST))
   {
   //code provided by professor Kazman
// Define constants for the shortest and longeste passwords
define('MIN_PASS_LEN', 8);    
define('MAX_PASS_LEN', 20);

// Function to check the password length
function check_pass($pword) 
{
    global $errors;
    if (strlen($pword) < MIN_PASS_LEN ) 
        $errors['password_short'] = 'Enter a longer password';    
    if (strlen($pword) > MAX_PASS_LEN ) 
        $errors['password_long'] = 'Enter a shorter password';
}


$errors = array();

// If the user pressed Submit, check the username and password
if (array_key_exists('submit', $_POST)) 
{
    check_pass($_POST['password']);   
    if (count($errors) == 0 && $_POST['username'] == $username 
        && $_POST['password'] == $password) 
    {
        die('correct!!');
    }   
}    
   }

 else               

                {
$username = $_POST['username'];
$password = $_POST['password'];
 //the data
$data = "$username | $password\n";

//open the file and choose the mode
$fh = fopen("users.txt", "a");

fwrite($fh, $data); //close the file fclose($fh); 
//close the file

fclose($fh);
   }
?>

</center>

在阅读了无数博客和论坛后,我知道 .txt 不是解决此问题的最佳方式,但我这样做是出于教育目的。在此先感谢您的帮助,我们将不胜感激。

4

0 回答 0