Jump to content

PHP File Deletion Problem


MarkSill

Recommended Posts

Hi everyone,
 
Lately, in a school project I've been working on, which involves using XML files to save information and read it out, as well as dynamically create and delete themselves. It's all been working just fine, until quite recently (today) when I noticed that two XML files had not deleted themselves, like they were supposed to. I went in and deleted them manually, but now they will display themselves whenever I try to display a different XML file with the same identification. Here's my XML file template that they copy from:

<?xml version="1.0" encoding="ISO-8859-1"?>
<msg>
	<to>template</to>
	<toid>0</toid>
	<from>template</from>
	<fromid>0</fromid>
	<title>template</title>
	<text>template</text>
	<archived>false</archived>
	<replyto>N/A</replyto>
	<id>0</id>
</msg>

And here's my display code (Whole webpage in this case):

<!DOCTYPE HTML>
<html>
	<script src="xml.js"></script>
	<script>
		function loadmsg() {
			m = document.getElementById("messageid");
			f = document.getElementById("from");
			t = document.getElementById("title");
			tt = document.getElementById("text");
			p = document.getElementById("everything");
			if (m.innerHTML != "") {
				if (localStorage.name == loadXMLDoc("msg"+m.innerHTML+".xml").getElementsByTagName("to")[0].childNodes[0].nodeValue) {
					p.removeChild(document.getElementById("msgloadthing"));
					x = loadXMLDoc("msg"+m.innerHTML+".xml");
					f.innerHTML = x.getElementsByTagName("from")[0].childNodes[0].nodeValue;
					t.innerHTML = x.getElementsByTagName("title")[0].childNodes[0].nodeValue;
					tt.innerHTML = x.getElementsByTagName("text")[0].childNodes[0].nodeValue;
				}
				else {
					
				}
			}
			else if (m.innerHTML == "") {
				
			}
			m.innerHTML = "";
		}
	</script>
	<noscript>
		
	</noscript>
	<head>
		<link rel="stylesheet" type="text/css" href="main.css"/>
		<title>Messages</title>
	</head>
	<body>
		<div id="everything">
		<script src="header.js"></script>
			<div id="header">
				Database Testing
			</div>
			<div id="sidebar">
				<a href="index.php">Home</a><br/>
				<a href="login.php">Login</a><br/>
				<a href="create.php">Create an Account</a><br/>
				<a href="userhome.php">User Home</a><br/>
			</div>
			<div id="msgloadthing">
				<?php
					echo "<em>If messages aren't appearing, try logging back in.";
					//load all messages that the current user has access to
					$num = 1;
					$class = false;
					while (file_exists("msg".$num.".xml")) {
						$sxe = new DOMDocument('1.0', 'utf-8');
						$sxe->formatOutput = true;
						$sxe->preserveWhiteSpace = false;
						$sxe->load("msg".$num.".xml");
						$msg = $sxe->getElementsByTagName("msg")->item(0);
						$from = $msg->getElementsByTagName("from")->item(0)->nodeValue;
						$title = $msg->getElementsByTagName("title")->item(0)->nodeValue;
						$to = $msg->getElementsByTagName("to")->item(0)->nodeValue;
						$fromid = $msg->getElementsByTagName("fromid")->item(0)->nodeValue;
						if ($_COOKIE["username"] == $to) {
							if ($class == false) {
								echo "<div class='t1'><a href='user.php?id=".$fromid."'>".$from."</a> - <a href='messages.php?id=".$num."'>".$title."</a></div>";
								$class = true;
							}
							else if ($class == true) {
								echo "<div class='t2'><a href='user.php?id=".$fromid."'>".$from."</a> - <a href='messages.php?id=".$num."'>".$title."</a></div>";
								$class = false;
							}
						}
						$num = $num + 1;
					}
				?>
			</div>
			<div id="messagecontent">
				<?php
				if (!(file_exists("msg".$_GET["id"].".xml")) and $_GET["id"] != "") {
					echo "<em><strong>ERROR: Message does not exist.</strong></em>";
				}
				if ($_GET["id"] != "") {
					echo '<div id="messageid">'.$_GET["id"].'</div>';
					echo "<a href='deletemsg.php?id=".$_GET["id"]."'>Delete</a>";
				}
				else {
					echo '<div id="messageid">'.$_GET["id"].'</div>';
				}
				?>
				<script>
					if (document.getElementById("messageid").innerHTML != "") {
						m = loadXMLDoc("msg"+document.getElementById("messageid").innerHTML+".xml");
						document.write("<a href='user.php?id="+m.getElementsByTagName("fromid")[0].childNodes[0].nodeValue+"'><div id='from'></div></a>");
					}
				</script>
				<div id="title"></div>
				<div id="text"></div>
				<script>
					loadmsg();
				</script>
			</div>
			<div id="footer">
				<em>© 2013 [Redacted]</em>
			</div>
		</div>
	</body>
