Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

PHP - Archiving Posts


I have created a script that allows members to make posts on a site.  However, I would like to archive any posts other than the msot recent, lets say, 10, in a separate file.  How could I achieve this?  I was thinking i could put in a line of hidden code before each post that the sctipt would detect, and every time a post is made, it would remove and write into a new file the HTML code that follows the last of such a hidden line.  Is this possible?  Or is there another solution.  I am trying to avoid MySQL, but if that is absolutely necessary, I will use it.  The site I am talking about is here:


If any existing script code is required, please e-mail me at harrisonchen@yahoo.com

Harrison Chen
1 Solution
what I did is this :
1) I've an include on top of each page of my forum to check the existence of the session (and set default values like "visitor" if not). This is just after the session_start() pf course
2) in that file, whenever a user comes in "new" ("visitor") and is provided with the login page, I check chether I need to "backup" too-old messages
3) if yes, then I run a function to "clean-up the DB"
4) I prevent double-work by using a special table "status" built into each of my sites (message of the day, status of periodic actions,lastmod, forum status = closed, in maintenance, open, restricted, etc).

Simple and efficient

Let you have a look at this :

includedfile.php :
[some stuff here]
// now check forum's status and is there any cleaning up to do ?
$linkID=mysql_pconnect("$dbHost","$dbLogin","$dbPassword") or die ("bad connect".mysql_error());
mysql_select_db($dbName,$linkID) or die ("bad select DB ".mysql_error());
$query="select * from $dbTableStatus where id='1'";
$result=mysql_query($query,$linkID) or die ("bad query get forum status. ".mysql_error());
$locCleanToDo=$res["CleanToDo"]; // this is the example I take for you

$sess_bloque=$res["bloque"]; // 0 -ok-,1 -plus poster avant mois suivant-,2-bloqui (nonpaiement)-

switch ($locEtat) {
case "0" :
case "1" :
case "2" :
case "3" :
default :
  LogAction($sess_pseudo,$REMOTE_ADDR,"Forum in abnormal status, closing...",2);
} // switch
[... here some other irrelevant stuff]
// I'm only doing this for the first session renewed on each Monday
if ($ov==1) { // $locStatsToDo est synchronisi
  if ($poub=='Mon') { $nv=0; $doIt=TRUE; }
  else {$nv=$ov; } //  R`F
} else if ($poub!='Mon') { $nv=1; $doIt=FALSE; }
else $nv=$ov;
if ($nv!=$ov) { // mise ` jour BdD
  $query="update $dbTableStatus set CleanToDo=$nv, StatsToDo=$nv where id=1";
  $result=mysql_query($query,$linkID) or die ("bad query set stats1 via query=$query . ".mysql_error());
// here I launch the real work of cleaning up a table !!
if ($doIt) { // travail effectif
  $Action=0; // on return below, indicates if processing has to take place or not
  // this is the interesting lmine ; 6 is a number of days to come
  $Action=CheckUpload(6,$contenu); // from dbvarious.inc
  if ($Action==1) {
    //VGR17112001 ADDed this automatic cleansing
    LogAction('automate',$REMOTE_ADDR,'cleaning upload of obsolete entries...',2);
  } // if action
  else LogAction('automate',$REMOTE_ADDR,'CHKUPL Monday, not yet done, but nothing to do',2);

} // do It

// now resume normal processing done on each request
if ($sess_shut=="1") {
  LogAction($sess_pseudo,$REMOTE_ADDR,"rejection (forum is shut down) sessinc.",2);
  echo "forum shut down for maintenance... come back later<BR>";
  exit; // sort du script
} else {
//VGR06052001 : ADDed liste noire (bannis)
// here I check banned IPs and Pseudos (black-list) etc
// now I offer the login form
// and if it passed OK, I read user's data from the DB, I set up his/her settings,

NOW IN DBVARIOUS.INC I've this kind of function to clean up the "upload" area (DB's table and actual filesystem's directory for upload)
// please adapt to your needs
// the returned parameter contains the (HTML-formatted) list of files having been cleaned up, the total amount of bytes cleaned, etc - you can strip this
function CleanUpload(&$contenu) {
  GLOBAL $dbHost, $dbName, $dbLogin, $dbPassword, $dbTableUpload,$sess_osWin;

  $locTo=date('Y-m-d H:I:s'); //date("Y-n-j G:i:s"); // date du jour formatie en datetime-compatible
//echo "$locTo<BR>";

  if ($sess_osWin==1) $linkID=mysql_connect("$dbHost","$dbLogin","$dbPassword") or die ("bad connect".mysql_error());
   else $linkID=mysql_pconnect("$dbHost","$dbLogin","$dbPassword") or die ("bad connect".mysql_error());
  mysql_select_db($dbName,$linkID) or die ("bad select DB ".mysql_error());
  //1 : dij` expiris
  $query="select * from $dbTableUpload where expiration<='$locTo' order by dateheure asc;";
//echo "query $query<BR>";

  $result=mysql_query($query,$linkID) or die ("bad query get clean upload. ".mysql_error());
  if (mysql_num_rows($result)>0) {
    $contenu="$locTo<BR>Effacement des fichiers pirimis dans upload :<BR><UL>";
    while ($res=mysql_fetch_array($result)) {
//echo "$loc1 $loc4<BR>";
//debug windows
//echo "risultat unlink = $totor<BR>";
      //VGR27042002 BUGFIX inversion du test sur $totor=unlink() .......
      if ($totor!=0) $contenu.="<LI>OK EFFACEMENT $loc4 (ID=$loc1, taille=$loc8) d\'auteur $loc2 posti le $loc3 et dicrit par \"$loc7\"</LI>";
else       $contenu.="<LI>IMPOSSIBLE D\'EFFACER (raison coderetour=$totor) $loc4 (ID=$loc1) d\'auteur $loc2 posti le $loc3 et dicrit par \"$loc7\"</LI>";

    $contenu.="</UL><BR><BR>ce qui reprisente $compteur octets libiris.<BR>";
//echo "$contenu<BR>";
  $query="delete from $dbTableUpload where expiration<='$locTo';";
  $result=mysql_query($query,$linkID) or die ("bad query perform clean upload. ".mysql_error());
  } // if travail ` faire

} // CleanUpload procedure

I don't give you function CheckUpload($parNJ,&$contenu), it's almost the same

Hope this helps,

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Accept: VGR {http:#8101430}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

EE Cleanup Volunteer

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now