I'm working on a PHP login page. The first page shows two forms, a login and register form and does some form verification. When a user logs in it reads their information out of a text file. The information is listed as
username;password;fname;lname;pagetitle;bgcolor;imgURL
with the end of line occurring after the image. This info then populates a form on the next page so that the user can edit it if they so choose. I have no idea though how to read the line from the file when the user submits the changes and then write it back to the file. I also need to know how I can change the bgcolor and imgURL when these changes are submitted.
My current code is listed below.
<?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>