</html>

And the creation code:
 

<!DOCTYPE HTML>
<html>
	<script>
		//JS code goes here
	</script>
	<noscript>
		
	</noscript>
	<head>
		<link rel="stylesheet" type="text/css" href="main.css"/>
		<title>Send Message</title>
	</head>
	<body>
		<div id="everything">
		<script src="header.js"></script>
			<div id="header">
				Database Testing
			</div>
			<div id="sidebar">
				<a href="index.php">Home</a><br/>
				<a href="login.php">Login</a><br/>
				<a href="create.php">Create an Account</a><br/>
				<a href="userhome.php">User Home</a><br/>
				
			</div>
			<div id="content">	
				<?php
					$num=1;
					$num1 = 1;
					while (file_exists("msg" . $num . ".xml") == 1) {
						if (file_exists("msg" . $num . ".xml") == 1) {
							$num = $num + 1;
						}
					}
					/*if (file_exists("msg" . $num . ".xml") == 0) {
						$num = $num - 1;
					}*/
					copy("template.xml", "msg".$num.".xml");
					while (file_exists("user".$num1.".xml")) {
						$usrr = new DOMDocument('1.0', 'utf-8');
						$usrr->formatOutput = true;
						$usrr->preserveWhiteSpace = false;
						$usrr->load("user".$num1.".xml");
						$usr = $usrr->getElementsByTagName("user")->item(0);
						$name = $usr->getElementsByTagName("name")->item(0);
						if ($name->nodeValue == $_COOKIE["username"]) {
							$ffrom = $usrr->getElementsByTagName("id")->item(0);
							break;
						}
						$num1 = $num1 + 1;
					}
					$usrrr = new DOMDocument('1.0', 'utf-8');
					$usrrr->formatOutput = true;
					$usrrr->preserveWhiteSpace = false;
					$usrrr->load("user".$_COOKIE["id"].".xml");
					$nusr = $usrrr->getElementsByTagName("user")->item(0);
					$tto = $nusr->getElementsByTagName("name")->item(0);
					
					$sxe = new DOMDocument('1.0', 'utf-8');
					$sxe->formatOutput = true;
					$sxe->preserveWhiteSpace = false;
					$sxe->load("msg".$num.".xml");
					$msg = $sxe->getElementsByTagName("msg")->item(0);
					$to = $msg->getElementsByTagName("to")->item(0);
					$toid = $msg->getElementsByTagName("toid")->item(0);
					$from = $msg->getElementsByTagName("from")->item(0);
					$fromid = $msg->getElementsByTagName("fromid")->item(0);
					$title = $msg->getElementsByTagName("title")->item(0);
					$text = $msg->getElementsByTagName("text")->item(0);
					$id = $msg->getElementsByTagName("id")->item(0);
					$to->nodeValue = $tto->nodeValue;
					$toid->nodeValue = $_COOKIE["id"];
					$from->nodeValue = $_COOKIE["username"];
					$fromid->nodeValue = $ffrom->nodeValue;
					$title->nodeValue = $_POST["title"];
					$text->nodeValue = $_POST["text"];
					$id->nodeValue = $num;
					$sxe->save("msg".$num.".xml");
					echo "<em>Message sent successfully.</em>";
				?>
				<form action="userhome.php">
					<input type="submit" value="Continue"/>
				</form>
			</div>
			<div id="footer">
				<em>© 2013 [Redacted]</em>
			</div>
		</div>
	</body>
</html>

And last, but not least, the deletion code:

<?php
	header("Location: messages.php?id=");
	unlink("msg".$_GET["id"].".xml");
	exit;
?>

I'm fully aware that the website is not built in the best way, so over the summer I'm planning on fixing a lot of it. I just can't seem to figure this out though. Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.