ok, so i have been trying to edit a flat file with something like a visitor log that tracks some info about the user and saves it to log.txt this file looks something like
and i wrote some script to read the file and edit the file. view.php simply just reads the file and outputs it through a HTML table for view. Right now edit_data.php takes the array and makes it so that it is all displayed in a form field for each entry (i would like to change it so that i can simply have a link to click on the view page so that i can edit a specific entry.)
reader(view.php)
Data Editor (edit_data.php)
so whats wrong with my script? it won't really do anything, i'm not sure but is it something to do with the way i am trying to explode '/n'?? also if you could give me a help with making the edit link so that i can edit one entry at a time. if anyone could try to help me i would greatly appreciate it.
Code:
time|agent|IP|referer
time|agent|IP|referer
07-15-03 01:45:30|Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)|123.213.123.321|[URL unfurl="true"]http://www.site.com[/URL]
07-15-03 01:45:31|Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)|123.213.123.321|[URL unfurl="true"]http://www.site.com[/URL]
and i wrote some script to read the file and edit the file. view.php simply just reads the file and outputs it through a HTML table for view. Right now edit_data.php takes the array and makes it so that it is all displayed in a form field for each entry (i would like to change it so that i can simply have a link to click on the view page so that i can edit a specific entry.)
reader(view.php)
Code:
<?PHP
/* ( GET FILE CONTENTS )----------------------------------------------- */
$file = 'log.txt';
$fhandle = fopen( $file, "r" );
$contents = fread( $fhandle, filesize ( $file ) );
fclose( $fhandle );
clearstatcache();
/* ( CONSTRUCT ENTRIES )----------------------------------------------- */
$entries = explode('/n', $contents );
array_pop( $entries );
/* ( START HTML )------------------------------------------------------ */
?>
<html>
<head>
<title>Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #FFFFFF;
margin: 0px;
background-color: #000000;
cursor: default;
}
.head {
background-color: #355CC1;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #CCCCCC;
padding: 3px 6px;
font-weight: bold;
border-top: 1px solid #000000;
border-right: 1px solid #000000;
border-bottom: 3px solid #000000;
border-left: 1px solid #000000;
cursor: default;
}
.mid {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #355CC1;
background-color: #C0C0C0;
border: 1px solid #000000;
padding: 2px 6px;
cursor: default;
}
.lower {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #000000;
background-color: #808080;
border: 1px solid #000000;
padding: 6px;
cursor: default;
}
-->
</style>
</head>
<body>
<p> </p>
<p> </p>
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="25%" height="18" class="head"><B>DATE / TIME</B></td>
<td width="50%" class="head"><B>AGENT</B></td>
<td width="25%" class="head"><B>IP ADDRESS</B></td>
</tr>
<?php
/* ( BUILD LOG )-------------------------------------------------------- */
foreach ( $entries as $key=> $event )
{
$data = explode( '|', $event );
?>
<tr>
<td height="24" class="mid">
<?PHP
echo stripslashes( $data['0'] );
?>
</td>
<td class="mid">
<?PHP
echo stripslashes( $data['1'] );
?>
</td>
<td class="mid">
<?PHP
echo stripslashes( $data['2'] );
?>
</td>
</tr>
<tr>
<td height="30" colspan="3" class="lower">
<?PHP
echo stripslashes( $data['3'] );
?>
</td>
</tr>
<tr>
<td height="18" colspan="3"> </td>
</tr>
<?PHP
}
?>
</table>
<p> </p>
</body>
</html>
Data Editor (edit_data.php)
Code:
<?PHP
/* ( CHECK REQUEST FOR POST OR GET )----------------------------------- */
if ( isset ( $_POST['submit'] ) )
{
$file = 'log.txt';
$fhandle = fopen( $file, "w" );
fwrite( $fhandle, '' );
fclose( $fhandle );
$fhandle = fopen( $file, "a" );
$x = $_POST['count'];
$i = '1';
while ( $i <= $x )
{
$time = $_POST['time'.$i];
$agent = $_POST['agent'.$i];
$ipadd = $_POST['ipadd'.$i];
$referer = $_POST['referer'.$i];
$data = $time.'|'.$agent.'|'.$ipadd.'|'.$referer.'\n';
fwrite( $fhandle, $data );
$i++;
}
fclose( $fhandle );
clearstatcache();
header( 'location: view.php' );
exit;
}
/* ( GET FILE CONTENTS )----------------------------------------------- */
$file = 'log.txt';
$fhandle = fopen( $file, "r" );
$contents = fread( $fhandle, filesize ( $file ) );
fclose( $fhandle );
clearstatcache();
/* ( CONSTRUCT ENTRIES )----------------------------------------------- */
$entries = explode( "*", $contents );
array_reverse( $entries );
$count = count( $entries );
/* ( START HTML )------------------------------------------------------ */
?>
<html>
<head>
<title>Edit Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #FFFFFF;
margin: 0px;
background-color: #000000;
cursor: default;
}
.head {
background-color: #355CC1;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #CCCCCC;
padding: 3px 6px;
font-weight: bold;
border-top: 1px solid #000000;
border-right: 1px solid #000000;
border-bottom: 3px solid #000000;
border-left: 1px solid #000000;
cursor: default;
}
.mid {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #000000;
background-color: #C0C0C0;
border: 1px solid #000000;
padding: 2px 6px;
cursor: default;
}
.lower {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #000000;
background-color: #808080;
border: 1px solid #000000;
padding: 6px;
cursor: default;
}
-->
</style>
<style type="text/css">
<!--
.fields {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #000000;
border: none;
}
.button {
background : #355CC1;
border : 1 solid #000000;
color : #FFFFFF;
font-family : Verdana, Arial, Helvetica, sans-serif;
font-size : 9px;
font-weight : bold;
}
-->
</style>
</head>
<body>
<p> </p>
<p> </p>
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="count" type="hidden" value="<?PHP echo $count; ?>">
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="39" class="head">There is a total of <?PHP echo $count; ?> events logged in the DB.</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
<?php
/* ( BUILD INTERFACE )---------------------------------------------------- */
$x = '1';
foreach ( $entries as $key=> $event )
{
$data = explode( '|', $event );
$clean = str_replace( '\n', '', $data['3'] );
?>
<tr>
<td width="15%" height="31" class="mid">Date:</td>
<td width="85%" class="mid"><input name="time<?PHP echo $x; ?>" type="text" class="fields" id="time<?PHP echo $x; ?>" value="<?PHP echo stripslashes( $data['0'] ); ?>" size="45"></td>
</tr>
<tr>
<td height="30" class="lower">Agent </td>
<td height="30" class="lower"><input name="agent<?PHP echo $x; ?>" type="text" class="fields" id="agent<?PHP echo $x; ?>" value="<?PHP echo stripslashes( $data['1'] ); ?>" size="45"></td>
</tr>
<tr>
<td height="31" class="mid">IP Address:</td>
<td height="31" class="mid"><input name="ipadd<?PHP echo $x; ?>" type="text" class="fields" id="ipadd<?PHP echo $x; ?>" value="<?PHP echo stripslashes( $data['2'] ); ?>"></td>
</tr>
<tr>
<td height="18" class="lower">Refferer</td>
<td height="18" class="lower"><input name="referer<?PHP echo $x; ?>" type="text" class="fields" id="referer<?PHP echo $x; ?>" value="<?PHP echo stripslashes( $data['3'] ); ?>" size="45">
</td>
</tr>
<tr>
<td height="18" colspan="2"> </td>
</tr>
<?PHP
$x++;
}
?>
</table>
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="15%" class="mid"> </td>
<td width="85%" height="40" class="mid"><input name="submit" type="submit" class="button" id="submit" value=".:submit:."></td>
</tr>
</table>
</form>
<p> </p>
</body>
</html>
so whats wrong with my script? it won't really do anything, i'm not sure but is it something to do with the way i am trying to explode '/n'?? also if you could give me a help with making the edit link so that i can edit one entry at a time. if anyone could try to help me i would greatly appreciate it.