php delete and create ?

Post Reply
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

php delete and create ?

Post by Mysoogal » Mon Jan 04, 2010 1:01 am

hello, again lol neo and everybody,

i'm wondering about something, i've got a chat script that doesnt use mysql, its flat based no mysql used

all the messages go into msg.html so after time you know this will fill the html into Mbs to gigabytes

how would i delete everything inside msg.html through php ?

or can i delete msg.html and recreate msg.html through php ? is this possible,
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: php delete and create ?

Post by Mysoogal » Mon Jan 04, 2010 1:05 am

i find this

Code: Select all

$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle); 
so i would make , for example

cleanMessages.php

Code: Select all

<php?
$ourFileName = "msg.html";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
 
it will just overwrite the msg.html and thus clean it ?
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: php delete and create ?

Post by Mysoogal » Mon Jan 04, 2010 1:10 am

sorry i've figured it out already

i will use ajax with mootools or jason to reload url cleanMessages.php every 10 mints thus cleans msg.html :D
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: php delete and create ?

Post by Neo » Mon Jan 04, 2010 1:23 am

Can it be that "testFile.txt" holds the real list of messages and the HTML is just used for viewing on the browser ?
If that is the case you would open it as you have noted.

However, You might need to think of a free php based shout box which automatically handle this.
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: php delete and create ?

Post by Mysoogal » Mon Jan 04, 2010 1:39 am

chat.php

Code: Select all

<?php
session_start();

function createForm(){
?>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <table align="center">
          <tr><td colspan="2">Please eneter a nickname to login!</td></tr>
          <tr><td>Your name: </td>
          <td><input class="text" type="text" name="name" /></td></tr>
          <tr><td colspan="2" align="center">
             <input class="text" type="submit" name="submitBtn" value="Login" />
          </td></tr>
        </table>
      </form>
<?php
}

if (isset($_GET['u'])){
   unset($_SESSION['nickname']);
}

// Process login info
if (isset($_POST['submitBtn'])){
      $name    = isset($_POST['name']) ? $_POST['name'] : "Unnamed";
      $_SESSION['nickname'] = $name;
}

$nickname = isset($_SESSION['nickname']) ? $_SESSION['nickname'] : "Hidden";   
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Micro Chat</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
    <script language="javascript" type="text/javascript">
    <!--
      var httpObject = null;
      var link = "";
      var timerID = 0;
      var nickName = "<?php echo $nickname; ?>";

      // Get the HTTP Object
      function getHTTPObject(){
         if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
         else if (window.XMLHttpRequest) return new XMLHttpRequest();
         else {
            alert("Your browser does not support AJAX.");
            return null;
         }
      }   

      // Change the value of the outputText field
      function setOutput(){
         if(httpObject.readyState == 4){
            var response = httpObject.responseText;
            var objDiv = document.getElementById("result");
            objDiv.innerHTML += response;
            objDiv.scrollTop = objDiv.scrollHeight;
            var inpObj = document.getElementById("msg");
            inpObj.value = "";
            inpObj.focus();
         }
      }

      // Change the value of the outputText field
      function setAll(){
         if(httpObject.readyState == 4){
            var response = httpObject.responseText;
            var objDiv = document.getElementById("result");
            objDiv.innerHTML = response;
            objDiv.scrollTop = objDiv.scrollHeight;
         }
      }

      // Implement business logic    
      function doWork(){    
         httpObject = getHTTPObject();
         if (httpObject != null) {
            link = "message.php?nick="+nickName+"&msg="+document.getElementById('msg').value;
            httpObject.open("GET", link , true);
            httpObject.onreadystatechange = setOutput;
            httpObject.send(null);
         }
      }

      // Implement business logic    
      function doReload(){    
         httpObject = getHTTPObject();
         var randomnumber=Math.floor(Math.random()*10000);
         if (httpObject != null) {
            link = "message.php?all=1&rnd="+randomnumber;
            httpObject.open("GET", link , true);
            httpObject.onreadystatechange = setAll;
            httpObject.send(null);
         }
      }

      function UpdateTimer() {
         doReload();   
         timerID = setTimeout("UpdateTimer()", 5000);
      }
    
    
      function keypressed(e){
         if(e.keyCode=='13'){
            doWork();
         }
      }
    //-->
    </script>   
</head>
<body onload="UpdateTimer();">
    <div id="main">
      <div id="caption">Micro Chat</div>
      <div id="icon">&nbsp;</div>
<?php 

if (!isset($_SESSION['nickname']) ){ 
    createForm();
} else  { 
      $name    = isset($_POST['name']) ? $_POST['name'] : "Unnamed";
      $_SESSION['nickname'] = $name;
    ?>
      
     <div id="result">
     <?php 
        $data = file("msg.html");
        foreach ($data as $line) {
            echo $line;
        }
     ?>
      </div>
      <div id="sender" onkeyup="keypressed(event);">
         Your message: <input type="text" name="msg" size="30" id="msg" />
         <button onclick="doWork();">Send</button>
      </div>   
<?php            
    }

?>
    </div>
</body>   


messages.php

[code=php]

<?php 
    if (isset($_GET['msg'])){
        if (file_exists('msg.html')) {
           $f = fopen('msg.html',"a+");
        } else {
           $f = fopen('msg.html',"w+");
        }
      $nick = isset($_GET['nick']) ? $_GET['nick'] : "Hidden";
      $msg  = isset($_GET['msg']) ? htmlspecialchars($_GET['msg']) : ".";
      $line = "<p><span class=\"name\">$nick: </span><span class=\"txt\">$msg</span></p>";
        fwrite($f,$line."\r\n");
        fclose($f);
        
        echo $line;
        
    } else if (isset($_GET['all'])) {
       $flag = file('msg.html');
       $content = "";
       foreach ($flag as $value) {
           $content .= $value;
       }
       echo $content;

    }
?>    

it

[/code]


it looks like just opens msg.html and adds msg i think. i could probably change that to messages.txt :mrgreen:

its ok i figured a wayyy better way to fire cleanMessages.php using Online cron jobs, so every 15 mints it will clean it :D

yes i thought about adding some shoutbox but the thing is, most of them are really ugly and not simple to use for example

every shoutbox, requests that you add your name,website,email,then message

in my shoutbox, you only add ur nickname, and type messages simple as that.
Post Reply

Return to “PHP & MySQL”