PDA

View Full Version : Variables


crane550
05-14-2006, 11:37 PM
Hi,

I am still working on my chat script, and it is comming along very well. I am more then pleased with my progess so far, it is better then I thought. Your alls input has helped a great deal. I still have some more questoins, however.

How would I go about storing variables in a text file? Say I have an array called mydata[], and in it I had info like names, times, and such. How do I go about breaking those down into a text file, and then retrieving them back again?

Thank you so much for your help.

Here is the site where my work in progess is: www.charlescrane.net/chat/index.html

And the code I have so far:

Index.html
<script type="text/javascript">
/* Writing Ajax Requests */
var http_request=false;var http_request2=false;var intUpdate;function ajax_request(url){http_request=false;if(window.XMLHttpRequest){http_request=new XMLHttpRequest();if(http_request.overrideMimeType){http_request.overrideMimeType('text/xml');}}else if(window.ActiveXObject){try{http_request=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{http_request=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}}
if(!http_request){alert('Giving up :( Cannot create an XMLHTTP instance');return false;}
http_request.onreadystatechange=alertContents;http_request.open('GET',url,true);http_request.send(null);}
function alertContents(){if(http_request.readyState==4){if(http_request.status==200){rec_response(http_request.responseText);}else{}}}

/* Reading Ajax Requests */
function ajax_request2(url){http_request2=false;if(window.XMLHttpRequest){http_request2=new XMLHttpRequest();if(http_request2.overrideMimeType){http_request2.overrideMimeType('text/xml');}}else if(window.ActiveXObject){try{http_request2=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{http_request2=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}}
if(!http_request2){alert('Giving up :( Cannot create an XMLHTTP instance');return false;}
http_request2.onreadystatechange=alertContents2;http_request2.open('GET',url,true);http_request2.send(null);}
function alertContents2(){if(http_request2.readyState==4){if(http_request2.status==200){rec_chatcontent(http_request2.responseText);}else{}}}

/* Chat Stuff */
waittime=2000;
intUpdate=window.setTimeout("read_cont()", waittime);
chatwindow.value = "connecting...";

function display_msg(msg1) {
/* Fill Textarea with the Content */
chatwindow.value = msg1;
}

function write_msg(msg1) {
ajax_request("w.php?m=" + escape(msg1));
}

function submit_msg() {
/* Send My Message */
write_msg(" " + namemsg.value + ": " + chatmsg.value);
chatmsg.value="";
}

function rec_response(str1) {
/* Response From w.php */
}

function rec_chatcontent(cont1) {
if (cont1 != "") {
out1 = "";
/* Display Last Message First */

out1 = unescape(cont1);
if (chatwindow.value != out1) { display_msg(out1); }
intUpdate=window.setTimeout("read_cont()", waittime);
}
}

function read_cont() {
/* Prevent Buffering by using ?x=timeinms */
zeit = new Date();
ms = (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds();
ajax_request2("chat.txt?x=" + ms);
}
function keyup(arg1) { if (arg1 == 13) { submit_msg(); } }
</script>

w.php
$waitsecs = 5;
$lastvisit = $_COOKIE["lachatlv"];
setcookie("lachatlv", time());

if ($lastvisit != "") {
$diff = time() - $lastvisit;
if ($diff < $waitsecs) { die(); }
}


<?php
/* spam-keywords & block ip's */
$spam[] = "wojo.com";

$blockip[] = "72.60.167.89";

$msg = $_REQUEST["m"];
if ($msg != "") {

/* Spam Control Part 1 */
if (strlen($msg) < 2) { die(); }

foreach ($blockip as $a) {
if ($_SERVER["REMOTE_ADDR"] == $a) { die(); }
}

$mystring = strtoupper($msg);
foreach ($spam as $a) {
if (strpos($mystring, strtoupper($a)) === false) {
/* Everything Ok Here */
} else {
die();
}
}

/* Preparing the Content */
$fn = "chat.txt";
$maxlines = 20;

$handle = fopen ($fn, 'r');
$chattext = fread($handle, filesize($fn)); fclose($handle);

$arr1 = explode("\n", $chattext);

if (count($arr1) > $maxlines) {
/* Pruning */
$arr1 = array_reverse($arr1);
for ($i=0; $i<$maxlines; $i++) { $arr2[$i] = $arr1[$i]; }
$arr2 = array_reverse($arr2);
} else {
$arr2 = $arr1;
}

$chattext = implode("\n", $arr2);

/* Spam Control Part 2 */
if (substr_count($chattext, $msg) > 2) { die(); }

/* Write */
$out = $chattext . $msg . "\n";
$out = str_replace("\'", "'", $out);
$out = str_replace("\\\"", "\"", $out);

$handle = fopen ($fn, 'w'); fwrite ($handle, $out); fclose($handle);
}
?>

Frank
05-22-2006, 11:05 PM
Here is something that might be useful. In ASP, you could use Application Variables to store data in memory and all users would have access to that data. Since PHP does not have this capability, flat files can be used as a work-around. It sounds like the same work-around for that problem might be helpful for your problem.

Check out this page:
http://www.leosingleton.com/projects/code/phpapp/

I only just started in PHP myself about a month or so ago, but if I can adapt this procedure for my needs, I'm sure you can do it too.

HTH