有一个问题,我找不到解决方案。在我继续我的问题之前,只是简要概述一下代码应该做什么(这是针对初级 php 课程中的一个类项目)。用户在 index.php 页面上自动启动。输入数量后,它会将它们带到 login.php 页面。如果他们有登录名,那么它会将他们带到 invoice.php 页面。如果没有,那么它会将他们带到 register.php 页面,然后转到 invoice.php 页面。
这是我遇到问题的地方,如何将 user.dat 文件中的用户名和密码(从用户注册页面输入)匹配到登录页面?这很令人沮丧,因为我们真的没有在课堂上复习它:(
任何帮助将不胜感激,请随时对所提供的任何代码提供反馈:)
非常感谢你们!这个网站确实缓解了一些编码难题:P
登录.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> </td>
<td> </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>
注册.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> </td>
<td> </td>
</tr>
</table>
<input type='submit' name='register' value='Register'>
</center>
</form>
</body>
</html>