hello everybody,
I wish to create a script but I don't particularly know how to approach it.
I have a whole user system setup with different usernames and passwords and everything. I want to create a page that allows users to delete their accounts. Sounds not too hard....
The setup for all this is that I have the usernames, passwords, and a few other pieces of information stored in a flat-file called users.php. When a user logs in, it starts a session with their information at hand. I feel like the way to approach it would be to create a script that loops through the flat-file looking for the line that has that persons username in it, then to delete that line from the file. I have a way for myself to do it using links, but I haven't yet figured out a way to do it with specific users. For reference, my personal delUser.php looks like:
<?php
session_start(); // Maintain session state
header("Cache-control: private"); // Fixes IE6's back button problem.
/********************BASED ON A SCRIPT BY MIKE HOLLOWAY ************************************/
// Check that we are logged in and an admin
if(@$_SESSION["permission"] > 1){
// Check if we have a number to mod
if(@$_GET["del"]){
// Check if we have already made changes and want
// to post
if(@$_GET["go"]){
// Get the line number we are changing
$id = $_GET["del"];
// Dont allow the first user to be deleted
if($id == 1){
// Redirect to del page
header("location: delUser.php?cantDelete=1");
}
else{
// Get the original vars
$file = file("users.php");
// Create the new file structure
#1. before line
for($i = 0; $i < $id; $i++){
$oldLines[$i] = $file[$i];
}
#2. after line
for($i = ($id +1); $i < sizeof($file); $i++){
$oldLines[$i] = $file[$i];
}
// Replace users file
$fp = fopen("users.php", "w");
// Add contents
for($i = 0; $i < sizeof($file); $i++){
if($i != $id) fwrite($fp, $oldLines[$i]);
else fwrite($fp, "//\r\n");
}
// Close file
fclose($fp);
// Redirect to del page
header("location: delUser.php?deleted=1");
}
}
?>
<script language = "javascript" type = "text/javascript">
<!-- // Confirm user wants to delete this user
if(confirm("Are you sure that you want to delete this user?\nThis action cannot be undone.") == 1) location.href = '<?php print($_SERVER["PHP_SELF"]. "?del=". $_GET["del"]."&go=1");?>';
else location.href = 'delUser.php';
-->
</script>
<?php
}
else{
?>
<html>
<head>
<link rel = "stylesheet" type = "text/css" href = "/css/">
<title>GreatVibrations | Delete Member</title>
</head>
<body>
<table border = "0" cellspacing = "0" cellpadding = "0" width = "80%" align = "center" style = "height: 100%;">
<tr>
<td height = "15%">
<!--// Spacer cell -->
</td>
</tr>
<tr>
<td valign = "top" style = "border: 3px double #888888;">
<table border = "0" cellspacing = "0" cellpadding = "10" width = "100%" align = "center">
<tr>
<td width = "60%" valign = "top" style = "border-bottom: 1px solid #888888; background: #f0f0f0;">
<span class = "bold">Delete a user</span>
<?php
// Display deleted message if applicable
if(@$_GET["deleted"]) echo " | <span class = 'alert'>Delete action performed successfully</span>";
elseif(@$_GET["cantDelete"]) echo " | <span class = 'alert'>Delete action cannot be performed on this user</span>";
?>
</td>
<td width = "40%" align = "right" style = "border-bottom: 1px solid #888888; background: #f0f0f0;">
<?php
// Print out admin links
if($_SESSION['permission'] > 1){
?>
<a href = "modUser.php">mod user</a> |
<a href = "userarea.php">back</a> |
<?php
}
?>
<a href = "logout.php">logout</a>
</td>
</tr>
<tr>
<td colspan = "2" valign = "top" style = "padding: 20;">
<?php
// Get all users from file
$file = file("users.php");
// Get amount of lines in file
$totalLines = sizeof($file);
// Loop through and display all users
for($line = 0; $line < $totalLines; $line++){
// Dont print out lines beginning with a comment
if("//" != substr($file[$line], 0, 2)){
// Put contents of line into variables
@list($username, $password, $perm, $email, $url, $dob, $location, $joined) = explode("<del>", $file[$line]);
$userString = "<a href = '".$_SERVER['PHP_SELF']."?del=".$line."'>".ucfirst($username). "</a><br><span style = 'color: #cccccc;'>";
$string = $userString;
$string .= "password (md5()): ". $password. "<br>";
$string .= "permission: ". $perm. "<br>";
$string .= "email: ". $email. "<br>";
$string .= "url: ". $url. "<br>";
$string .= "date of birth: ". $dob. "<br>";
$string .= "location: ". $location. "<br>";
$string .= "joined: ". $joined. "</span><br><br>";
echo $string;
}
}
?>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height = "15%">
</td>
</tr>
</table>
</body>
</html>
<?php
}
}
else header("location: index.php?fail=1");
?>
Can somebody give me some sort of guidance on how to go about this?
--
Thanks,
Nay