我正在开发一个PHP登录页面。第一页显示两种形式,登录和注册表单,并进行一些表单验证。当用户登录时,它会从文本文件中读取信息。信息列为
username;password;fname;lname;pagetitle;bgcolor;imgURL
在图像之后出现行尾。然后,此信息将填充下一页上的表单,以便用户可以选择进行编辑。我不知道如何在用户提交更改时从文件中读取行,然后将其写回文件。我还需要知道如何在提交这些更改时更改bgcolor和imgURL。
我目前的代码如下所示。
<?php
session_start();
$bkcolor = "red";
if(isset($_GET['exit'])){
session_unset();
session_destroy();
}
if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
?>
<h1>Welcome to First Last's Assignment 11 PHP page!</h1>
<?php
echo"Hello, ".$_SESSION['fname']. " " .$_SESSION['lname']."!";
//echo $_SESSION['image'];
?>
<form name = "logoutForm" action = "assignment11.php" method = "get">
<input type = "hidden" name = "logout" value = "doLogout"><br>
<input type = "submit" name = "exit" value = "Logout" />
</form></div>
<br><hr><br>
<form name = "editAccountForm" action = "assignment11.php" method = "get">
First name:<input type = "text" name = "fname" value = "<?php echo" $_SESSION['fname']; ?>" /><br>
Last name:<input type = "text" name = "lname" value = "<?php echo $_SESSION['lname']; ?>" /><br>
Title:<input type = "text" name = "title" value = "<?php echo $_SESSION['title']; ?>" /><br>
<input type = "hidden" name = "edit" value = "true">
<input type = "submit" name = "edit1" value = "Edit Account Info" />
</form>
<?php
}else{
?>
<head><title>Welcome to First Last's Assignment 11 PHP page!</title></head>
<body bgcolor = "<?php echo $bkcolor; ?>" >
<h1>Welcome to First Last's Assignment 11 PHP page!</h1>
<br>
<div id = "login">
If you have an existing account login here:<br>
<form method = "get" action = "assignment11.php" name = "loginForm">
Username: <input type = "text" name = "user1" /><br>
Password: <input type = "password" name = "pass1" /><br>
<input type = "submit" name = "login" value = "Login!" />
</form></div>
<br><hr><br>
<div id = "create">
If you'd like to create an account login here:<br>
<form method = "get" action = "assignment11.php" name = "createForm">
Username: <input type = "text" name = "user" /><br>
Password: <input type = "password" name = "pass" /><br>
First Name: <input type = "text" name = "first" /><br>
Last Name: <input type = "text" name = "last" /><br>
<input type = "hidden" name = "create" value = "true" />
<input type = "submit" name = "register" value = "Create Account!" />
<p id = "incorrect"></p>
</form></div>
<?php
}
$image = '<img src = "http://upload.wikimedia.org/wikipedia/commons/thumb /9/94/Stick_Figure.svg/170px-Stick_Figure.svg.png />';
//$bkcolor = "yellow";
$myfile = fopen("assignment11-account-info.txt", "a+");
$file = fread($myfile,filesize("assignment11-account-info.txt"));
$title = "Welcome to First Last's Assignment 11 PHP page!";
$result = file_get_contents("assignment11-account-info.txt");
$result = str_replace(PHP_EOL,";",$result);
$result = explode(";",$result);
if(isset($_GET['register'])){
$user = $_GET['user'];
$password = $_GET['pass'];
$first = $_GET['first'];
$last = $_GET['last'];
$found = false;
if(empty($user)){
echo"Enter a username.";
}else{
$user = $user;
}
if(empty($password)){
echo"Enter a password.";
}
else{
$password = $password;
}
if(empty($first)){
echo"Enter first name.";
}
else{
$first = $first;
}
if(empty($last)){
echo"Enter last name.";
}
else{
$last = $last;
}
// $found = true;
for($i = 0; $i < count($result); $i++){
if($result[$i] === $user){
$found = true;
echo"Username unavailable";
break;
}
}
if($found == false){
$_SESSION['username'] = $user;
$_SESSION['password'] = $password;
$_SESSION['fname'] = $_GET['first'];
$_SESSION['lname'] = $_GET['last'];
$_SESSION['title'] = $title;
echo "HERE!";
fwrite($myfile,$user.";".$password.";".$first.";".$last.";".$bkcolor.";".$title.";".$image.PHP_EOL);
echo"<script>document.getElementById('login').style.display = 'none'</script>";
echo"<script>document.getElementById('create').style.display = 'none'</script>";
?>
<form name = "logoutForm" action = "assignment11.php" method = "get">
<input type = "hidden" name = "logout" value = "doLogout"><br>
<input type = "submit" name = "exit" value = "Logout" />
<form name = "editAccountForm" action = "assignment11.php" method = "get">
First name:<input type = "text" name = "fname" value = "<?php echo $_SESSION['fname']; ?>" /><br>
Last name:<input type = "text" name = "lname" value = "<?php echo $_SESSION['lname']; ?>" /><br>
Title:<input type = "text" name = "title" value = "<?php echo $title; ?>" /><br>
Background color:<input type = "text" name = "bkcolor" value = "<?php echo $bkcolor; ?>" /><br>
Image:<input type = "text" name = "image" size = "100" value = "testing" /><br>
<input type = "hidden" name = "edit" value = "true">
<input type = "submit" name = "edit1" value = "Edit Account Info" />
</form>
<?php
}
}
if(isset($_GET['login'])){
$user = $_GET['user1'];
$password = $_GET['pass1'];
// $image = '<img src = "http://upload.wikimedia.org/wikipedia/commons/thumb/9/94/Stick_Figure.svg/170px-Stick_Figure.svg.png />';
// echo $image;
for($i = 0; $i < count($result); $i++){
if($result[$i] === $user && $result[$i+1] === $password){
echo"<script>document.getElementById('login').style.display = 'none'</script>";
echo"<script>document.getElementById('create').style.display = 'none'</script>";
$_SESSION['username'] = $user;
$_SESSION['password'] = $password;
$_SESSION['fname'] = $result[$i+2];
$_SESSION['lname'] = $result[$i+3];
$_SESSION['title'] = $title;
// $_SESSION['image'] = $image;
$_SESSION['loggedin'] = true;
echo"Hello, ".$_SESSION['fname']." ".$_SESSION['lname']."!";
// echo $_SESSION['image'];
?>
<form name = "logoutForm" action = "assignment11.php" method = "get">
<input type = "hidden" name = "logout" value = "doLogout"><br>
<input type = "submit" name = "exit" value = "Logout" />
</form></div>
<br><hr><br>
<form name = "editAccountForm" action = "assignment11.php" method = "get">
First name:<input type = "text" name = "fname" value = "<?php echo $_SESSION['fname']; ?>" /><br>
Last name:<input type = "text" name = "lname" value = "<?php echo $_SESSION['lname']; ?>" /><br>
Title:<input type = "text" name = "title" value = "<?php echo $title; ?>" /><br>
Background color:<input type = "text" name = "bkcolor" value = "<?php echo $bkcolor; ?>" /><br>
Image:<input type = "text" name = "image" size = "100" value = "testing" /><br>
<input type = "hidden" name = "edit" value = "true">
<input type = "submit" name = "edit1" value = "Edit Account Info" />
</form>
<?php
}
else if($result[$i] === $user && $result[$i+1] !== $password){
echo"Incorrect password!";
}
/* else if($result[$i] !== $user){
echo"Invalid username. Have you registered?";
break;
}*/
}
}
if(isset($_GET['exit'])){
session_unset();
session_destroy();
}
if(isset($_GET['edit1'])){
for($i = 0; $i < count($result); $i++){
if($result[$i] == $_SESSION['username']){
$_SESSION['username'].";".$_SESSION['password'].";".$_GET['fname'].";".$_GET['lname'].";".$_GET['title'].";".$_GET['bkcolor'].";".$_GET['image'].PHP_EOL);
}
}
}
// print_r($_SESSION);
?>
</body>
</html>